PHP 26
WP pre_get_posts meta_query By micha on 15th October 2021 12:07:13 PM
  1. // Fires after the query variable object is created, but before the actual query is run. https://developer.wordpress.org/reference/hooks/pre_get_posts/
  2.  
  3. /*
  4.  * Exclude Posts if option is set to `hide`
  5.  */
  6. function modify_default_queries($query) {
  7. if ( !is_admin() ) {
  8.         if ( $query->is_home() || $query->is_archive() ) {
  9.                 $meta_query = [
  10.                 'relation'     => 'OR',
  11.  
  12.                 array(
  13.                 'key'        => 'hide_post-in-view-cats',
  14.                 'value'      => true,
  15.                 'compare'    => '!=',
  16.                 ),
  17.                 array(
  18.                 'key'        => 'hide_post-in-view-cats',
  19.                 'compare'    => 'NOT EXISTS'
  20.                 )
  21.  
  22.                 ];
  23.                 $query->set( 'meta_query', $meta_query );
  24.         }
  25.         return $query;
  26. }
  27. }
  28. add_action( 'pre_get_posts', 'modify_default_queries' );

Paste is for source code and general debugging text.

Login or Register to edit, delete and keep track of your pastes and more.

Raw Paste

Login or Register to edit or fork this paste. It's free.