Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

WordPress hide some posts on the site

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;

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

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');
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading