WP_Query meta_query argument - wordpress

I am trying to write a 'complex' WP_Query but it keeps failing. The meta_query argument Im using is:
$query_args['meta_query'] = array(
"relation" => "AND",
array("relation" => "OR",
array(
'key' => '_htp_hide_trending_posts',
'value' => 'on',
'type' => 'CHAR',
'compare' => '!='
),
array(
'key' => '_htp_hide_trending_posts',
'compare' => 'NOT EXISTS'
)
),
array(
'key' => 'views',
'value' => 0,
'type' => 'NUMERIC',
'compare' => '>'
)
);
_htp_hide_trending_posts is a custom meta_value. If I ignore the views part and just use the OR statement, it's ok but adding the ADD relation and it then ignores the OR relation search. I need both as it needs to order by views and that doesn't work if it's not in the meta_query. I'm going round in circles, hoping someone can spot an obvious mistake?

I finally worked out a solution:
$query_args['meta_query'] = array(
array(
'key' => '_htp_hide_trending_posts',
'compare' => 'NOT EXISTS'
)
);
$query_args['meta_key'] = 'views';
$query_args['meta_compare'] = '>';
$query_args['meta_value'] = '0';
$query_args['orderby'] = array('meta_value_num' => 'DESC');
I didn't need the OR query which seemed to do the trick.

Related

Wordpress meta_query of certain days after date stored in custom field

I like to set meta_query for 10 days after date stored in custom field.
I set like this, but it does not work.
Meta key '2a' has value like '2022-02-15'.
<?php
$today = wp_date('Y-m-d');
$args = array(
'meta_query' => array(
'relation' => 'AND',
array(
'key'=> '2a',
'value' => array( date("Y-m-d", strtotime("+10 day", strtotime(get_post_meta($post->ID , '2a' ,true)))), $today ),
'compare' => '<=',
'type' => 'DATE',
),
array(
'key'=> '3a',
'compare' => 'EXISTS'
),
));?>
You're passing two values into the date portion of your query which suggests you need to compare between as opposed to <=.
I tend to find it's better to perform calculations first and then pass the result into the query. Merging it all together as in your example makes it much harder to follow.
While strtotime() will work, there are classes that make handling time easier and more readable.
Example:
// Keeping the code brief for the example but you'll probably want to do some checks here.
$twoADate = new DateTimeImmutable( get_post_meta( $post->ID , '2a' ,true ) );
$tenDaysLater = $twoADate->modify( '+10 days' )->format( 'Y-m-d' );
$today = wp_date( 'Y-m-d' );
$args = [
'meta_query' => [
'relation' => 'AND',
[
'key'=> '2a',
'value' => [$tenDaysLater, $today],
'compare' => 'BETWEEN',
'type' => 'DATE',
],
[
'key' => '3a',
'compare' => 'EXISTS'
],
],
];
$today = wp_date('Y-m-d');
$TenDaysBefore = date("Y-m-d", strtotime("-10 day", strtotime($today)));
'key'=> '2a',
'value' => $TenDaysBefore ,
'compare' => '<=',
'type' => 'DATE',
Works now

How to display only instock products in all of my site? Woocommerce + Wordpress

How to add more values to 'value' => 'noproduzione', ?
If i add 'value' => array('noproduzione','10days'), functions.php does not give me error but on frontend i receive error Warning : trim() expects parameter 1 to be string, array given in
add_action( 'woocommerce_product_query_meta_query', 'custom_product_query_meta_query', 1000 );
function custom_product_query_meta_query( $meta_query ) {
if ( ! is_admin() ) {
$meta_query[] = array(
'key' => '_stock_status',
'value' => 'noproduzione',
'compare' => '!=',
);
}
return $meta_query;
}
Complex (nested, multiple relation) queries have been supported since Wordpress 4.1.
You can then achieve a more complex query like this:
$meta_query[] = array(
'relation' => 'OR',
array(
'key' => '_stock_status',
'value' => 'noproduzione',
'compare' => '!=',
),
array(
'key' => '_stock_status',
'value' => '10days',
'compare' => '!=',
),
);

ACF query posts where post object is NULL / false

The code below should hopefully make it clear what I'm trying to achieve. The key issue is with the second array in the meta_query. I am trying to find posts where the field 'alias' has not had a post_object set.
When running the query with var_dump( get_field('alias') ); the results returned are 'NULL'. I can't figure out how to query based on a NULL post object field. Pointers would be really appreciated.
$args = array(
'post_type' => 'event',
'posts_per_page' => -1,
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'game',
'value' => 'baseball',
'compare' => '='
),
array(
'key' => 'alias',
'value' => NULL,
'compare' => '='
)
)
);
You're comparing it wrong. You can't check for null in meta_query. Try this:
array(
'key' => 'alias',
'compare' => 'NOT EXISTS'
)
This is basically the SQL way of saying is null.

Wordpress: Query Year in a Range of years

I have a start_year and a end_year
f.e: Project-Lorem-Ipsum: $start_year=2001, $end_year=2005
and I need to select a certain year (f.e. $filter_year=2002) and show the posts that are running in this year.
That means, $filter_year could be $start_year or $end_year or between $start_year and $end_year.
I did this sketch to show maybe better what i need,
thanks for any help!
$filter_year=2002;
$posts = get_posts(array(
'posts_per_page' => -1,
'post_type' => 'post',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'start_year',
'value' => $filter_year,
'compare' => '???',
),
array(
'key' => 'end_year',
'value' => $filter_year,
'compare' => '???',
),
),
You can use meta query for this. But there are changes to be made to make these work, if not already implemented.
1: You can use the date type in meta query but it will only work in the yyyy-mm-dd format
$start_date = date('Y-m-d', strtotime('1/1/15') ); // cant just use the year, it will see it as a time value
$end_date = date('Y-m-d', strtotime('31/12/15') ); // cant just use the year, it will see it as a time value
'meta_query' => array(
'relation' => 'AND'
array(
'key' => 'start_year',
'value' => $start_date,
'compare' => '>='
),
array(
'key' => 'start_year',
'value' => $end_date,
'compare' => '<='
),
)
2: Convert dates to timestamps (preferred)
$start_date = strtotime('1/1/15'); // cant just use the year, it will see it as a time value
$end_date = strtotime('31/12/15'); // cant just use the year, it will see it as a time value
'meta_query' => array(
'relation' => 'AND'
array(
'key' => 'start_year',
'value' => $start_date,
'compare' => '>='
),
array(
'key' => 'start_year',
'value' => $end_date,
'compare' => '<='
),
)
You can loop over the meta values using wp_query or get_posts and checking to see if the get_post_meta($postid, 'start_year', true); exists.

Multiple values in meta_key query

I have a variable that looks like this
$the_vacancies_industry_areas_list = 'Call Centre','Child Care','Cleaning'
And i want to find if just one of these variables satisfies this case in a meta query
array(
'key' => 'industry_areas_list',
'value' => $the_vacancies_industry_areas_list,
'compare' => 'LIKE',
),
It only seems to satisfy if all three areas are present, not just one.
I want someone with just 'Child Care' or 'Cleaning' for example.
You need to set the relation type on the wp_query args to OR, then define each of the OR conditions as arrays.
$args = array(
'meta_query' => array(
'relation' => 'OR',
array (
'key' => 'industry_areas_list',
'value' => 'Call Centre',
'compare' => 'LIKE'
),
array (
'key' => 'industry_areas_list',
'value' => 'Child Care',
'compare' => 'LIKE'
),
array (
'key' => 'industry_areas_list',
'value' => 'Cleaning',
'compare' => 'LIKE'
)
);
Try this: I made $the_vacancies_industry_areas_list as array variable and pass into wp_query
$the_vacancies_industry_areas_list = array('Call Centre','Child Care','Cleaning');
$args = array(
'post_type' => 'YOUR_POST_TYPE', //Set your post_type
'post_status' => 'publish', //Set your post_status
'meta_query' => array(
array(
'key' => 'industry_areas_list',
'value' => $the_vacancies_industry_areas_list,
'compare' => 'IN'
),
),
);

Resources