WordPress.org

Ready to get started?Download WordPress

Forums

query_posts, get post by second category (3 posts)

  1. Alain
    Member
    Posted 4 years ago #

    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 http://www.domainname.com/?cat=5

  2. MichaelH
    Member
    Posted 4 years ago #

    <?php
    $args=array(
      '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>
        <?php
      endwhile;
    }
    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

  3. alchymyth
    Forum Moderator
    Posted 4 years ago #

    showposts=3

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

Topic Closed

This topic has been closed to new replies.

About this Topic