Get all posts which contain specific shortcode

Advertisement

While working on some project, I was stuck on one issue.

The issue was, User was multiple posts and he was use my shortcode on some of them.

And I was trying to figure out which are the posts which content have my shortcode.


The answer is in my question. To do this I need to search the shortcode from all the post content.

To achieve this I have used SELECT query. E.g.

My shortcode was something like myshortcode (Not exactly this shortcode, I have use this shortcode for example purpose.)

Example 1: Search shortcode myshortcodein all the posts. (Including posts, pages, custom post types)

global $wpdb;
$ids = $wpdb->get_row("SELECT ID FROM {$wpdb->postmeta} WHERE meta_value LIKE '%myshortcode%'", ARRAY_A);
// print_r( $ids ); // Print all the IDs.

Above SELECT query return the array of IDs of all the posts, pages, custom post types which post content contain myshortcode.


Example 2: Search shortcode myshortcode ONLY in PAGES.

global $wpdb;
$ids = $wpdb->get_row("SELECT ID FROM {$wpdb->posts} WHERE post_content LIKE '%[myshortcode AND post_type='page'", ARRAY_A);
// print_r( $ids ); // Print all the IDs.

Leave a Reply