Ordering posts by custom field within another query - wordpress

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 )
))
));

Related

How define a subfolder for a wordpress custom type having its own query

I have created a Post Type named: Event. It is showing "upcoming"(today events and later) events on the default archive page(/events) via a query I wrote:
$query = new WP_Query( array(
'post_type' => 'event',
'meta_key' => 'date',
'orderby' => 'meta_value',
'order' => 'ASC',
'posts_per_page' => 30,
'paged' => $paged,
'meta_query' => array(
'featured_orderr' => array(
'key' => 'featured_order',
'value' => 1000000000,
'type' => 'NUMERIC',
'compare' => '='
),
'featured_orderrr' => array(
'key' => 'date',
'value' => $today,
'compare' => '>='
),
)
) );
Now I want to show just "today" events on: /events/today and with its own pagination.
Date is a custom field

How to combine ordering on multiarray meta_key query

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.

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 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',
);

wordpress query posts by meta date value

I am trying to query a custom post type by a custom field that contains a date. My query looks like this:
query_posts(
array(
'showposts' => 500,
'post_type' => 'holidays',
'post_status' => 'publish',
'order' => 'desc',
'orderby' => 'meta_value',
'meta_query' => array
(
array
(
'key' => 'from_date',
'value' => array( '01-01-2012', '31-12-2014' ),
'type' => 'DATE', // TRIED: DATE, SIGNED, NUMBER
'compare' => 'BETWEEN'
)
),
));
My custom fields store the date like this: 19-11-2013
When i run the query no results are shown, although dates within the range exist.
Am i approaching this is the correct manor, or am i missing something?
I can confirm Dan White's answer is correct, when querying by a date range the required format needs to be 'YYYY-mm-dd'. Your final code should look like this:
query_posts(
array(
'showposts' => 500,
'post_type' => 'holidays',
'post_status' => 'publish',
'order' => 'desc',
'orderby' => 'meta_value',
'meta_query' => array
(
array
(
'key' => 'from_date',
'value' => array( '2012-01-01', '2014-12-31' ),
'type' => 'DATE',
'compare' => 'BETWEEN'
)
),
));

Resources