Hot answers tagged

2

Use strtotime to compare dates. $start = strtotime('yesterday'); $end = strtotime( '+1 week',$start); $args = array( 'posts_per_page' => 5, 'ignore_sticky_posts' => 1, 'meta_key' => 'sw_post_views_count', 'orderby' => 'meta_value_num', 'order' => 'DESC', 'date_query' => array( 'after' => $end, ...


2

Filters are for modifying data that's passed through them. You need to take the input, modify it, then return it. function exclude_not_allowed_terms( $args, $taxonomies ) { if(!current_user_can('edit_others_posts') && in_array( 'my-taxonomy', $taxonomies ) ) { $args['meta_query'] = array( array( 'key' ...


2

It might be possible to achieve this by using the post clauses filters and rewriting the generated SQL query. You can also run 3 queries, One very lean query to get the desired posts from the date_query One very lean query to get all the posts from the meta_query One final query to get the complete query object. This help specially with pagination if you ...


1

Multiple meta values can be compared with BETWEEN with an array value: 'meta_query' => array( array( 'key' => REALIA_PROPERTY_PREFIX .'attributes_area', 'value' => array( $first_val, $second_val ), 'type' => 'numeric', 'compare' => 'BETWEEN', ), ), You can see this ...


1

There are few issue with your code When using WP_Query in conjuction with the_post() or using get_posts() in conjuction with setup_postdata( $post ), you need to reset the $post global with wp_reset_postdata(), not wp_reset_query(). wp_reset_query() is used in conjuction with query_posts() which you should never ever use You would want to reset posdata ...


1

If you want both posts that have and do not have a meta key, you need an OR relation meta query to select both posts with key and those where it does not exist. $args = array( 'meta_query' => array( 'relation' => 'OR', array( 'key' => '_post_like_count', 'compare' => 'EXISTS' ), array( ...


1

You should really read the documentation on meta_query's Here is what is allowed in a meta_query key (string) - Custom field key. value (string|array) - Custom field value. It can be an array only when compare is 'IN', 'NOT IN', 'BETWEEN', or 'NOT BETWEEN'. You don't have to specify a value when using the 'EXISTS' or 'NOT EXISTS' comparisons in WordPress ...


1

You can get both with an OR meta_query that also checks if the key does not exist: 'meta_query' => array( 'relation' => 'OR', array( 'key' => 'event_date', 'compare' => 'NOT EXISTS', ), array( 'key' => 'event_date', 'value' => date("Ymd", time()), 'compare' => '>', ), ), ...



Only top voted, non community-wiki answers of a minimum length are eligible