How to combine ordering on multiarray meta_key query - wordpress

I have a query, where I'm first checking for all posts which have meta_key topbox (kind of featured post). After that I'm checking, what custom order they have set in meta_key topbopx_order.
So far it works fine, unless two posts have the same order value, then it starts behave unpredictably. It seems to me, that it doesn't take date into account at all, because I have many more posts in the row, which have newer publish date and order set to 1, but they don't display.
This is my query so far
$feat_args = array(
'posts_per_page' => 5,
'post_status' => 'publish',
'meta_key' => 'topbox_order',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'asb_topbox',
'value' => 'yes',
'compare' => '='
),
array(
'key' => 'topbox_order',
'value' => array(1, 2, 3, 4, 5),
'compare' => 'IN'
)
),
'orderby' => array (
'meta_value_num' => 'ASC',
'date' => 'DESC'
),
'ignore_sticky_posts' => true,
'no_found_rows' => true,
);
$feat = new WP_Query( $feat_args );
Plus I'm setting topbox_order default for all posts to value 1.
Edit
I see now, that I'm maybe trying to achieve something impossible. I'd like to set the order to posts, but at the same time, I like them to be pushed down by newer posts with same or higher order number. Is there a combination of compare that would allow this behavior?

The solution was way over my head and seemed too complicated. After some talk with client we decided to go a different route - to control only the first post in the row.
Here is what I ended up with - I'm calling two sets of posts, order them before they get to the main wp_query and just display.
// first query
$first_ids = get_posts( array(
'fields' => 'ids',
'posts_per_page' => '1',
'post_status' => 'publish',
'orderby' => 'date',
'meta_key' => 'asb_topbox',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'asb_topbox',
'value' => 'yes',
'compare' => '='
),
array(
'key' => 'topbox_ofset',
'value' => 'no',
'compare' => '='
)
)
));
// second query
$second_ids = get_posts( array(
'fields' => 'ids',
'posts_per_page' => '5', // because one can be the same as first
'post_status' => 'publish',
'orderby' => 'date',
'meta_key' => 'asb_topbox',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'asb_topbox',
'value' => 'yes',
'compare' => '='
),
array(
'relation' => 'OR',
array(
'key' => 'topbox_ofset',
'compare' => 'EXISTS'
),
array(
'key' => 'topbox_ofset',
'compare' => 'NOT EXISTS'
)
)
)
));
// Remove duplicates
$mergedposts = array_unique( array_merge( $first_ids, $second_ids));
$feat_args = array(
'posts_per_page' => 5,
'post_status' => 'publish',
'post__in' => $mergedposts,
'orderby' => 'post__in',
'ignore_sticky_posts' => true,
'no_found_rows' => true
);
In principle, you can set the post to skip to second position. And no, I didn't wanted to use sticky for this, as with time I would have sticky posts all over the place and didn't wanted to keep in mind, that I have to make them unstick. My solution keeps the date flow and I can hold the first posts for some longer time without affecting the rest of the archives.

Related

Ordering posts by custom field within another query

I've googled and tried to figure this out on my own but here goes. I'm querying a custom post type and certain posts within that custom post type with an ID of 180. I then need to order these posts by a custom field. This is my code:
$namskeid = new WP_query(array(
'posts_per_page' => '6',
'post_type' => 'namskeid',
'meta_query' => array(
array (
'key' => 'course_type_display',
'compare' => 'LIKE',
'value' => get_the_ID( 180 )
))
));
Is there any way to do this?
I finally figured it out. I failed to specify meta_type = Date and orderby => 'meta_value_num'.
Here is the code that works:
$namskeid = new WP_query(array(
'posts_per_page' => '6',
'post_type' => 'namskeid',
'meta_type' => DATE,
'orderby' => 'meta_value_num',
'order' => 'ASC',
'meta_key' => 'course_date',
'meta_query' => array(
array (
'key' => 'course_type_display',
'compare' => 'LIKE',
'value' => get_the_ID( 180 )
))
));

Wordpress Query by custom date filed

Can someone show me how to do this, I have a query I'm using to display custom posts and using WP Facet for the query, by a custom field date, I got this far to order the posts by date in Ascending order, but the last thing I want to do is only show the posts that equal today's date or future dates;
I've tried a few things but no joy as of yet, thanks
<?php
return array(
'post_type' => 'event',
'post_status' => 'publish',
'posts_per_page' => 15,
'orderby' => 'event_date',
'order' => 'ASC',
'sort_custom' => true,
'meta_query' => array(
array(
'key' => 'event_date',
),
)
if this helps anyone - i worked it out:
<?php
return array(
'post_type' => 'event',
'post_status' => 'publish',
'meta_key' => 'event_date',
'posts_per_page' => 15,
'orderby' => 'event_date',
'order' => 'ASC',
'sort_custom' => true,
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'event_date',
'compare' => '>=',
'value' => date("Y-m-d"),
'type' => 'DATE'
),
)
);

WP_Query Order By Two Meta Fields

I need to order a WP_Query by two meta fields: meta-order and meta-last-name. First by meta-order numerically, if blank, order by meta-last-name ASC. How do I do that, the documentation is not very clear on that. I've tried this but it does not work.
array(
'post_type' => 'student',
'post_status' => 'publish',
'order' => 'ASC',
'orderby' => 'meta_value',
'meta_key' => 'meta-order meta-last-name',
);
You will need to use the pre_get_posts action to be able to achieve this.
See answers from https://wordpress.stackexchange.com/q/169999 on how to do it.
This worked for me:
array(
'post_type' => 'student',
'meta_query' => array(
'relation' => 'OR',
'custom_order_clause' => array(
'key' => 'meta-custom-order',
'compare' => 'LIKE',
),
'last_name_clause' => array(
key' => 'meta-last-name',
'compare' => '=',
),
),
'orderby' => array(
'custom_order_clause' => 'ASC',
'last_name_clause' => 'ASC'
),
)
CAVEAT:
Make sure you delete empty meta values from DB otherwise this will not work.

Wp_query with 2 meta keys and array of meta values

Hi in my post_type=shop i have 2 meta keys and array of values
Custom fields
Name Values
cu_status pending,processing,completed
cu_date 12-Jan-2016 , 13-Jan-2016, ...... any date in the same format date("d-M-Y")
Now i need to loop through all posts with cu_status =pending,processing and cu_date is between 12-Jan-2016 to 13-Apr-2016
What will the query ?
Iam very confused . For to get all post with status pending,processing I know the query
$args = array(
'post_type' => 'shop',
'post_status' => 'publish',
'meta_query' => array(
array(
'key' => 'cu_status',
'value' => array('pending','processing'),
'compare' => 'IN',
)
),
'posts_per_page' => -1
);
Pleases help to complete the query .
You need to use the relation operator e.g.
$args = array(
'post_type' => 'shop',
'post_status' => 'publish',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'cu_status',
'value' => array('pending','processing'),
'compare' => 'IN',
) ,
array(
'key' => 'cu_date',
'value' => array($start, $end),
'compare' => 'BETWEEN',
'type' => 'DATE'
)
),
'posts_per_page' => -1
);
Also use the compare=>BETWEEN for getting the difference in 2 dates.
You may need to tweak the code a bit as I have not tested it.

wp_query on 2 post types, one having custom field for further filtering

This is my first question on here; I have hunted around for an answer but if it is out there already apologies.
I am using wp_query() to fetch both 'posts' and a custom post type called 'reviews'. However the custom post type has a sub type and I need to filter out all but one of these sub types just for reviews in the same query!
This is where I got to:
$type = array( 'post', 'review' );
$args=array(
'post_type' => $type,
'post_status' => 'publish',
'posts_per_page' => 20,
'paged' => $paged,
'meta_query' => array( // I need to filter the review post type further by subtype
array(
'key' => 'subtype',
'value' => 'review'
),
),
'orderby' => 'post_date', // Order by date
'order' => 'DESC',
);
Obviously, when I run this the default post type is not listed as it doesnt have the subtype custom field. How can I only apply this filter (meta_query) to the custom post type in this query and not have it apply to the default post type 'post'?
Thanks in advanced,
Kris...
UPDATE:
OK, I have tried this but with no success - any ideas?
$args=array(
'post_type' => $type,
'post_status' => 'publish',
'posts_per_page' => 20,
'paged' => $paged,
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'subtype',
'compare' => 'NOT EXISTS',
),
array(
'key' => 'subtype',
'value' => 'review',
),
),
'orderby' => 'post_date', // Order by date
'order' => 'DESC',
);
This will check the 'subtype' only if it exists in the post (not tested)
$type = array( 'post', 'review' );
$args=array(
'post_type' => $type,
'post_status' => 'publish',
'posts_per_page' => 20,
'paged' => $paged,
'meta_query' => array(
'relation' => AND,
array(
'key' => 'subtype',
'compare' => 'EXISTS'
),
array(
'key' => 'subtype',
'value' => 'review'
),
),
'orderby' => 'post_date', // Order by date
'order' => 'DESC',
);

Resources