How to use MySQL’s MATCH AGAINST in WP_Query?

As far as I know, you can’t use MATCH()… AGAINST in WP_Query. The possible values for comparison are the following: = != > >= < <= LIKE NOT LIKE IN NOT IN BETWEEN NOT BETWEEN NOT EXISTS REGEXP NOT REGEXP RLIKE Which the default is =. However, you can use MATCH() AGAINST in a generic … Read more

get current custom post ID by WP_Query method

Since the AJAX can be called from anywhere, even by a search engine, you should pass the ID manually. To do this, you need to include the ID of your current page somewhere in your template. There is a common practice to include extra information in hidden input elements. You can add this hidden element … Read more

Orderby ASC changes to DESC in WP_Query

You’ve made “orderby” and “order” part of the date_query sub-array. “Order” parameters belong to the main parameters array. I can’t vouch for the part of your code that concerns the year and the above-undefined variable $ppy, but if you want the posts from a specified year in ascending order by ‘post_date’ (which is the default), … Read more

How do I force WP_query to fetch fresh, non-cached results?

WP_Query has a Caching Parameters you can configure to return the desired data with/without caching. $swp_args = array( // Show posts without adding post meta information to the cache. ‘update_post_meta_cache’ => false, … ); In your specific case update_post_meta_cache allows you to turn on/off the caching for post meta.

Display post from current category and same tag?

Tags are a taxonomy called post_tag. You can use them in get_posts() via the tax_query. Since wp_get_post_tags() returns an array of objects, you need to clean it up a bit since only one field per object is required for the query. $tag_objects = wp_get_post_tags($post->ID); $tags = array(); foreach ($tag_objects as $tag_object) { $tags[] = $tag_object->term_id; … Read more

Make (post) query a clickable link

You can add a query string to your URL and then alter the query if that string is set. What I’m going to do is to use pre_get_posts to modify the main query. function sort_by_views($query) { if ( !is_admin() && $query->is_main_query() && $query->is_home() && isset( $_REQUEST[‘sort_by_views’] ) ) { $query->set( ‘post_type’, ‘download’); $query->set( ‘post_status’, ‘publish’ … Read more

How to change main query based on post meta

Finally got it. This code in functions.php: function set_order_by_last_date($query){ $query->set( ‘orderby’, ‘eventstart’); $query->set( ‘order’, ‘DESC’ ); $query->get_posts(); if ($query->have_posts()){ if ( eo_get_the_end(‘Ymd’,$query->posts[0]->ID,$query->posts[0]->occurrence_id) > date(‘Ymd’) ){ $query->set( ‘event_end_after’, ‘today’ ); $query->set( ‘orderby’, ‘eventstart’); $query->set( ‘order’, ‘ASC’ ); } else { $query->set( ‘event_end_before’, ‘today’ ); $query->set( ‘orderby’, ‘eventstart’); $query->set( ‘order’, ‘DESC’ ); } } return $query->query_vars; } … Read more