• Pete

    (@perthmetro)


    I’ve tried various things like below…

    <?php
    $args = array (
    	'gender' => 'female',
    	'seeking' => 'male-2',
    	'state' => 'south-australia',
    	'posts_per_page' => '4',
            'post__not_in' => array($post->ID),
    	'order' => 'ASC',
    	'orderby' => 'rand',
    );
    ?>
    <?php $query = new WP_Query( $args ); ?>
    <?php
    if ( $query->have_posts() ) {
    	while ( $query->have_posts() ) {
    		$query->the_post();
    ?>
    
    loop stuff goes here
    
    <?php wp_reset_postdata(); ?>

    But I can’t exclude the current post from appearing in this query (sidebar).

    What am I doing wrong?

    Thanks heaps

    Pete

Viewing 5 replies - 1 through 5 (of 5 total)
  • PhPCentre

    (@phpcentre)

    Try this:
    'post__not_in' => array( get_the_ID() ),

    Michael

    (@alchymyth)

    what is the result if you either echo $post->ID or get_the_ID() before your code?

    if you don’t get the ID of your single post, possibly try to add global $post before your code

    tormorten

    (@tormorten)

    Using get_queried_object()->ID could also work if you are on a page where the post object has been set.

    Thread Starter Pete

    (@perthmetro)

    @phpcentre… no joy, but thanks.

    @alchymyth… thanks, i’m not sure I’m putting the global $post in the correct place? Could you tell me where it goes?

    @tormorten… thanks, again would you be able to apply that for me in my snippet?

    Thanks all.

    Michael

    (@alchymyth)

    putting the global $post in the correct place?

    try before this line:
    $args = array (

    it is possible that the post ID could have been currupted by whatever code is between the ‘normal’ loop and your sidebar…
    did you try to print the post ID at the start of your code to see if it is the right one?

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘query posts and exclude current post’ is closed to new replies.