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);

Scroll to Top

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

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.