Exclude some posts from WP_Query All over
Add code in functions.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
function exclude_user_access( $query ) { if ( ! is_admin() ) { $user = wp_get_current_user(); if ( in_array( 'subscriber', (array) $user->roles ) || !is_user_logged_in() ) { if($query->query_vars['post_type'] == "model"){ $meta = $query->get( 'meta_query'); if(!empty($meta)){ $newQuery = array( 'relation' => 'OR', // Optional, defaults to "AND" array( 'key' => 'user_access', 'value' => "no", 'compare' => 'LIKE', ), array( 'key' => 'user_access', 'compare' => 'NOT EXISTS' ) ); $combine = array('relation' => 'AND',$newQuery,$meta); }else{ $combine = array( 'relation' => 'OR', // Optional, defaults to "AND" array( 'key' => 'user_access', 'value' => "no", 'compare' => 'LIKE', // 'type' => 'CHAR' ), array( 'key' => 'user_access', 'compare' => 'NOT EXISTS' ) ); } } } } return $query; } add_action( 'pre_get_posts', 'exclude_user_access' ); |