For example my example.com/sample-page/ is using [my_shortcode param="1"]
.
How can I get all the URLs of the pages that uses [my_shortcode]
?
Is there an approach to achieve that?
For example my example.com/sample-page/ is using [my_shortcode param="1"]
.
How can I get all the URLs of the pages that uses [my_shortcode]
?
Is there an approach to achieve that?
Solution that I've found:
$pages = get_pages();
$pattern = get_shortcode_regex();
foreach($pages as $page) {
if ( preg_match_all( '/'. $pattern .'/s', $page->post_content, $matches )
&& array_key_exists( 2, $matches )
&& in_array( 'my_shortcode', $matches[2] ) )
{
echo $page->post_title . <br />;
}
}
get_shortcode_regex(array('my_shortcode'))
and save yourself the trouble of comparing the $matches
$matches[3]
with that approach.
Commented
Sep 11, 2018 at 6:06
preg_match_all()
might take a long time to execute, so using a simpler regex will probably make it alot faster
has_shortcode()
which can be used for checking if a shortcode exists.
Commented
Sep 11, 2018 at 6:39