Created
June 29, 2017 14:22
-
-
Save alexstandiford/20700439eda971b4e6efcecc93b02aad to your computer and use it in GitHub Desktop.
Just a simple way to search for things in a WordPress install. Allows for an easy way to search for locations where a shortcode is used
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function tas_search(){ | |
ob_start(); | |
//Searches by string | |
$args = [ | |
's' => 'yop_poll', //Or whatever | |
'post_type' => 'any', | |
'posts_per_page' => -1, | |
]; | |
//Searches for a page template | |
/* | |
$args = [ | |
'post_type' => 'page', | |
'meta_key' => '_wp_page_template', | |
'meta_value' => 'template-events.php', //Or whatever | |
'posts_per_page' => -1, | |
];*/ | |
$the_query = new WP_Query($args); | |
if($the_query->have_posts()){ | |
echo '<ul>'; | |
while($the_query->have_posts()){ | |
$the_query->the_post(); ?> | |
<li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li> | |
<?php | |
} | |
echo '</ul>'; | |
} | |
else{ | |
echo "Sorry no posts found"; | |
} | |
wp_reset_postdata(); | |
return ob_get_clean(); | |
} | |
add_shortcode('wpquery', 'tas_search'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Most of the credit for this goes to: http://www.wpbeginner.com/wp-tutorials/how-to-find-and-remove-unused-shortcodes-from-wordpress-posts