Forums › Forums › Search & Filter Pro › Exclude current post from results
- This topic has 9 replies, 3 voices, and was last updated 6 years, 3 months ago by Anonymous.
-
Anonymous(Private) July 27, 2018 at 3:09 pm #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(Private) July 27, 2018 at 3:45 pm #183720This 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(Private) July 30, 2018 at 1:27 pm #183892Hi,
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(Private) July 30, 2018 at 3:04 pm #183922Hi
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 );
Trevor(Private) July 30, 2018 at 5:35 pm #183979Not 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 );
Ross Moderator(Private) August 1, 2018 at 8:48 pm #184347Hi 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
-
AuthorPosts