Fetch WordPress posts and order by meta_key's ID - php

I am trying to fetch the post by meta_key in WordPress, and now I want to order posts by the meta_key's ID.
My code:
$args = array(
'post_type' => 'post',
'posts_per_page' => 15,
'meta_query' => array(
array(
'key' => 'featured',
'value' => 'on',
'compare' => '=',
),
),
);
$query = new WP_Query( $args );
I want to order them based on the time meta_key was created. How can I accomplish that?

Related

How to query posts by ACF field but then also order by a separate ACF field?

I am using WP_Query to get posts that have a specific value in one of the ACF fields. I also need to order them by a separate ACF field. I am not sure how to accomplish this. Everything I've read says to use 'orderby' => 'meta_value' but I believe thats the value of the field I'm filtering the posts by, which is not what I want.
This is what I have right now..
$args = array(
'post_type' => 'contacts',
'posts_per_page' => -1,
'meta_key' => 'department',
'meta_value' => 'Transportation',
'orderby' => 'meta_value'
);
$the_query = new WP_Query( $args );
I need to orderby an ACF field named last_name.
It's possible to assign a name to a meta query, and then refer to that name in your orderby. Something like this.
$args = array (
'post_type' => 'contacts',
'post_status' => 'publish',
'nopaging' => true,
'posts_per_page' => -1,
'meta_query' => array( 'main_query' => array(
'key' => 'department',
'value' => 'Transportation'
), 'orderby_query' => array(
'key' => 'last_name',
)
),
'orderby' => array(
'orderby_query' => 'ASC',
),
);
$the_query = new WP_Query( $args );

Wordpress $args select query from multiple custom post with custom category check

Hi to all I want to create $args query to check that if custom post has category then check if category value is none or not if not then check if custom field value is true or false. and for other post type that category is not available.
here is my code
$args = array(
'posts_per_page' => 10,
'paged' => $page_no,
'post_type' => array('ct_photo', 'ct_event', 'ct_video', 'ct_thought'),
//'post_type' => 'ct_video',
'orderby' => 'post_date',
'order' => 'DESC',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'vid_category',
'compare' => 'IN',
'value' => array(10, 11, 12),
),
array(
'relation' => 'AND',
array(
'key' => 'vid_category',
'compare' => 'IN',
'value' => array(10, 11),
),
array(
'key' => 'display_home',
'compare' => '=',
'value' => 'true',
)
),
)
);
Now what i Want
I want that this query will return result which check that if post type is video then it check that if category value is available then check custom field "display_home" is true or not and if category is not selected then it must be show in query.
and for other post type it will not check category.
i tried to use case & if but that is not working.
Kindly help me.
Ohkay I found my answer
SELECT wp.id, wp.post_type, wpm.meta_key, wpm.meta_value FROM `wp_posts` as wp Inner Join wp_postmeta as wpm ON wp.id = wpm.post_id where (wpm.meta_key = 'vid_category' AND wpm.meta_value ="true") OR (wpm.meta_key = 'display_home' and wpm.meta_value ="true") OR(wp.post_type IN ('ct_photo', 'ct_event', 'ct_thought')) and wp.post_status = 'publish' GROUP BY wp.id ORDER BY `id` ASC

Sort post meta data using meta_key?

I'm trying to get the post, meta datas on table wp_postmeta. I need to get the post id, meta keys and meta value by using the meta_key and post id. The meta key stored is date. eg,. 2014-01-02, 2014-02-03, I have to query them based on the year or month.
$args = array(
'post_type' => 'calendar_holiday',
'ID' => $id;
'meta_query' => array(
array(
'key' => ,
'value' => ,
'compare' => 'LIKE'
),
)
);
$query = new WP_Query( $args );
I'm not sure what to put on key and value.. any idea?? thanks
// 'orderby' => 'meta_value_num', // it's a numeric value
$args = array(
'orderby' => 'meta_value',
'post_type' => 'calendar_holiday',
'meta_query' => array(
array(
'key' => 'over-make',
'value' => 'Doral',
'compare' => 'LIKE'
)
)
);
$query = new WP_Query( $args );

WordPress Query: ORDER BY CASE WHEN

I want to first display the posts that match a particular meta_key => cr_id and order those by another 'meta_key' => 'cr_list_price' and then display the rests of the posts that are also ordered by 'meta_key' => 'cr_list_price'.
This is my current code which does everything I want except show the posts where cr_id = 4.
$args = array(
'post_type' => 'properties',
'paged' => get_query_var( 'paged' ),
'meta_query' => array(
array(
'key' => 'cr_list_price',
'value' => array($minPrice, $maxPrice),
'type' => 'numeric',
'compare' => 'BETWEEN'
),
array(
'key' => 'cr_prop_bed',
'value' => $beds,
'compare' => '>='
),
),
'orderby' => 'meta_value_num',
'meta_key' => 'cr_list_price'
);
$loop = new WP_Query( $args );
If I was using raw MySQL I'd do something like ORDER BY CASE WHEN in my query, however I'm not sure how or if I can accomplish this with WordPress.
I wonder if you could add a filter to the query by adding that little extension on there. I'm thinking maybe posts_orderby
Function
function filter_case($orderby = '') {
$orderby .= 'CASE WHEN [some conditions]';
return $orderby;
}
Before Query
add_filter( 'posts_orderby', 'filter_case' );
$wp_query = new WP_Query($args);
remove_filter( 'posts_orderby', 'filter_case' );
I cant gaurantee this will work first time but it could be worked on if you think it's worth pursuing? List of WP_Query filters are here.

Retrieve CPT Posts from a specific category | WordPress

I am currently retrieving posts from a custom post type using the following array:
$args = array(
'post_type' => 'event',
'meta_query' => array(
array(
'key' => 'location',
'value' => get_the_ID(),
'compare' => 'like',
)
)
);
$events_list = new WP_Query($args);
It works great.
Now, I was wondering if I could retrieve these same posts but ONLY if they appear in the category ID '6'.
Can someone explain how I update my current code to allow this?
Thank you.
Use 'cat' instead of 'category':
$args = array(
'post_type' => 'event',
'cat' => 6,
'meta_query' => array(
array(
'key' => 'location',
'value' => get_the_ID(),
'compare' => 'like',
)
)
);
$events_list = new WP_Query($args);

Categories