0

Solution: what worked in the end was adding

  global $post;

at the top of the query, so before

$custom_taxterms = ...

Bill Erickson's Genesis custom loop is the template I followed in the end.

Original question

I was hoping for some help with an issue that has me stumped.

I have the following query. It works and outputs data.

$custom_taxterms = wp_get_object_terms( $post->ID, 'region', array('fields' => 'ids') );
// arguments
$args = array(
    'post_type' => 'stay',
    'post_status' => 'publish',
    'posts_per_page' => 25, // you may edit this number
    'orderby' => 'rand',
    'tax_query' => array(
        array(
            'taxonomy' => 'region',
            'field' => 'id',
            'terms' => $custom_taxterms
        )
    ),
    'post__not_in' => array ($post->ID),
);
$related_items = new WP_Query( $args );
// loop over query
if ($related_items->have_posts()) :
    echo '<ul>';
    while ( $related_items->have_posts() ) : $related_items->the_post();
        ?>
        <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
        <?php
    endwhile;
    echo '</ul>';
endif;
// Reset Post Data
wp_reset_postdata();

If I now put it into a function, it stops working.

add_action( 'genesis_loop', 'related_posts_cpt' );
function related_posts_cpt (){
    // same loop as above
}

Just to be clear, if I use the same function with a different output, it does work:

add_action( 'genesis_loop', 'related_posts_cpt' );
function related_posts_cpt() { 
    ?>
    <p>AAAAAAAAHHHHHH</p>
    <?php
}

What am I missing? Thank you in advance.

5
  • 1
    Since Genesis Framework is 3rd Party ( Not WordPress Core ) it is considered to be Off-Topic. That being said, a quick search came up with this Replace default Genesis loop with Custom loop which may be of some use.
    – Howdy_McGee
    May 30, 2017 at 19:37
  • 1
    wp_get_object_terms() can also return WP_Error, so we might need to check for that too. Most likely your $post variable is undefined when you move your code inside a function, in a shelter from the global scope.
    – birgire
    May 30, 2017 at 20:30
  • 1
    Have you tried using global $post;? also try to echo out your post id and other variables to make sure they are not empty.
    – Sam
    May 31, 2017 at 7:27
  • Global $post turned out to make it work. Thank you! May 31, 2017 at 8:21
  • Glad it worked! You should update your question with the working code so others can benefit.
    – Sam
    May 31, 2017 at 11:58

1 Answer 1

0

functions return an output. you don't want to just echo it out.

try this as your function:

function mycoolquery() {
    $custom_taxterms = wp_get_object_terms( $post->ID, 'region', array('fields' => 'ids') );
// arguments
    $args = array(
        'post_type' => 'stay',
        'post_status' => 'publish',
        'posts_per_page' => 25, // you may edit this number
        'orderby' => 'rand',
        'tax_query' => array(
            array(
                'taxonomy' => 'region',
                'field' => 'id',
                'terms' => $custom_taxterms
            )
         ),
        'post__not_in' => array ($post->ID),
    );
    $related_items = new WP_Query( $args );
// loop over query
    if ($related_items->have_posts()) :
       $output =  '<ul>';
       while ( $related_items->have_posts() ) : $related_items->the_post();

            $output .= '<li><a href="'.the_permalink().'">'.the_title().'</a></li>';

        endwhile;
        $output .= '</ul>';
    endif;
    // Reset Post Data
    wp_reset_postdata();
    return $output;
}

then echo out the function where you want it:

echo mycoolquery();
1
  • Indentation would be helpful here. Also note that the_* functions here can't be appended to string as they echo the output, not return it. The $post is also undefined, maybe you can use the post id as an input argument to the my_cool_query( $post_id ) instead?
    – birgire
    May 30, 2017 at 20:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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