Wordpress Query, check a value against multiple keys - wordpress

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 );

Related

Sort WP_Query using multiple meta_keys not working

I have a custom post type, that has price in 2 different meta keys, based on product type. I want to filter the query so that all products can be displayed in either asc or desc order. Here is my query and it is not sorting the results.
$args = array(
'post_type' => 'ars-products',
'post_status' => 'publish',
'meta_query' => array(
'relation' => 'AND',
array(
'relation' => 'OR',
array(
'key' => 'ars_product_price',
'compare' => 'EXISTS',
),
array(
'key' => 'ars_product_min_amount',
'compare' => 'EXISTS',
)
),
array(
'relation' => 'OR',
array(
'key' => 'ars_product_type',
'value' => 'one_time',
'compare' => '=',
),
array(
'key' => 'ars_product_type',
'value' => 'donation',
'compare' => '=',
),
),
),
'orderby' => array(
'ars_product_price' => 'ASC',
'ars_product_min_amount' => 'ASC'
)
);
$products_data = new \WP_Query( $filters );
Edit: it does sort perfectly, if I only use one meta key ars_product_price.

get post base on latest date without comparision

I have custom post type having meta value include last update date.
I have to get post id in which meta value having latest date without compare other date.
below is my wp query to get post id but it is not working.
$args = array(
'posts_per_page' => 1,
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'post_status',
'compare' => '=',
'value' => 'active'
),
array(
'key' => 'other_cpt_id',
'compare' => '=',
'value' => $cptID
),
array(
'key' => 'post_last_update_date',
'value' => '',
'compare' => '>',
'type' => 'DATE'
)
),
'orderby' => array( 'meta_value_num' => 'ASC','ID' => 'ASC' ),
'post_type' => 'custom_post_type',
'post_status' => 'publish'
);
If you are using WP 4.2+, you may try the following:
$args = array(
'posts_per_page' => 1,
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'post_status',
'compare' => '=',
'value' => 'active'
),
array(
'key' => 'other_cpt_id',
'compare' => '=',
'value' => $cptID
),
array(
'key' => 'post_last_update_date',
'value' => '',
'compare' => '>',
'type' => 'DATE'
)
),
'orderby' => array( 'post_last_update_date' => 'ASC', 'ID' => 'ASC' ),
'post_type' => 'custom_post_type',
'post_status' => 'publish'
);
Some sources:
General WP_Query: https://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters
4.0+ orderby array: https://make.wordpress.org/core/2014/08/29/a-more-powerful-order-by-in-wordpress-4-0/
4.2+ orderby meta data: https://make.wordpress.org/core/2015/03/30/query-improvements-in-wp-4-2-orderby-and-meta_query/

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.

Meta Queries - should nesting work after WP 4.1?

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,
);

Wordpress meta_query to compare 3 meta value with same meta key

I would like to fetch all post that have meta_value Book, Study, Art. But when I put 3 array on meta_query system doesn't return anything, but if I put online 2 array It's work
// this working
$args = array(
'numberposts' => 10,
'post_type' => 'content',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'media_type',
'value' => 'Book',
'compare' => 'LIKE'
),
array(
'key' => 'media_type',
'value' => 'Study',
'compare' => 'LIKE'
)
)
);
//this now working
$args = array(
'numberposts' => 10,
'post_type' => 'content',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'media_type',
'value' => 'Book',
'compare' => 'LIKE'
),
array(
'key' => 'media_type',
'value' => 'Study',
'compare' => 'LIKE'
),
array(
'key' => 'media_type',
'value' => 'Art',
'compare' => 'LIKE'
)
)
);
It sounds like you are searching for the exact meta values, so did you try:
$args = array(
'posts_per_page' => 10,
'post_type' => 'content',
'meta_query' => array(
array(
'key' => 'media_type',
'value' => array( 'Art', 'Book','Study' ),
'compare' => 'IN'
),
),
);
as your query arguments?
Sidenote: The parameter numberposts works, but posts_per_page is now more commonly used in WP_Query().

Resources