Wordpress ACF field not working in WP_Query - wordpress

Why does this return all posts;
$partner = get_query_var('partner');
echo "Partner ID: ". $partner; // Echoes correctly
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'meta_query' => array(
'meta_key' => 'partner',
'meta_value' => array($partner), // also $partner (without array)
'compare' => 'LIKE', // Also 'IN'
),
);
$partner_query = new WP_Query($args);
the ACF field 'partner' is an array, the query variable is a string (obv)

The correct usage of meta_query is like this:
The args shouldn't be meta_key if you're using meta_query - they should be key, value, etc.
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'partner',
'value' => array( $partner ),
'compare' => 'IN',
),
),
);

Try like this:
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'meta_query' => array(
'meta_key' => 'partner',
'meta_value' => array($partner), // also $partner (without array)
'compare' => '=', // Also 'IN'
),
);
$partner_query = new WP_Query($args);

For anyone who gets stuck in this rabbit hole.
My query parameter is a string, ACF stores the IDs as a string if there's one and an array if there's more than one.
This form of the arguments returns the correct results. (many thanks to Howard E and IronCanTaco)
$partner = get_query_var('partner'); // string
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'partner', // Could be string or array
'value' => $partner,
'compare' => 'LIKE',
),
),
);

Related

ACF - don't show passed events and filter with start date

I have a list of events and I'd like to do 2 things :
order posts according to starting date
don't include passed events in the query
I did some research and builded a query but it doesn't work.
'''
$current_date = date_i18n('d.m.y');
$the_query = new WP_Query( array(
'post_type' => 'spectacles',
'meta_query' => array(
array(
'key' => 'header_spec_period_start',
'type' => 'DATE'
),
array(
'key' => 'header_spec_period_end',
'value' => '$current_date',
'compare' => '>',
'type' => 'DATE'
),
),
'order' => 'ASC' ) );
?>
'''
All the events are showing, I can't get the passed events filtered.
$date_now = date('Ymd');
$args = array(
'post_type' => 'spectacles',
'post_status' => 'publish',
'meta_key' => 'header_spec_period_start',
'order' => 'ASC',
'orderby' => 'meta_value',
'meta_query' => array(
array(
'key' => 'header_spec_period_start',
'compare' => '>',
'value' => $date_now,
),
),
);
$the_query = new WP_Query( $args );
You can try this ...

WP Query: How to get posts from a specific author OR those with a specific meta value

I would like to retrieve all those posts whose author (post table field) is a given one OR those which has a given meta value (postmeda table field).
If "author" was a meta value, I know I could use a meta_query to achieve it. The thing here is that it is not... so I think I cannot use the "author" field within a meta_query and use the "relation" key.
I'm looking for something like:
$args = array(
'post_type' => array('post'),
'orderby' => 'ASC',
'order' => 'date',
'meta_query' => array(
'relation' => 'OR',
array(
'relation' => 'AND',
array(
'field' => 'author',
'value' => $author_id,
'compare' => '==',
),
array(
'key' => '_meta_field_name',
'compare' => 'NOT EXISTS',
),
),
array(
'relation' => 'AND',
array(
'key' => '_meta_field_name',
'compare' => 'EXISTS',
),
array(
'key' => '_meta_field_name',
'value' => $meta_field_value,
'compare' => '==',
),
),
),
);
$data = new WP_Query( $args );
Any suggestion on how to achieve that using WP_Query?
Thanks!
You might like to try an approach like this one instead. The idea is to query the two conditions you want to search for, then merge the two queries into one finished product.
//Get posts with the author you're looking for
$args1 = array(
'author_name' => 'testuser', //or 'author' => $author_id or something else
);
$data1 = get_posts( $args1 );
//Get posts with the meta data you're looking for
$args2 = array(
'meta_query' => array(
array(
'key' => 'meta_field_name',
'compare' => 'EXISTS',
),
array(
'key' => 'meta_field_name',
'value' => $meta_field_value,
'compare' => '==',
),
),
);
$data2 = get_posts( $args2 );
//Merge both arrays
$allData = array_merge( $data1, $data2 );
//Get just the IDs of all the posts found, while also dropping any duplicates
$postIDs = array_unique( wp_list_pluck( $allData, 'ID' ) );
//Do a new query with these IDs to get a properly sorted array of post objects
$args3 = array(
'post__in' => $postIDs,
'order' => 'ASC',
'orderby' => 'date',
);
$finalAnswer = get_posts( $args3 ); //This is your array of post objects. Ta-Da!

Search serialized postmeta value from array

This is the first time am writing anything as such. I have an array that has to search serialized value in the database and return its matching results. Below is my current code that am still working on.
//This is my array
$country = array(128, 191, 158);
//This is my args passed to
$args = array(
'post_parent' => 85,
'posts_per_page' => 10,
'post_type' => 'page',
'orderby' => 'menu_order',
'order' => 'desc',
'post_status' => 'publish',
's' => $keyword? $keyword: NULL,
'meta_query' => array(
'relation' => 'AND',
$country? array('key' => 'country', 'value' => $country, 'compare' => 'IN'): NULL,
$related? array('key' => 'related', 'value' => $related, 'compare' => '='): NULL
)
);
query_posts( $args );
//Example of postmeta value in the database
$meta = 'a:6:{i:0;s:2:"20";i:1;s:2:"25";i:2;s:2:"128";i:3;s:2:"191";i:4;s:2:"23";i:5;s:2:"24";}';
Any help or directing me to the right resource will be appreciated.

How to use custom fields in wp_query for ordering and filtering the result at the same time?

I want to query the posts that have been set as "featured" and order them by their "priority" field at the same time.
featured field is a true/false type and priority field is a number, and they are created by ACF plugin.
here is my code, but it's not working...
$args = array(
'post_type' => 'tour',
'posts_per_page' => 8,
'orderby' => 'meta_value_num date',
'meta_key' => 'priority',
'meta_query' => array(
array(
'key' => 'featured_tour',
'value' => true,
'compare' => '=',
),
),
);
$query = new WP_Query( $args );
try this :
$args = array(
'post_type' => 'tour',
'posts_per_page' => 8,
'orderby' => 'meta_value_num',
'order'=>'DESC',
'meta_key' => 'priority',
'meta_query' => array(
relation=>'AND',
array(
'key' => 'featured_tour',
'value' => true,
'compare' => '=',
),
array(
'key' => 'priority',
'value' => array(1,6), //YOUR VALUES
'compare' => 'BETWEEN',
'type' => 'NUMERIC'
),
),
);
$query = new WP_Query( $args );

wp_query with a variable quantity of arguments

I want to build a dynamic set of arguments for a wp_query but have had real problems doing this. Take the following example code (which does work)...
$args = array(
'numberposts' => -1,
'posts_per_page' => -1,
'post_type' => 'skills',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'years',
'value' => 'Primary',
'compare' => 'LIKE'
),
array(
'key' => 'years',
'value' => 'Secondary',
'compare' => 'LIKE'
)
)
);
// get results
$the_query = new WP_Query( $args );
The idea is that depending on the results of user input in a form, the number of clauses in my meta_query value would alter. In the code above there are two options, but maybe depending on input there are 3 or another time 5.
I tried compiling these inner array elements externally of $args. Assume in following code that $inputArray is a single dimensional array of string elements. The test looked like:
$inputArray=array();
if (is_array($yearsArray)){
foreach( $yearsArray as $year ) {
$inputArray[]=array('key' => 'years','value' => $year,'compare' => 'LIKE');
}
}
// args
$args = array(
'numberposts' => -1,
'posts_per_page' => -1,
'post_type' => 'skills',
'meta_query' => array(
'relation' => 'OR',
$inputArray
)
);
But the query just runs as if no meta_query queries had been applied.
I also tried using the function wp_parse_args to try and merge multiple meta_queries, however it seems that I'm overwriting the value and only the last one is ever used. This looked like this:
// args
$args = array(
'numberposts' => -1,
'posts_per_page' => -1,
'post_type' => 'skills'
);
$args2 = array(
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'years',
'value' => 'Secondary',
'compare' => 'LIKE'
)
)
);
$args3 = array(
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'years',
'value' => 'Primary',
'compare' => 'LIKE'
)
)
);
$args=wp_parse_args($args,$args2,$args3);
So as you can see I've tried a few different methods but none are working. Can anyone assist?
I fixed this by discovering that you can use IN as a compare clause. $yearsArray was just a single dimensional array of string values.
$args = array(
'numberposts' => -1,
'posts_per_page' => -1,
'post_type' => 'skills',
'meta_query' => array(
array(
'key' => 'years',
'value' => $yearsArray,
'compare' => 'IN'
)
)
);

Resources