Exclude current post from results

Forums › Forums › Search & Filter Pro › Exclude current post from results

Viewing 10 posts – 1 through 10 (of 10 total)
  • Anonymous
    #183719

    Hi,

    I have a custom post type of artists which pulls in my custom fields for that artist. At the bottom of each artist page I have the results showing all artists. What I would is for it to show artists apart from the one you are currently viewing.

    So, you are viewing artist A and at the bottom of the page is the results for all artists excluding artist A.

    Is this something that is easily achievable?

    Thanks

    Trevor
    #183720

    This thread starts the answer:

    https://support.searchandfilter.com/forums/topic/is-there-a-way-to-exclude-the-the-current-post-id/

    Your code you would have to get the current post id (using the get_the_ID() function)

    And then add a post__not_in argument to the arguments.

    I do not have a code snippet for you though.

    Anonymous
    #183892

    Hi,

    Below is the code snippet I have used but it doesn’t seem to be working. I get ‘No results found’. Can you see anything obviously wrong with it?

    function filter_function_name( $query_args, $sfid ) {
    
    	//if search form ID = 264, the do something with this query
    	if($sfid==264) {
    		//modify $query_args here before returning it
    		// $query_args['somearg'] = 'newvalue';
    		$postid = get_the_ID();
    		$query_args = array(
    			'post_type' => 'artist',
    			'post__not_in' => $postid,
    		);
    	}
    
    	return $query_args;
    }
    add_filter( 'sf_edit_query_args', 'filter_function_name', 20, 2 );
    Trevor
    #183922

    Hi

    Would it not be:

    function filter_function_name( $query_args, $sfid ) {
    
    	//if search form ID = 264, the do something with this query
    	if($sfid==264) {
    		//modify $query_args here before returning it
    		// $query_args['somearg'] = 'newvalue';
    		$postid = get_the_ID();
    		$query_args['post__not_in'] = $postid
    	}
    
    	return $query_args;
    }
    add_filter( 'sf_edit_query_args', 'filter_function_name', 20, 2 );
    Anonymous
    #183959

    Sadly that hasn’t worked.

    I am using a custom post type called artist though so do I not need to specify that too?

    Thanks

    Trevor
    #183979

    Not if you have already specified that in the search form setup. You might have to do this:

    function filter_function_name( $query_args, $sfid ) {
    
    	//if search form ID = 264, the do something with this query
    	if($sfid==264) {
    		//modify $query_args here before returning it
    		// $query_args['somearg'] = 'newvalue';
    		global $post;
    		$postid = $post->ID ;
    		$query_args['post__not_in'] = $postid
    	}
    
    	return $query_args;
    }
    add_filter( 'sf_edit_query_args', 'filter_function_name', 20, 2 );
    Anonymous
    #184279

    Hi Trevor,

    I have specified the post type in the search form setup but sadly the code didn’t work.

    I am showing the results using a shortcode, would that make any difference?

    Thanks

    Trevor
    #184286
    This reply has been marked as private.
    Ross Moderator
    #184347

    Hi Dan

    There appears to be a small typo in the code Trevor supplied, post__not_in needs to be an array –

    function filter_post__not_in( $query_args, $sfid ) {
    
    	//if search form ID = 264, the do something with this query
    	if($sfid==264) {
    		global $post;
    		$postid = $post->ID ;
    		$query_args['post__not_in'] = array($postid);
    	}
    	
    	return $query_args;
    }
    add_filter( 'sf_edit_query_args', 'filter_post__not_in', 20, 2 );

    Thanks

    Anonymous
    #184385

    Hi Ross,

    Thank you for this, it worked straight away.

    Dan

Viewing 10 posts – 1 through 10 (of 10 total)
Search & Filter Support
Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.

We also use cookies to store items in your cart as well as allowing your to login on the site.

You can adjust all of your cookie settings by navigating the tabs on the left hand side.

By continuing to use this site, you also agree to our Privacy Policy.