WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] post__not_in query (3 posts)

  1. greencode
    Member
    Posted 2 years ago #

    I'm using the following to display a list of posts from category with ID 27 but I would also like to exclude a post with ID of 297. Not too sure what I'm doing wrong.

    <?php /* Start the Loop */ ?>
    <?php
    $featuredPosts = new WP_Query();
    $featuredPosts->query('cat=27&post__not_in=297');
    while ($featuredPosts->have_posts()) : $featuredPosts->the_post(); ?>
    <li><a href="<?php the_permalink() ?>"><?php the_title() ?></a></li>
    <?php endwhile; ?>

    Any help would be much appreciated.

  2. alchymyth
    The Sweeper & Moderator
    Posted 2 years ago #

    this will not work with the 'string' parameter syntax - you need to use the 'array' parameter syntax;

    also, 'post__not_in' needs an array as argument;

    http://codex.wordpress.org/Class_Reference/WP_Query
    http://codex.wordpress.org/Class_Reference/WP_Query#Post_.26_Page_Parameters
    http://codex.wordpress.org/Class_Reference/WP_Query#Category_Parameters

    try for example:

    <?php
    $featuredPosts = new WP_Query( array('cat' => 27, 'post__not_in' => array(297)) );
    while ($featuredPosts->have_posts()) : $featuredPosts->the_post(); ?>
  3. greencode
    Member
    Posted 2 years ago #

    Thanks for this - works perfectly

Topic Closed

This topic has been closed to new replies.

About this Topic