Home > other >  Wordpress hide some posts on the site
Wordpress hide some posts on the site

Time:10-13

I have a wordpress site. I want to hide some posts on this site everywhere on the site (Homepage, search page, archive, etc.).

I found a code like this;

    function exclude_from_everywhere($query) {
   if ( $query->is_home() || $query->is_feed() ||  $query->is_search() || 
$query->is_archive() ) {
     $query->set('post__not_in', array(992, 1968, 173));
   }
}
add_action('pre_get_posts', 'exclude_from_everywhere');

This code hides the text according to the ID number. However, what I want is to hide according to the custom field section on the site. The code below is the custom field code;

get_post_meta($post->ID, 'Mood', true);

If something is written in a custom field called Mood, what should I do to hide these topics? In other words, if the custom field is full, the posts should be hidden, but if nothing is written in the custom field, the posts should not be hidden.

Thank you very much in advance to those who help.

CodePudding user response:

Use $meta_query

function exclude_from_everywhere($query) {
   if ( $query->is_home() || $query->is_feed() ||  $query->is_search() || $query->is_archive() ) {
    $meta_query = array(
               array(
                  'key'=>'Mood',
                  'value'=>true,
                  'compare'=>'=',
               ),
    );
    $query->set('meta_query',$meta_query);
}
}
add_action('pre_get_posts', 'exclude_from_everywhere');
  • Related