Wordpress Query with Multiple meta_keys not working - php

Ok, so I am querying events made by The Events Calendar, using Advanced Custom Fields. I have a plugin that converts the serialized data to standard under a new meta_key. That said, I am able to query events by the meta_key and meta_value individually. i.e
$args = array(
'numberposts' => -1,
'post_type' => 'tribe_events',
'meta_key' => 'display_override',
'meta_value' => 'Arkansas Literary Festival'
);
and also
// args
$args = array(
'numberposts' => -1,
'post_type' => 'tribe_events',
'meta_key' => 'display_global',
'meta_value' => 'Enabled'
);
However, I can not get them to work simultaneously, i.e
// args
$args = array(
'numberposts' => -1,
'post_type' => 'tribe_events',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'display_override',
'value' => 'Arkansas Literary Festival',
'compare' => '='
),
array(
'key' => 'display_global',
'value' => 'Enabled',
'compare' => '='
)
)
);
When I experiment with this, by using 'OR' or 'AND', and 'LIKE' instead of '=', I either get no posts, or I get the master list of unfiltered posts....

Related

woocommerce product attribute, how to filter between two values

Lets say i have a list off houses and they have a attribute called "size" now I want to get all houses between size 200 and 300.
I have tried
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => 2,
'paged' => $paged,
'meta_query' => array(
array(
'key' => 'pa_size',
'value' => array($sizeMin, $sizeMax),
'compare' => 'BETWEEN',
'type' => 'NUMERIC'
)
);
);
Then I tried with tax_query but I couldn't find a way to get a term between two values.
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => 2,
'paged' => $paged,
'tax_query' => array(
array(
'taxonomy' => 'pa_size',
'field' => 'slug',
'terms' => $sizevalue
)
);
);
Can't understand if this should not be possible but I think the value has to be a string therefor it cant be between.
for now im sorting them in my foreach loop when im displaying them but then my pagination is not working.
My conclusion was that you cant do this with woocommerce product attributes because they a text based, så I made some Advancec custom fields and used them insted like this
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => 2,
'paged' => $paged,
'meta_query' => array(
'relation' => 'AND,
array(
'key' => 'myCutsomField',
'value' => array($sizeMin, $sizeMax),
'compare' => 'BETWEEN',
'type' => 'NUMERIC'
),
array(
'key' => 'myCutsomField2',
'value' => array($valueMin, $valueMax),
'compare' => 'BETWEEN',
'type' => 'NUMERIC'
)
);
$products = new WP_Query( $args );

Query by start date and then by start time in Wordpress

I am trying to query the database and ordering the result by the date and then by time. But with the query below it only orders by the start time. When I remove the start time it orders the result by date.
So far I have tried changing the order and multiple solutions throughout the web but nothing works.
$args = array(
'numberposts' => -1,
'post_type' => 'reservation',
'cat' => $categorie,
'meta_key' => 'start_time',
'orderby' => array( 'start_date' => 'ASC', 'meta_value_num' => 'ASC' ),
'posts_per_page' => 10,
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'start_date',
'compare' => '>=',
'value' => $date,
),
array(
'key' => 'reservation_status',
'compare' => '<',
'value' => 3,
)
)
);
// query
$the_query = new WP_Query( $args );
Please help.
Thanks
How about something like this:
order => 'start_date start_time',
orderby => 'ASC',

What is wrong with wordpress code for displaying random popular posts?

<?php
$args_popular = array(
'post_type' => 'post',
'meta_key'='wpb_post_views_count',
'ignore_sticky_posts' => 1,
'posts_per_page' => $entries_display,
'orderby' => 'meta_value_num' ,
'order' => 'rand()',
array(
'key' => 'wpb_post_views_count',
'value' => '1000',
'compare' => '>=',
),
);
?>
I need to display popular posts whose views >1000 and the posts need to be random. This is the code I have written.
Any suggestions would be appreciated.
You have a lot of issues here
order value is invalid, valid values are ASC and DESC
To order random, orderby needs to be set to rand
You do not need to set the meta_key as you are ordering randomly
You meta_query is incomplete. Your array should be wrappped in another array, and parameter should be meta_query
This should work
$args_popular = array(
'post_type' => 'post',
'ignore_sticky_posts' => 1,
'posts_per_page' => $entries_display,
'orderby' => 'rand',
'meta_query' => array(
array(
'key' => 'wpb_post_views_count',
'value' => '1000',
'compare' => '>=',
'type' => 'decimal',
),
),
);

How to combine meta_query and orderby date meta in WordPress?

I have 3 meta fields, date (date formatted string), location, and consultant.
I can pull the posts and orderby the date field when meta_key is set:
// get posts
$posts = get_posts(array(
'post_type' => 'event',
'posts_per_page' => -1,
'meta_key' => 'start_date',
'orderby' => 'meta_value_num',
'order' => 'DESC'
));
However, I have not be able to get this to work with a more complex meta_query:
$args = array(
'post_type' => 'uppy_events',
'orderby' => 'meta_value_num',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => $key,
'value' => array($value),
'compare' => '>='
)
),
'numberposts' => -1
);
Basically I want to pull all posts that have a meta start_date greater than today's date and order by these dates.
As a side note - can you do a meta_query search on say location key and still orderby date key?
This is the first time I've ever tried ordering by meta, and have found some good posts but still cannot wrap my head around it.
OK, I figured this out as my post lead me to another good example. The commented out meta_query is date greater than today. The other is using a different key. Both work!
$today = date("Ymd");
$args = array(
'post_type' => 'uppy_events',
'post_status' => 'publish',
'meta_key' => 'event_date',
'orderby' => 'meta_value_num',
'order' => 'ASC',
'meta_query' => array(
/* array(
'key' => 'event_date',
'value' => $today,
'compare' => '>='
) */
array(
'key' => 'event_location',
'value' => 'vancouver',
'compare' => '='
)
),
'numberposts' => -1
);

Order by One Custom Field Value and Select Posts From Another With Wordpress

I'm trying to only show posts that are marked as in-stock and order them by their inventory_number (which is a number value so I'm using meta_value_num). The code below is selecting in-stock items, but it isn't ordering the posts by inventory_number. What am I doing wrong?
$args = array(
'numberposts' => -1,
'post_status'=>"publish",
'post_type'=>"post",
'category_name'=>"tape",
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'inventory_number',
'orderby' => 'meta_value_num',
'order' => 'asc'
),
array(
'key' => 'status',
'value' => 'in-stock',
'compare' => 'LIKE'
)
)
);
For order by on a custom field, meta_key=keyname must be present in the query. Plus I don't think you want the order by in the AND clause. So try this...
$args = array(
'numberposts' => -1,
'post_status' => 'publish',
'post_type' => 'post',
'category_name' => 'tape',
'meta_query' => array(
array(
'key' => 'status',
'value' => 'in-stock',
'compare' => 'LIKE'
)
),
'meta_key' => 'inventory_number',
'orderby' => 'meta_value_num',
'order' => 'ASC'
);

Categories