Skip to main content

Get All Posts Containing a Shortcode in WordPress

WordPress is a content management system that enables users to create and manage websites. One of the features of WordPress is the ability to use shortcodes. Shortcodes are snippets of code that can be used to insert content into a post or page.

WordPress is not aware of your shortcode until it is rendered on the front end. This means that when WordPress sees the shortcode in the content and replaces it, that is when it becomes aware that your shortcode exists. The only solution is to use the LIKE parameter and pass your shortcode attributes as params.

In this code snippet, we will learn how to get all WordPress posts containing a shortcode. This can be very useful if you want to target all posts with a specific shortcode and then perform some action on them.

global $wpdb;
$results = $wpdb->get_results("SELECT ID FROM {$wpdb->posts} WHERE post_content LIKE '%[shortcode_name%'", ARRAY_N);

By continuing to use the site, you agree to the use of cookies.