Vignesh Pichamani
Vignesh Pichamani

Reputation: 8070

Get the Page/Post ID of included Shortcode in WordPress

I am trying to find some information on how to get the page id if i include unique shortcode in the text editor of wordpress. Let say I have a shortcode as [testingshortcode] is doing some function(displaying a form) put it in the editor of wordpress. Now I want to find that shortcode is use in which page id?

Is there is a way to find that shortcode is used in which page?

Any Suggestion would be great :)

Upvotes: 1

Views: 1314

Answers (1)

oelna
oelna

Reputation: 2436

I'm not sure I understood the question correctly, but I'll give it a go.

I'm not too much of a Wordpress expert, so I don't know whether a built-in functionality exists, but I immediately thought: this would be easy to do with a manual SQL query.

So I set up the query like this:

<?php
    /* global $wpdb; //you may need to enable this */
    $results = $wpdb->get_results('SELECT ID FROM '.$table_prefix.'posts WHERE post_content LIKE "%[testingshortcode]%" AND post_parent = 0');
    var_dump($results);
?>

Depending on where you'll be using this, you may need to enable the global $wpdb variable.
The query does a simple search on the Wordpress database and retrieves all posts that mention the string [testingshortcode]. At first I was surprised to see it return more IDs than I expected, but then I remembered Wordpress saves post revisions now, so I filtered those out using the post_parent = 0 statement. It should only return genuine post IDs now.

More info on using the $wpdb class on the Worpress Codex.

I hope this helps.

Upvotes: 3

Related Questions