Advertisement
BakerMan

Unaffected query within shortcode

Oct 17th, 2013
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.54 KB | None | 0 0
  1. add_shortcode('find_my_special_posts', 'find_my_special_posts');
  2.  
  3. function find_my_special_posts() {
  4.     $posts = new WP_Query(array(
  5.         'posts_per_page' => -1,
  6.         'post_type' => 'post',
  7.         'tax_query' => array(array(
  8.             'taxonomy' => 'category',
  9.             'field' => 'slug',
  10.             'terms' => 'random-post-term'
  11.         ))
  12.     ));
  13.  
  14.     $output = '';
  15.  
  16.     while ($posts->have_posts()) {
  17.         $posts->the_post();
  18.         $output .= '<p>' . get_the_title() . '</p>';
  19.     }
  20.  
  21.     wp_reset_postdata();
  22.  
  23.     return empty($output) ? "<p> Sorry matey but you're out of luck. </p>" : $output;
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement