• Hi, i am trying to list all posts that have two specific categories, namely posts that are in the News and and the featured Category.

    I have the following WP_Query object code in my sidebar.php file, it is listing pages for the archive list of posts for which i want to highlight specific featured posts at the top of the sidebar:

    <ul>
    <?php $myPosts = new WP_Query();
    $myPosts->query( array('category__and' => array(get_cat_ID( 'featured' ), get_cat_ID(single_cat_title('')) ), showposts => '5' ) );
    
    while ($myPosts->have_posts()) : $myPosts->the_post(); ?>
    <li><?php $myPosts->the_title(); ?></li>
    <?php endwhile; ?>
    </ul>

    Sorry i am new to wp so i am not sure what i am doing wrong, many thanks in advance.

    Regards,

    Chris

Viewing 1 replies (of 1 total)
  • <?php
    $news_id = get_cat_ID('News');
    $featured_id = get_cat_ID('Featured');
    $cat_ids = explode(',',$news_id.','.$featured_id);
    $args=array(
      'category__and' => $cat_ids,
      '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().
    ?>
Viewing 1 replies (of 1 total)

The topic ‘List posts with TWO categories’ is closed to new replies.