0

I have set up a custom post type (buyers_club) with a custom category (buyers-club) and custom tags (country). I'd like to show a list of related posts by specific category, FEATURED, and by the current tag.

So far I have this but it isn't working:

<ul>
<?php
$country_name = strip_tags( get_the_term_list( $wp_query->post->ID, 'country' ) );
query_posts('post_type=buyers_club&buyers-club=featured&country= .$country_name . &showposts=5');
while ( have_posts() ) : the_post();
?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php endwhile; ?>
<?php wp_reset_query(); ?> 
</ul>   

Can anybody help?

UPDATE

I have changed it to this with mixed results. I have one query that gets the posts of a specified category and current tag. This works except that it does not exclude the current post from the list:

 <?php
        $country_name = strip_tags( get_the_term_list( $wp_query->post->ID, 'country' ) );
        $args = array(
        'post_type'=> 'buyers_club',
        'buyers-club'    => 'Featured',
        'country' => $country_name,
        'order'    => 'ASC',
        'exclude' => $post->ID,
        'posts_per_page' => 5

        );
        query_posts( $args );

        while ( have_posts() ) : the_post();
        echo '<li>';
        the_title();
        echo '</li>';
        endwhile;

// Reset Query
wp_reset_query();
?>

Then I have a query that gets related posts from the current category AND current tag. This does not work:

<?php
        $country_name = strip_tags( get_the_term_list( $wp_query->post->ID, 'country' ) );
        $category_name = strip_tags( get_the_term_list( $wp_query->post->ID, 'buyers-club' ) );
        $args = array(
        'post_type'=> 'buyers_club',
        'buyers-club'    => $category_name,
        'country' => $country_name, 
        'order'    => 'rand',
    'exclude' => $post->ID,
        'posts_per_page' => 5
        );
        query_posts( $args );

        while ( have_posts() ) : the_post();
        echo '<li>';
        the_title();
        echo '</li>';
        endwhile;

// Reset Query
wp_reset_query();
?>
5
  • 2
    For starters, you're missing single quotes before and after the concatenation: ...country='.$country_name.'$showposts=5. If this is still not working: HOW is is not working? And BTW, you should use WP_Query!
    – tfrommen
    Aug 12, 2013 at 10:56
  • showposts is deprecated. Use posts_per_page instead
    – JMau
    Aug 12, 2013 at 11:29
  • @JMa Thanks for getting back so quickly. I have updated my question. As for what is not working... well it's simply not returning a list of the post titles and it is not excluding the current post. Aug 12, 2013 at 12:28
  • @JMa Ok I got the second Query working, I needed to separate the terms with commas. All I need do now is exclude the current post. Any ideas? Aug 12, 2013 at 13:09
  • 1
    never use query_posts for second query >>When should you use WP_Query vs query_posts() vs get_posts()
    – JMau
    Aug 12, 2013 at 13:13

1 Answer 1

1

There is a descriptively named argument to WP_Query, which is what you should be using, called post__not_in which excludes any post IDs passed into it.

Also, the {tax} => {term_name} pattern that you have used twice is deprecated. You should be using a tax_query argument instead.

Lines like this one, strip_tags( get_the_term_list( $wp_query->post->ID, 'buyers-club' ) );, will fail if more than one term is returned. That is not the way you should be doing this. That function isn't meant for returning a list of slugs.

It should look something more like this:

$country_name = wp_list_pluck(get_the_terms( 1, 'country' ), 'term_id')
$category_name = wp_list_pluck(get_the_terms( 1, 'buyers-club' ), 'term_id')
$args = array(
  'post_type'=> 'buyers_club',
  'tax_query' => array(
    array(
      'taxonomy' => 'buyers-club',
      'field' => 'id',
      'terms' => $category_name
    ),
    array(
      'taxonomy' => 'country',
      'field' => 'id',
      'terms' => $country_name
    )
  ),
  'order'    => 'rand',
  'post__not_in' => array($post->ID),
  'posts_per_page' => 5
);
$buyer_club = new WP_Query( $args );

if ($buyer_club->have_posts()) {
  while ( $buyer_club->have_posts() ) {
    $buyer_club->the_post();
    echo '<li>';
    the_title();
    echo '</li>';
  }
}
3
  • Thank you for this, however it's not returning anything at all now. Aug 12, 2013 at 15:26
  • Do you need an OR relationship betwen the meta fields?
    – s_ha_dum
    Aug 12, 2013 at 19:34
  • Typo-- "buyer" not "buyers". Check again. One big problem with questions like this is that it is nearly impossible to test properly as I don't have your data in my database.
    – s_ha_dum
    Aug 13, 2013 at 1:44

Your Answer

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

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