• Resolved Riley Magnuson

    (@rmagnuson)


    I am writing a plugin for wordpress that creates shortcodes. On this plugins settings page, I would like to have it list out what pages the shortcode is being used on. (For ease of access and locating them for quick changes.)

    I figured it would be something with get_post(), where it searched all the posts that matched a filter, then listed them, but I can’t seem to figure out what the syntax for that would be, or if this is even possible.

    Any suggestions?

    Edit: A user on stackoverflow suggested I run a query in the database using the following query; SELECT * FROM wp_posts WHERE post_content LIKE ‘%[mod-%’ AND post_status = ‘publish’ LIMIT 0,1000 however i’m not sure that will work in my case.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Running the query directly on the database would be a whole lot more efficient. On top of that, a call to get_posts() doesn’t have any parameters to check for content, so the only thing that it oculd do is return *everything* and you’d have to loop through manually to find out if anything does exist.

    Stick with the direct query. It’s a whole lot better for what you need.

    Thread Starter Riley Magnuson

    (@rmagnuson)

    @catacaustic, How exactly would this be done then on the wordpress database? I can’t seem to find any information on this task

    The query that you gave before is basically right. The only thing that you’d do is convert it to be more “WordPress’y”. 🙂

    Something like…

    global $wpdb;
    
    $query = "SELECT ID, post_title FROM ".$wpdb->posts." WHERE post_content LIKE '%[mod-%' AND post_status = 'publish'";
    
    $results = $wpdb->get_results ($query);

    Then just loop through the results like normal.

    Just note that I did change that query to only return the ID and post_titlte fields. You might need more depending on what you want to reply, but querying for * will slow things down, and I doubt that you’d need every field anyway.

    Thread Starter Riley Magnuson

    (@rmagnuson)

    Thanks for the help! Managed to do it like this:

    global $wpdb;
    $query = "SELECT ID, post_title, guid FROM ".$wpdb->posts." WHERE post_content LIKE '%[mod-%' AND post_status = 'publish'";
    $results = $wpdb->get_results ($query);

    Then displayed it like so:
    <?php foreach ( $results as $results ) { ?><p><a>ID;?>"><?php echo $results->post_title;?></a><br></p><?php } ?>

    This will list all pages containing “[mod-” and provide a link to them.

    Why SELECT the guid?
    Does your loop show only one, always? It’s using the same variable name twice.
    And I assume you have the correct syntax for a link instead of what you have written there…

    I’m interested in this as part of WordPress core. But they want to use blocks instead of shortcodes these days.

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘List all pages containing shortcode (WordPress)’ is closed to new replies.