Articles of posts where

How to use filter hook posts_join for querying taxonomy terms in posts_where?

For the last few hours, I have been trying to use the filter hooks posts_where and posts_join to search for posts based on taxonomy terms, in this case, the taxonomy is ‘category’ and the term is ‘politics’ This is what I tried and resulted in a empty query: This line is inside the hook posts_where […]

When querying a combination of posts and other meta fields, is there a better solution than directly modifying the WHERE value?

This question is a follow up to a question I recently asked. I’ve implemented what seemed to be the only solution, and it seems to work (YAY!). I just need to verify that: This is really the only way to modify the query to match with a match to the (post_title OR post_content OR any […]

How to search for posts IN OR title OR content OR author?

I have looked at many posts and questions regarding searches for custom meta, but I want to search for posts and include those if the search query matches an author name. I have come up with the following code but it is not working. For some reason, it returns posts with status “revision” First, I […]

Custom time range for the posts_where filter

I’m creating a function that displays the most viewed posts and would like to be able to show posts based on how old they are. Modifying the query is not really a problem. But since I can’t pass any arguments to the function I can’t modify the “-30 days” part. function print_stuff($args) { add_filter( ‘posts_where’, […]

Extending WP_Query — Optimise SQL query

I’m storing posts, a user follows in a custom table that has the columns id, post_id and user_id. To fetch posts that a user follows I have extended the WP_Query as follows: class WP_Query_Posts_User_Follows extends WP_Query { function __construct($args=array()) { if ( !empty($args[‘followed_by’]) ) { $this->followed_by = $args[‘followed_by’]; add_filter(‘posts_where’, array($this, ‘posts_where’)); } parent::query($args); } function […]

Custom query incorrectly returning everything

I’m missing something somewhere, but I cannot connect the dots. Everything outputs properly except that the query returns everything from the DB, not just posts. Can anyone spot a reason for this behavior? Having messed around a bit, I know it’s related to add_filter, removing it allows the query to function normally. However, I still […]

Search ONLY by meta key / meta values

I’m almost there on this one. On functions.php i have this: function base_home_product_post_type( $query ) { if($query->is_search() && $_POST[‘box’] == ‘sku’) { $query->query_vars[‘post_type’] = ‘product’; $query->query_vars[‘meta_key’] = ‘sku’; $query->query_vars[‘meta_value’] = $query->query_vars[‘s’]; return; } } add_action(‘pre_get_posts’, ‘base_home_product_post_type’, 1); And it does what it’s told. Searches for the search string on the meta_key ‘sku’. Problem here is […]

Prevent pre_get_posts filter on specific post type

I am running into one issue with pre_get_post filter. Actually I have 4 post types. Post, Pages, Events, and Venue. I created one custom field called city for event and venue post type. When creating custom field I have one option to select post type on which I want to display that custom field. Now, […]

Is there any difference between hooks posts_where with posts_join and posts_search performance wise?

I want to know which filter hook I should use that performs faster. I did try both several times and I noticed that posts_search is a bit faster than using the combination of posts_where and posts_join What is your take on this?

How to modify posts_where filter only for the search query

I’m using posts_where filter in order to modify the user searches on a web, but i find out that some default widgets like the “more recent posts” uses this filter too and their behaviour are also modified. I am trying to find a way to avoid that anything other than the users searches use the […]