Exclude Posts programmatically
-
Hello,
I need to exclude posts which has a custom field value 1,function exclude_discontinued_search($query) { //only run for the main query and don't run on admin pages if (!is_admin() && $query->is_main_query()) { //now check to see if you are on a search results page if ($query->is_search) { //get sponsor posts that are NOT advertorials, so we can exclude their IDs from search $args = array( //get posts of the custom post type sponsor_post 'post_type' => 'product', //get all posts 'posts_per_page' => -1, //return an array of post IDs 'fields' => 'ids', //now check for posts that have a sponsor_post_type that is not 'advertorial' 'meta_query' => array( 'relation' => 'OR', array( 'key' => 'discontinued', 'value' => '1', 'compare' => '!=' ), ) ); //now get the posts $excluded_ids = get_posts($args); //add these post IDs to the 'post__not_in' query parameter $query->set('post__not_in', $excluded_ids); } } } add_action('pre_get_posts', 'exclude_discontinued_search');
But its not working with me,
But When i export the var_dump($excluded_ids) i can see the IDS correctly.`am I doing anything wrong not compatible with Relevanssi ?
I noticed in there is an option called relevanssi_exclude_posts.
there is any hook we can use it to add the list of ID into this ?Thank you
Viewing 3 replies - 1 through 3 (of 3 total)
Viewing 3 replies - 1 through 3 (of 3 total)
- The topic ‘Exclude Posts programmatically’ is closed to new replies.