0

I'm trying to use wp_query as part of a page template. The page is tagged with a taxonomy term and I'm trying to grab a list of posts that are also tagged with this taxonomy term.

I'm able to grab the term itself from the page no problem, and use them as part of the $args which seem to be working ok as well.

However anything that uses WP_Query even with global $post returns nothing, and any code below the wp_query in the function won't execute.

I'm utilising the Genesis framework so this may have something to do with it but I'm really struggling to figure this out. using WP_Query in shortcodes has functioned fine so why not here?

Here is the code

/**
 * Template Name: Car Page
 *
 */

add_action( 'genesis_entry_footer', 'racelistfunc2' );

function racelistfunc2() {
   $terms = get_the_terms( $post->ID, 'vehicle' ) ;
   if ($terms) {
    $terms_slugs = array();
    foreach ( $terms as $term ) {
        $terms_slugs[] = $term->slug;
    }
    $race = $terms_slugs[0];      
   }



    $args = array(
        'tax_query' => array(
            array(
        'taxonomy' => 'vehicle',
        'field' => 'slug',
        'terms' => $race

    )
)
    );

 echo $race; /* test to see if all functioning ok (it is here) */


       $race_posts = new WP_Query( $args );

   if ($race_posts->have_posts()) {
       while ( $race_posts->have_posts() ) {
           $race_posts->the_post();
    $html .= "<li class='racelistnum'><a href='" . get_permalink($post->ID) . "'>" . get_the_title() . "</a></li>";
    return $html;
      }
   }
      else  {   
      $html .= "<p>no posts lol </p>";
    return $html; 

      }
       wp_reset_postdata();

    echo 'test here'; /* no code functions here */

     }

    genesis();
  • if this is your actual code, you can see the problem above, the comment after echo $race; isn't closed. – Milo Aug 13 '15 at 15:29
  • oops! no that was just inserted for the basis of this question! – tim daniels Aug 13 '15 at 15:33
  • return $html; inside your while loop is halting execution and exiting the function. – Milo Aug 13 '15 at 15:40
  • shouldn't it be echo $html; ? – Michael Aug 13 '15 at 18:20
1

Couple of issues are seen here -

  1. You have hooked a function in a template file whereas such code should go in functions.php instead or you can directly put the code of this function in the template.
  2. You does not seem to be getting value of $post inside this function.
  3. Both the return statements should be removed and just echo $html; should be placed just after the wp_reset_postdata() in your current code.
  • Argh it really was just because of echo! The simplest explanation is usually the right one! Also it has to be a function due to the way Genesis works :\ Working fine now, thanks! – tim daniels Aug 14 '15 at 8:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.