Use posts_where to exclude posts ids from wp_query

Instead of using posts_where it is a better idea to use pre_get_posts filter. Here is the code I end up implementing: add_filter( ‘pre_get_posts’, ‘hide_unwanted_posts_filter’ ); function hide_unwanted_posts_filter( $query ) { global $current_user; get_currentuserinfo(); $user_id = $current_user->ID; $key = ‘unwanted_posts’; $unwanted_posts = get_user_meta($user_id,$key,true); if(is_user_logged_in() && !empty($unwanted_posts) && !$query->is_admin) { $query->set(‘post__not_in’, $unwanted_posts ); // id of page … Read more

How to exclude posts for current user

I’m not entirely sure what the problem is, because you’ve already mentioned all the tools that you need to solve it… Just use pre_get_posts filter, check if the user is logged in, get the IDs of posts he should not see and exclude them in query: function remove_some_posts_for_user( $query ) { if ( ! is_admin() … Read more

apply custom where to the default $wp_query

The posts_where filter should still fire. Just add it like this: function homepage_load_posts($query) { if ($query->is_home()) { add_filter( ‘posts_where’, ‘filter_where’ ); // And make the filter self-removing… // Create a new filtering function that will add our where clause to the query function filter_where( $where=”” ) { remove_filter( ‘posts_where’, ‘filter_where’ ); // posts 30 to … Read more

Get Posts updated or published within the last x hours

here is a shorter version that assumes the GET parameter ?time=[integer] (for example ?time=8) add_filter(‘posts_where’, ‘filter_where’, 11); function filter_where($where=””) { if(isset($_GET[‘time’])){ $time = $_GET[‘time’]; $time=(int)$time; // only allow integers if(in_array($time,array(8,24,72))){ $where .= ” AND post_date > ‘”.date(‘Y-m-d H:i:s’, strtotime(‘-“.$time.” hours’)).”‘”; } } return $where; }

Wp query show post by author name or post title or post content

I hope this would do, i have not tested it. Test it on your own risk, but the query related to it very closely. function kv_get_custom_search_query($search_text){ global $wpdb; $wpdb->get_results( “SELECT * FROM $wpdb->posts WHERE post_title=”%$search_text%” OR post_content=”%$search_text%” OR post_title=”%$search_text%” LEFT JOIN $wpdb->users ON $wpdb->posts.post_author=$wpdb->users.ID AND display_name=”%$search_text%””); }

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)