• Hi there I was wondering if there’s anyway to exclude a post returned by a wp_query using the post’s ID.

    See I have one section of the page (on left for eg) which shows one featured post (w/ excerpt + large image etc) then on the other side (on right for eg) of the page, there are more featured/popular posts (just showing a small image). By default the post on the left is the first post from the list of featured posts, and the same for the section on the right. So I don’t want two of the same posts showing up.

    Is there any way in which I can use the_ID() from the “right query” in the “left query” as to exclude it from the returned posts. (e.g “WHERE post_id !=$featuredPost; …or something like that, in a wordpress query though)

Viewing 5 replies - 1 through 5 (of 5 total)
  • Try this: WP_Query(‘p=-1’);
    Replace 1 with post id and make sure there is – before it.

    Thread Starter dcairbre

    (@dcairbre)

    hey I tried it with the “-” but it just returned that post here’s the query if you want:

    new WP_Query("p=-$featuredpostid" , array ( 'orderby' => 'date', 'posts_per_page' => '3', 'order' => 'DESC' ) );

    I got $featuredpostid from get_the_ID(); in an earlier loop

    thanks for the help.

    I have an idea, but not sure if it would work. In wp-includes/query.php in function parse_query replace

    $qv['p'] =  absint($qv['p']);

    with

    $qv['p'] =  intval($qv['p']);

    then in function get_posts() below replace this

    $where .= " AND {$wpdb->posts}.ID = " . $q['p'];

    with this

    $where .= " AND {$wpdb->posts}.ID " . ($q['p'] > 0 ? ' = ' : ' != ') . abs($q['p']);

    and try your code with minus before post id again

    Thread Starter dcairbre

    (@dcairbre)

    no, unfortunately it just returned a blank list ??

    Try

    new WP_Query( array ('post__not_in' => $featuredpostid, 'orderby' => 'date', 'posts_per_page' => '3', 'order' => 'DESC' ) );

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘WP_Query exclude post by ID’ is closed to new replies.