Support » Fixing WordPress » query_posts, get post by second category

  • Alain


    In my sidebar I am trying to retrieve a list of posts from a certain category with query_posts.

    $query = "cat=5&showposts=3";
    $posts = query_posts($query);

    This is working well except for posts that are assigned to more than one category.

    For example:
    Post a,b,c are in category 5 and post d is in category 4 and 5.
    Now my query doesn’t return post d.

    Is there a way to make this work?

    By the way these posts with multiple categories do get displayed when queried in the normal loop for example when I use

Viewing 2 replies - 1 through 2 (of 2 total)
  • <?php
      'category__in' => array(4,5),
      'post_type' => 'post',
      'post_status' => 'publish',
      'posts_per_page' => -1,
      'caller_get_posts'=> 1
    $my_query = null;
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
      echo 'List of Posts';
      while ($my_query->have_posts()) : $my_query->the_post(); ?>
        <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
    wp_reset_query();  // Restore global post data stomped by the_post().

    For posts that belong to category 4 and 5 only see category__and parameter


    if your post d with cat=4,5 is number four in the list, it will not show.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘query_posts, get post by second category’ is closed to new replies.