Support Forums

The forums are closed and will be removed when we launch our new site.

Looking for support? You can access the support system via your account.

Forums Forums Search & Filter Pro Exclude current post from results

Viewing 10 posts - 1 through 10 (of 10 total)
  • danriverdigital
    #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 Moderator
    #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.

    danriverdigital
    #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 Moderator
    #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 );
    danriverdigital
    #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 Moderator
    #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 );
    danriverdigital
    #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 Moderator
    #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

    danriverdigital
    #184385

    Hi Ross,

    Thank you for this, it worked straight away.

    Dan

Viewing 10 posts - 1 through 10 (of 10 total)

The topic ‘Exclude current post from results’ is closed to new replies.