Get all posts containing a shortcode

I’m writing a WP plugin that involves using shortcode. One of the missions is to display all (publish) posts that contain my shortcode regardless of their post type. Is there a built-in function for this?

Thanks in advance.

Related posts

Leave a Reply

1 comment

  1. WordPress isn’t aware of your shortcode until it’s rendered on the front end. So when WP sees it in the content and replaces it, that’s when it’s aware that your shortcode exists. It also promptly forgets about it afterward, of course.

    So there’s no built in function to do what you’re asking. The best you can do is probably write a LIKE query which may or may not be a good idea.

    <?php
    function wpse87582_find_shortcode_posts()
    {
        global $wpdb;
        return $wpdb->get_results("SELECT ID FROM {$wpdb->posts} WHERE post_content LIKE '%[your_shortcode_here%'", ARRAY_N);
    }