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 posts_where($where) { global $wpdb; $table = $wpdb->prefix . 'post_followed_by'; $where .= $wpdb->prepare(" AND ID IN (SELECT post_id FROM $table WHERE user_id = %d)", $this->followed_by); return …
Hi I'm working on a store locator. I have a custom post "clinics" where I save clinic details (address,.., lat, lng). Each time I publish a new clinic I save post_id, lat and lng also in a custom table "lat_lng_post". I'm able to search clinics within a specific distance using filter post_where in this way: add_filter( 'posts_where' , 'location_posts_where' ) function location_posts_where( $where ) { // $lat and $ lng are latitude and longitude of the searched point global $wpdb; …
I have to create a search query which allows me to filter all posts where author name and post titles or post content has the word from search textbox. So if i search wordpress it should list all the posts from authors with that word also post who includes it either on their title or content.
Let's say you have some code which filters posts by using pre_get_posts e.g. add_action( 'pre_get_posts', 'be_exclude_category_from_blog' ); function be_exclude_category_from_blog( $query ) { if( $query->is_main_query() && $query->is_home() ) { $query->set( 'cat', '-4' ); } } I found a nice bit of code which enables you to further filter these posts to the last 90 days: add_action( 'pre_get_posts', 'be_exclude_category_from_blog' ); function be_exclude_category_from_blog( $query ) { if( $query->is_main_query() && $query->is_home() ) { add_filter( 'posts_where', 'filter_date' ); $query->set( 'cat', '-4' ); } } function …
I am using Pods plugin in WordPress. I am new user of this plugin. I have 3 Custom post type. Tasks, Teams and Volunteers. I can fetch Volunteers using this code get_post_meta($_POST[q], 'volunteers', false);. I would like to make relation of Volunteers with Tasks. I need to fetch how many Tasks are assigned to a Volunteer. How can I fetch this ? My database structure is like below.
I have built a posts query that includes a custom table in search using the posts_join and posts_where filters. Now the customer wants me to include yet another custom table in the search, but doing so increases query time from an instant to over 5 seconds. This is what I have done: function my_posts_join( $join ) { global $wpdb; $old_table = "wp_some_custom_table"; $new_table = "wp_another_custom_table"; if ( is_search() ) { $join .=' LEFT JOIN '.$wpdb->postmeta. ' ON '. $wpdb->posts . …
In my wordpress site I am making use of the pre_get_posts filter to customise my site search query. In functions file I have the following below. Now my issue is, using FieldManager I have added a custom meta field called hide_from_search_results which is now included in my wp_postmeta table, with either a 0 or 1 value. Not all posts/pages will have this field set however. I need to add the condition to not show anything that has this set to …
I am pretty new to WordPress here, and I have seen the way to add condition into the query is by adding below inside functions.php: add_filter('posts_where', 'add_where'); function add_where( $where ) { global $wpdb; $post_title = get_query_var( 'post_title', FALSE ); $where .= " AND {$wpdb->prefix}posts.post_title LIKE '%$post_title%'"; return $where; } For the record, I have added 'post_title' in the query vars as well. So when I trigger the search and when I printed the query, it is actually showing the …
I am using posts_join and posts_where filters to include a custom table in a query. this page says I should "use whatever WordPress did to search the post title field to search my custom table fields (rather than trying to duplicate WordPress's rather complex logic)" ...but the code they provide doesn't work. The replace finds nothing and hence nothing is added. $where = preg_replace( "/\(\s*post_title\s+LIKE\s*(\'[^\']+\')\s*\)/", "(post_title LIKE $1) OR (wp_mytable.entry LIKE $1)", $where ); I used $wp_query->request to display the …
How can I hide posts that are in an array of ids in the usermeta for the current user in the main query. I can use post__not_in in meta_query but I do not know which option to use for only a specific user. I think should use posts_where?
I am attempting to exclude password protected posts from any frontend loop - inspired by this post However, I have a custom angularjs admin theme that is activated when a user visits a certain post type and is logged in as an admin (ar-admin-page). This bypasses wp admin. It pulls posts to be manipulated by calling the Json wp api. The problem: With the below in a mu-plugin, I can remove the password protected post from the frontend but it …
I'm trying to dynamically hide posts across my entire site if the value of a custom field called distribution is "1". I've set up a meta box on the post editor screen to set the value of the field, but, especially on older posts, the field will not always be set. Note that I'm doing this in a plugin, so the easy way of directly editing loops and WP_queries in the theme isn't an option for me. It seems like …
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 posts_where filter. This is my code: add_filter( 'posts_where' , 'posts_where_statement' ); function posts_where_statement( $where ) { global $wp_query; global $expp; global $wpdb; $local_db = $wpdb->prefix."posts"; …
I've a custom table and by following the instructions on this page https://codex.wordpress.org/Custom_Queries I was able to modify the WHERE clause. function geotag_search_where( $where ){ if( is_search() ) { $where = preg_replace( "/\(\s*" . $wpdb->posts . ".post_title\s+LIKE\s*(\'[^\']+\')\s*\)/", " CONCAT( posts.post_title, customtable.customfield) LIKE $1 ", $where ); } return $where; } add_filter('posts_where', 'geotag_search_where' ); My only problem is that wordpress keeps the OR (posts.post_content LIKE '%searchterm%') and this slows down my search time. Is there a way to modify the preg_replace() …
I want to use posts_where to exclude an array of posts ids from all queries in my site. The array containing the posts ids depends on the user that is logged in. Can someone please tell me how to use the filter posts_where to exclude a list of posts ids from all queries in my site? Thank you.
Some of my posts (not all) have a price as a meta key/value. Today I use the pre_get_posts action so that my users can search for prices that are between a certain value. This is the code that I'm using today, and it's working. add_action('pre_get_posts', 'my_search_price'); function my_search_price( $query ) { if ($query->get('maxprice') != "" && $query->get('minprice') != "" && $query->is_main_query()) { $maxprice = intval($query->get('maxprice')); $minprice = intval($query->get('minprice')); $meta = array(); $meta[] = array ( 'key' => 'price', 'value' => …
I am writing a plugin that will hide and sort posts based on tags and postmeta. Products tagged "antique" that are out of stock will be hidden by default. So far, I can hide sold products, but when I attempt to hide sold products that are tagged "antique" my code fails. I was hoping that someone could help me find a solution. I think that I am having trouble in the WHERE clause or in the LEFT JOIN statement for …
I recently had to change qTranslate for qTranslate X since the former is not being updated. This broke my menu and after searching I found it was because qTranslate X sets suppress_filters to false. I happen to have posts_join_paged, posts_where and posts_orderby filters in my functions.php and this is breaking my menu. Is there a condition I could use in those filters to avoid affecting any menus? I've tried $query->is_main_query() inside my filters but I guess menu queries are main …
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 that by default, the query object returned from WordPress also contains this and queries post_title and post_content by default: SELECT SQL_CALC_FOUND_ROWS whirl_2_posts.ID FROM whirl_2_posts INNER …
Using the pre_get_posts hook, I'm able to take $query as an argument. But on posts_where and posts_join hook, I receive the respective clauses and not the query. The problem is that the hook is running on every query, not just the main one. Given the following code, the code will be executed once on each query the page runs: add_filter('posts_join', 'my_join'); function my_join($join) { echo 'lala'; return $join; } How can I tell if the query is the main one …