Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question

1 Answer 1

up vote 0 down vote accepted

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 build 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);
}
share|improve this answer
    
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

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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