Skip to content

Instantly share code, notes, and snippets.

@elseloop
Created March 7, 2018 22:01
Show Gist options
  • Select an option

  • Save elseloop/2a9cbd2595401db0a6fbff7429ce909b to your computer and use it in GitHub Desktop.

Select an option

Save elseloop/2a9cbd2595401db0a6fbff7429ce909b to your computer and use it in GitHub Desktop.
WordPress function to print all instances of a passed shortcode, using built-in WP search
function turn_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 '<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('shortcodefinder', 'turn_find_shortcode');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment