WordPress Snippets at WPcustoms

Find posts containing a shortcode

A little helper to find posts which contain a specific shortcode. If you switch themes and the shortcodes are not transfered to a plugin your site’s content will break. If you have many posts you can use this helping snippet to list all posts containing your specified shortcode so you can remove or edit the broken posts and pages.


/**
 * Snippet Name: Find posts containing a shortcode
 * Snippet URL: https://wpcustoms.net/snippets/find-posts-containing-shortcode/
 */
  // usage in post\page: [shortcodefinder find='myshortcode']
function wpc_find_shortcode($atts, $content=null) { 
ob_start();
extract( shortcode_atts( array(
		'find' => '',
	), $atts ) );

$string = $atts['find'];

$args = array(
	's' => $string,
	);

$the_query = new WP_Query( $args );

if ( $the_query->have_posts() ) {
        echo '
    '; while ( $the_query->have_posts() ) { $the_query->the_post(); ?>
  • '; } else { echo "Sorry no posts found"; } wp_reset_postdata(); return ob_get_clean(); } add_shortcode('shortcodefinder', 'wpc_find_shortcode');