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%””); }

How do I hide posts across all loops based on the value of a custom field?

This should do it, using pre_get_posts: function wpse_275546_hide_distribution_post( $query ) { /* Don’t filter posts in admin, or the current post when viewing a single post. */ if ( ! is_admin() && ! ( $query->is_main_query() && is_single() ) ) { /** * Get current meta query if it exists, otherwise an empty array. * This … Read more

what are the checksums surrounding keywords in the SQL generated by WP_Query and do I need to use them too?

It is literaly a place holder and not a checksum and it indicates what part of the query should be converted to % before executing the query. (this is done to harden security) I do not see right now an easy way to fix the preg_replace that the codex suggests as I suspect this place … Read more

Compare post-IDs within WP_Query? (Less than / Greater than)

I ended up using something like this: function filter_where( $where=”” ) { global $post, $prev_next; $the_ID = $post->ID; $where .= ” AND wp_posts.ID “; $where .= $prev_next==’next’ ? “< $the_ID” : “> $the_ID”; return $where; } add_filter( ‘posts_where’, ‘filter_where’ ); $nextProjQuery = new WP_Query( $filtered_query_args ); remove_filter( ‘posts_where’, ‘filter_where’ );