Skip to content

Instantly share code, notes, and snippets.

@alexstandiford
Created June 29, 2017 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save alexstandiford/20700439eda971b4e6efcecc93b02aad to your computer and use it in GitHub Desktop.
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
<?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');
@alexstandiford
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment