Meta Queries - should nesting work after WP 4.1? - wordpress

I have a rather huge meta_query here and it hangs at execution: browser just won't load the page, it goes on loading ...
As far as I know this should work after WP 4.1 (I have it). Also tried updating to 4.2 to no avail.
Am I doing something terribly wrong here? Thanks for any info.
$meta_query = array(
'relation' => 'AND',
// IF $level GIVEN
array(
'relation' => 'OR',
array(
'key' => 'co_level',
'value' => $level,
'compare' => 'IN'
),
array(
'relation' => 'AND',
array(
'key' => 'co_program2',
'value' => $program_ids,
'compare' => 'IN'
),
array(
'key' => 'co_level2',
'value' => $level,
'compare' => 'IN'
),
),
array(
'relation' => 'AND',
array(
'key' => 'co_program3',
'value' => $program_ids,
'compare' => 'IN'
),
array(
'key' => 'co_level3',
'value' => $level,
'compare' => 'IN'
),
),
),
// IF $year GIVEN
array(
'relation' => 'OR',
array(
'key' => 'co_year',
'value' => $year,
'compare' => 'IN'
),
array(
'relation' => 'AND',
array(
'key' => 'co_program2',
'value' => $program_ids,
'compare' => 'IN'
),
array(
'key' => 'co_year2',
'value' => $year,
'compare' => 'IN'
),
),
array(
'relation' => 'AND',
array(
'key' => 'co_program3',
'value' => $program_ids,
'compare' => 'IN'
),
array(
'key' => 'co_year3',
'value' => $year,
'compare' => 'IN'
),
),
),
);
$args = array(
'post_type' => 'course',
'posts_per_page' => -1, // get all posts
'orderby' => array('meta_value_num'=>'ASC','menu_order'=>'ASC'), // order by given meta value AND menu_order
'meta_key' => $meta_key_orderby,
'order' => 'ASC',
'meta_query' => $meta_query,
);

Related

Wordpress filter with multiple parameters

I'm trying to set up a wordpress page in which there is a main section with upcoming events and a secondary section in which there are passed events.
I'm using this filter (located in functions.php) in order to switch upcoming and passed events:
// upcoming events
function listing_filter_function($date_args){
$date_args = array(
'post_type' => 'evento',
'meta_key' => 'start_date',
'posts_per_page' => -1,
'orderby' => 'meta_value_num',
'order' => 'ASC',
'meta_query'=> array(
array(
'key' => 'start_date',
'compare' => '>=',
'value' => date("Ymd"),
'type' => 'DATE'
)
),
);
return $date_args; } add_filter('listing_filter', 'listing_filter_function');
// events archive
function archive_filter_function($date_args){
$date_args = array(
'post_type' => 'evento',
'meta_key' => 'start_date',
'posts_per_page' => -1,
'orderby' => 'meta_value_num',
'order' => 'DESC',
'meta_query'=> array(
array(
'key' => 'start_date',
'compare' => '<',
'value' => date("Ymd"),
'type' => 'DATE'
)
),
);
return $date_args; } add_filter('archive_filter', 'archive_filter_function');
In this filter the events are sorted using the start date, but I would also need to check the end date to prevent events lasting more than one day from being show in the archive section after the first day
Thanks in advance!
you can use the meta_query relation for this like this example
'meta_query'=> array(
'relation' => 'AND',
array(
'key' => 'start_date',
'compare' => '>='
'value' => date("Ymd"),
'type' => 'DATE'
),
array(
'key' => 'end_date',
'compare' => '<'
'value' => date("Ymd"),
'type' => 'DATE'
),
)
for the archive you can check if the end_date is smaller then today or equal or less (<=)
Edit to answer the Question in the comment, this was asked after marking the answer correct. Please note I'm not 100% on this as I'm readying mixed results about this method so if it works please let us know so I can update this to reflect the results
'meta_query' => array(
'relation' => 'OR',
array(
'relation' => 'AND',
array(
'key' => 'start_date',
'compare' => '>'
'value' => date("Ymd"),
'type' => 'DATE'
),
array(
'key' => 'end_date',
'compare' => '<'
'value' => date("Ymd"),
'type' => 'DATE'
),
),
array(
'relation' => 'AND',
array(
'key' => 'start_date',
'compare' => '>'
'value' => date("Ymd"),
'type' => 'DATE'
),
array(
'key' => 'end_date',
'value' => '1',
'compare' => 'NOT EXISTS'
),
),
),
it finally works!I've made some changes:
'meta_query'=> array('relation' => 'OR',
array('relation' => 'AND',
array(
'key' => 'start_date',
'compare' => '<',
'value' => date("Ymd"),
'type' => 'DATE'
),
array(
'key' => 'end_date',
'value' => '',
'compare' => '='
),
),
array('relation' => 'AND',
array(
'key' => 'start_date',
'compare' => '<',
'value' => date("Ymd"),
'type' => 'DATE'
),
array(
'key' => 'end_date',
'compare' => '<',
'value' => date("Ymd"),
'type' => 'DATE'
),
),
),

WP several meta_query matches, and double counted

I set following meta_query.
I like to add one more condition.
My problem is that the article which matches both meta_query conditions are double counted by count(get_posts on the end of line.
Is it possible to add condition like this? If both 1bb and 1aa matches, count it as if only 1bb matches.
<?php
$args = array(
'category_name' => $cat,
'posts_per_page' => -1,
'post_type' => 'post',
'post_status' => 'publish',
'meta_query' => array(
'relation' => 'or',
array(
'key'=> '1bb',
'value' => array($from , $to),
'compare' => 'BETWEEN',
'type' => 'DATE',
),
array(
'key'=> '1aa',
'value' => array($from , $to),
'compare' => 'BETWEEN',
'type' => 'DATE',
),
),
);
echo count( get_posts( $args ) );
?>
Thank you.
The following is additional code that I tried to prevent from double counting.
Its too long time to load, and can not use it.
'meta_query' => array(
'relation' => 'or',
array(
'relation' => 'AND',
array(
array(
'key'=> '1aa',
'value' => array($from , $to),
'compare' => 'BETWEEN',
'inclusive' => 'true',
'type' => 'DATE',
),
array(
'key' => '1bb',
'compare' => 'NOT EXISTS',
),
),
),
array(
'relation' => 'AND',
array(
array(
'key'=> '1bb',
'value' => array($from , $to),
'compare' => 'BETWEEN',
'inclusive' => 'true',
'type' => 'DATE',
),
array(
'key' => '1aa',
'compare' => 'NOT EXISTS',
),
),
),
array(
'relation' => 'AND',
array(
array(
'key'=> '1bb',
'value' => array($from , $to),
'compare' => 'BETWEEN',
'inclusive' => 'true',
'type' => 'DATE',
),
array(
'key' => '1aa',
'compare' => 'EXISTS',
),
array(
'key' => '1bb',
'compare' => 'EXISTS',
),
),
),
),

Wp_Query meta_query not working

I'm trying to do the equivalent of this in meta_query but so far no success.
WHERE tm_url_1="" AND tm_url_2="" AND (tm_url_3="" OR tm_embed_code=!="") AND (tm_url_3!="" OR tm_embed_code=="")
Here the WP_Query. Once i add the OR clause nothing is returned.
$args = array(
'post_status' => $post_status,
'posts_per_page' => $posts_count,
'ignore_sticky_posts' => 1,
'post_type' => $post_type,
'order_by' => $order_by,
'order' => $order,
"meta_query" => array(
array('relation' => 'AND',
array(
'key' => 'tm_url_1',
'value' => '',
'compare' => '='
),
array(
'key' => 'tm_url_2',
'value' => '',
'compare' => '='
),
array(
'relation' => 'OR',
array(
'key' => 'tm_url_3',
'value' => '',
'compare' => '='
),
array(
'key' => 'tm_embed_code',
'value' => '',
'compare' => '!='
)
),
array(
'relation' => 'OR',
array(
'key' => 'tm_url_3',
'value' => '',
'compare' => '!='
),
array(
'key' => 'tm_embed_code',
'value' => '',
'compare' => '='
)
)
)
)
);
Nothing is returned. Where did i go wrong in the meta_query?
you should remove the first "array" in the media_query parameter:
"meta_query" =>
array(
'relation' => 'AND',
array(
'key' => 'tm_url_1',
'value' => '',
'compare' => '='
),
array(
'key' => 'tm_url_2',
'value' => '',
'compare' => '='
),
array(
'relation' => 'OR',
array(
'key' => 'tm_url_3',
'value' => '',
'compare' => '='
),
array(
'key' => 'tm_embed_code',
'value' => '',
'compare' => '!='
)
),
array(
'relation' => 'OR',
array(
'key' => 'tm_url_3',
'value' => '',
'compare' => '!='
),
array(
'key' => 'tm_embed_code',
'value' => '',
'compare' => '='
)
)
)
see: https://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters
Here is the correct way to add the meta_query for the conditions you want.
$args = array(
'post_status' => $post_status,
'posts_per_page' => $posts_count,
'ignore_sticky_posts' => 1,
'post_type' => $post_type,
'order_by' => $order_by,
'order' => $order,
'meta_query' => array(
array(
'key' => 'tm_url_1',
'value' => '',
) ,
array(
'key' => 'tm_url_2',
'value' => '',
) ,
array(
'relation' => 'OR',
array(
'key' => 'tm_url_3',
'value' => '',
) ,
array(
'key' => 'tm_embed_code',
'value' => '',
'compare' => '!='
)
) ,
array(
'relation' => 'OR',
array(
'key' => 'tm_url_3',
'value' => '',
'compare' => '!='
) ,
array(
'key' => 'tm_embed_code',
'value' => '',
)
)
)
);
Note that I have removed the default values for compare in some arrays assuming it is = as in your condition. The reference for this can be seen here.

Wordpress Query, check a value against multiple keys

My Wordpress setup has a Publication custom post type and has 10 custom fields each corresponding to an author who may have contributed to that publication. Every post can have different authors in those fields or the same authors but in a different order. I am using Wordpress's WP_Query to search for posts matching my criteria. I need to check a single author name against all the custom fields to see if it matches any one of them. I tried doing this with the below code but it produces no result. Any help would be appreciated!
$args = array(
'post_type' => 'publication',
'post_status' => 'publish',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'year_published',
'value' => $selected_pub_year,
'compare' => '=',
),
array(
'key' => array(
'author_0_name',
'author_1_name',
'author_2_name',
'author_3_name',
'author_4_name',
'author_5_name',
'author_6_name',
'author_7_name',
'author_8_name',
'author_9_name',
),
'value' => $selected_pub_author,
'compare' => '=',
),
),
);
// The above meta-query may be modified through the use of this:
// echo "<pre>".print_r($args[meta_query][relation])."</pre>";
// The Query
$the_query = new WP_Query($args);
I have added OR relation for post author query,Try to change your argument array as follow,
$args = array(
'post_type' => 'publication',
'post_status' => 'publish',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'year_published',
'value' => $selected_pub_year,
'compare' => '=',
),
array(
'relation' => 'OR',
array(
'key' => 'author_0_name',
'value' => $selected_pub_author,
'compare' => '=',
),
array(
'key' => 'author_1_name',
'value' => $selected_pub_author,
'compare' => '=',
),
array(
'key' => 'author_2_name',
'value' => $selected_pub_author,
'compare' => '=',
),
array(
'key' => 'author_3_name',
'value' => $selected_pub_author,
'compare' => '=',
),
array(
'key' => 'author_4_name',
'value' => $selected_pub_author,
'compare' => '=',
),
array(
'key' => 'author_5_name',
'value' => $selected_pub_author,
'compare' => '=',
),
array(
'key' => 'author_6_name',
'value' => $selected_pub_author,
'compare' => '=',
),
array(
'key' => 'author_7_name',
'value' => $selected_pub_author,
'compare' => '=',
),
array(
'key' => 'author_8_name',
'value' => $selected_pub_author,
'compare' => '=',
),
array(
'key' => 'author_9_name',
'value' => $selected_pub_author,
'compare' => '=',
),
),
),
);
$the_query = new WP_Query( $args );

Query with meta key is serialized array

I've made a custom query containing a meta query but some fields are serialized arrays. Is there a way to get the value from that array? Everything I've tried so far won't work.
$query_args = array(
'post_type' => 'yacht',
'_meta_or_tax' => true,
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'manufacturers',
'field' => 'id',
'terms' => $_GET['manufacturer']
),
array(
'taxonomy' => 'category',
'field' => 'id',
'terms' => $_GET['cat'],
)
),
'posts_per_page' => -1,
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'yachts_length',
'value' => array($_GET['min_length'], $_GET['max_length']),
'compare' => 'BETWEEN',
'type' => 'NUMERIC',
),
array(
'key' => 'yachts_price',
'value' => array($_GET['min_price'], $_GET['max_price']),
'compare' => 'BETWEEN',
'type' => 'NUMERIC',
),
array(
'key' => 'yachts_year',
'value' => array($_GET['min_year'], $_GET['max_year']),
'compare' => 'BETWEEN',
'type' => 'NUMERIC',
),
array(
'key' => 'yachts_fuel',
'value' => $_GET['fuel'],
'compare' => 'LIKE',
),
array(
'key' => 'yachts_cabins',
'value' => $_GET['cabins'],
'compare' => 'LIKE',
'type' => 'NUMERIC',
),
array(
'key' => 'yachts_engine_type',
'value' => $_GET['engine_type'],
'compare' => 'LIKE',
),
array(
'key' => 'yachts_en',
'value' => $_GET['engines'],
'compare' => 'LIKE',
'type' => 'NUMERIC',
),
array(
'key' => 'yachts_drive',
'value' => $_GET['drive_type'],
'compare' => 'LIKE',
),
)
);
$yacht_query = new WP_Query( $query_args );
yachts_cabins, yachts_fuel, yachts_engine_type, yachts_en, yachts_type are serialized arrays.

Resources