I am writing a meta query for search purpose. I have total 16 keys (fields) from which I need to search the keyword. So far I am retrieving results from 4 keys (keys). But when I am extending code for all the fields I get the error. Kindly point out the mistake and also the right approach to go.
The code that is working fine:
$WorkinFilter_k_s = array(
'meta_query' => array(
'relation' => 'OR',
array(
'relation' => 'OR',
array(
'key' => 'Building_Name',
'value' => '',
'compare' => 'LIKE'
),
array(
'key' => 'Street',
'value' => $keywords1,
'compare' => 'LIKE'
),
),
array(
'relation' => 'OR',
array(
'key' => 'Town',
'value' => $keywords1,
'compare' => 'LIKE'
),
array(
'key' => 'Description',
'value' => $keywords1,
'compare' => 'LIKE'
),
),
)
);
The code when I extend for all 16 fields. It is not working:
$WorkinFilter_k_s = array(
'meta_query' => array(
'relation' => 'OR',
array(
'relation' => 'OR',
array(
'relation' => 'OR',
array(
'relation' => 'OR',
array(
'relation' => 'OR',
array(
'key' => 'Street',
'value' => $keywords1,
'compare' => 'LIKE'),
array(
'key' => 'COMMENTS1',
'value' => $keywords1,
'compare' => 'LIKE')
),
array(
'relation' => 'OR',
array(
'key' => 'COMMENTS3',
'value' => $keywords1,
'compare' => 'LIKE'
),
array(
'key' => 'Town',
'value' => $keywords1,
'compare' => 'LIKE'
)
)
),
array(
'relation' => 'OR',
array(
'key' => 'Building_Name',
'value' => $keywords1,
'compare' => 'LIKE'
)
array(
'key' => 'Description',
'value' => $keywords1,
'compare' => 'LIKE'
)
),
array('relation' => 'OR',
array(
'key' => 'District',
'value' => $keywords1,
'compare' => 'LIKE'),
array(
'key' => 'BulletPoint5',
'value' => $keywords1,
'compare' => 'LIKE')
)
)
),
array(
'relation' => 'OR',
array(
'relation' => 'OR',
array(
'relation' => 'OR',
array(
'relation' => 'OR',
array(
'key' => 'BulletPoint2',
'value' => $keywords1,
'compare' => 'LIKE'),
array(
'key' => 'BulletPoint3',
'value' => $keywords1,
'compare' => 'LIKE')
),
array(
'relation' => 'OR',
array(
'key' => 'BulletPoint1',
'value' => $keywords1,
'compare' => 'LIKE'
),
array(
'key' => 'BulletPoint6',
'value' => $keywords1,
'compare' => 'LIKE'
)
)
),
array(
'relation' => 'OR',
array(
'key' => 'BulletPoint7',
'value' => $keywords1,
'compare' => 'LIKE'
)
array(
'key' => 'BulletPoint8',
'value' => $keywords1,
'compare' => 'LIKE'
)
),
array('relation' => 'OR',
array(
'key' => 'BulletPoint9',
'value' => $keywords1,
'compare' => 'LIKE'),
array(
'key' => '',
'value' => $BulletPoint10,
'compare' => 'LIKE')
)
)
)
)
);
It is likely to still work inefficient but you mostly just have to clean up your code.
$WorkinFilter_k_s = array(
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'Street',
'value' => $keywords1,
'compare' => 'LIKE',
),
array(
'key' => 'COMMENTS1',
'value' => $keywords1,
'compare' => 'LIKE',
),
array(
'key' => 'COMMENTS3',
'value' => $keywords1,
'compare' => 'LIKE',
),
array(
'key' => 'Town',
'value' => $keywords1,
'compare' => 'LIKE',
),
array(
'key' => 'Building_Name',
'value' => $keywords1,
'compare' => 'LIKE',
),
array(
'key' => 'Description',
'value' => $keywords1,
'compare' => 'LIKE',
),
array(
'key' => 'District',
'value' => $keywords1,
'compare' => 'LIKE',
),
array(
'key' => 'BulletPoint5',
'value' => $keywords1,
'compare' => 'LIKE',
),
array(
'key' => 'BulletPoint2',
'value' => $keywords1,
'compare' => 'LIKE',
),
array(
'key' => 'BulletPoint3',
'value' => $keywords1,
'compare' => 'LIKE',
),
array(
'key' => 'BulletPoint1',
'value' => $keywords1,
'compare' => 'LIKE',
),
array(
'key' => 'BulletPoint6',
'value' => $keywords1,
'compare' => 'LIKE',
),
array(
'key' => 'BulletPoint7',
'value' => $keywords1,
'compare' => 'LIKE',
),
array(
'key' => 'BulletPoint8',
'value' => $keywords1,
'compare' => 'LIKE',
),
array(
'key' => 'BulletPoint9',
'value' => $keywords1,
'compare' => 'LIKE',
),
),
);
Related
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'
),
),
),
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',
),
),
),
),
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.
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.
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,
);