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.


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.

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);
  • Thanks @chrisguitarguy. Indeed, seems like it's the only way. My concern is, such a SELECT query will be slow with bigger tables. And FULL TEXT is not an option, as we're not searching for full words here. Any idea? – An Phan Feb 20 '13 at 6:26
  • You could speed it up making a check when saving the post. If the shortcode is in there, add a custom meta value. You could find your posts easily with wp_query then. – Pieter Feb 20 '13 at 9:23
  • Great idea. Let me try following that route. – An Phan Feb 20 '13 at 10:31
  • Cache the result with a transient. So you don't have to make the query very time. – chrisguitarguy Feb 20 '13 at 13:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.