I am trying to sort by my WP_Query results by two meta fields, first date and then time. So effectively
Date A 17:00
Date B 18:00
Date C 07:00
Date B 10:00
Date A 9:00
would be come
Date A 9:00
Date A 17:00
Date B 10:00
Date B 18:00
Date C 07:00
This is my current code:
"post_type" => "event",
"post_status" => "publish",
"posts_per_page" => 25,
"ignore_sticky_posts" => 0,
"show_sticky" => true,
"meta_query" => array(
array(
"key" => "_meta_event_start_date",
"compare" => ">=",
"value" => 0
),
array(
"key" => "_meta_event_start_time",
"compare" => ">=",
"value" => 0
),
),
"order" => "ASC",
"orderby" => "_meta_event_start_date _meta_event_start_time"
It is currently sorting my results appropriately by the date, but not the time (time format is stored hh:mm (24hr)). How might I amend the query to make this work?
I have seen other responses recommending using foreach loops, sql queries or even filters. I can't use these unfortunately as the site I'm working on uses the search plugin Facet WP, and as search requires WP_Query to query the database and output content.
Thanks in advance.
As I see you are trying to order by meta keys - you have to use in this case a quite more complicated syntax according to changes in WordPress 4.2 - something like below:
"post_type" => "event",
"post_status" => "publish",
"posts_per_page" => 25,
"ignore_sticky_posts" => 0,
"show_sticky" => true,
"meta_query" => array(
"event_start_date" => array(
"key" => "_meta_event_start_date",
"compare" => ">=",
"value" => 0
),
"event_start_time" => array(
"key" => "_meta_event_start_time",
"compare" => ">=",
"value" => 0
)
),
"orderby" => array(
"event_start_date" => "ASC",
"event_start_time" => "ASC"
)
Additionally - for single meta_key ordering it is worth to remember that you have to use in the orderby clause values meta_value and meta_value_num
Related
I have a CPT with daily deals, I need to show these daily deals on my frontend as per today's day.
For eg, if it is Thursday : The returning array should return Thursday, Friday, Saturday, Sunday, Monday, Tuesday, Wednesday.
For this to happen I have a meta_value on all posts namely Day which returns days in the following order:
$days = [
1 => 'Monday',
2 => 'Tuesday',
3 => 'Wednesday',
4 => 'Thursday',
5 => 'Friday',
6 => 'Saturday',
7 => 'Sunday',
];
My current WP_Query is follows which returns data in ascending order according to date : Monday to Sunday
$args = array(
'post_status' => 'publish',
'post_type' => 'special',
'meta_key' => 'day',
'orderby' => 'day',
'order' => 'ASC',
);
I want to construct a query such as I can assign a starting day and then continue the rest of the days.
Could anyone please help?
I tried adding comparing operators but they just remove the rest of the days as a whole.
How can I group checkboxes for entities in Symfony forms?
->add(
'products',
EntityType::class,
[
'class' => Product::class,
'choice_label' => 'titel',
'multiple' => true,
'expanded' => true,
]
)
Im my domain model a Product belongs to exactly one ProductFamily. Every ProductFamily belongs to exactly one ProductSuperFamily. In my form the Product checkboxes should be grouped like this:
Product Super Family 1
Product Family 1
[ ] Product 1
[ ] Product 2
Product Family 2
[ ] Product 3
Product Family 3
[ ] Product 4
[ ] Product 5
Product Super Family 2
Product Family 4
[ ] Product 6
[ ] Product 7
[ ] Product 8
Product Family 5
[ ] Product 9
Product Super Family 3
Product Family 6
[ ] Product 10
[ ] Product 11
How can I achive this?
It is allowed by the choice type, you must use the group_by option:
'group_by' => function(Product $product) {
return $product->getFamily()->getLabel()
},
I'm trying to achieve a list of most tagged taxonomy tagged in last 24 hours. For an instance:
China from taxonomy country has been tagged 20 times
Eminem from taxonomy music has been tagged 15 times
Chelsea from taxonomy football has been tagged 10 times
USA from taxonomy country has been tagged 12 times
The result should show list something like: China Eminem USA Chelsea
I ended up with this:
<?php $terms = get_terms( array(
'taxonomy' => 'country','music','football',
'orderby' => 'count',
'monthnum' => date('m'),
'day' => date('d'),
'year' => date('Y')
);
$loop = new WP_Query($args);
if ($loop->have_posts()) :
while ($loop->have_posts()) : $loop->the_post(); ?>
Table1(identity1, a1,b1)
Table2(identity, foreign(identity1),foreign(identity1))
Now when I use this query
$query = $qb->select('a1', ' b1')
->from('table2', 'm1')
->join('ApiMapBundle:tabl1, 'u1', 'WITH', $qb->expr()->orX('m1. foreign(a1) =u1.identity', 'm1. foreign(b1) = u1.identity '))
->andWhere('m1.identity=:tt')
->setParameter('tt', $cn)
->getQuery()
->getResult();
Now the problem with this query is that sometimes it gives id let's tt:5 so it gives me a value like this
Array(
0 => Array(id => 1, b1=> 8000225),
1 => Array(id => 9, b1 => 8000234)) given).
Basically in table the values structure is like this
Table2(Identity=5,foreign1=9,foreign2=1)
Any idea that how can I exactly get the given structure? Because in some cases it is fine they give me proper foreign 1 and foreign 2 but in other cases it make it alternative. Any idea?
This is the array for my arguments for the WP_Query.
Array
(
[showposts] => 4
[paged] => 1
[post_type] => post
[post_content] => tree
[category__and] => Array
(
[0] => 6
[1] => 15
[2] => 28
)
)
I want to return the posts where they are in the categories 6, 15, 28 and where the post_content has the word tree.
My problem is that I'm returning several duplicated results for the posts that have the word tree in it. Ideally I would like to return one.
anyone have any idea how I can fix or improve this?
I found the issue, I shouldn't have been trying to filter on post_content, but rather 's'
e.g.
Array
(
[showposts] => 4
[paged] => 1
[s] => post
[post_content] => tree
[category__and] => Array
(
[0] => 6
[1] => 15
[2] => 28
)
)