WordPress sort wp_query multiple meta_values - php

I would like to sort my post and tried nearly everything to make it work.. but no luck :-(
In my loop is a custom post type called deal and expired deal (meta_value through ACF) posts. I want to show the normal posts first and then the expired posts.
This is my code so far:
$args = array(
'posts_per_page' => -1,
'post_type' => 'deal',
'orderby' => 'date',
'order' => 'DESC',
'post__not_in' => array($not_in),
);
Any ideas how I can put the "expired" posts behind the normal posts?

What you want to do is set orderby to meta_value and meta_key to your custom field.
$args = array(
'posts_per_page' => -1,
'post_type' => 'deal',
'meta_key' => 'YOURCUSTOMFIELDHERE',
'orderby' => 'meta_value',
'order' => 'DESC',
'post__not_in' => array($not_in),
);
If the DESC order is the wrong direction you can switch it to ASC.

Related

Having Issues getting custom posts by Advanced Custom Fields (meta_key and meta_value)

I don't know why I am having this issues, I've done this sort of thing before, hopefully somebody can help shed some light on this situation.
$arch_state = get_field('service_area_archive_state');
$args01 = array(
'post_type' => 'service-area',
'posts_per_page' => -1,
'orderby' => 'title',
'order' => 'ASC',
'meta_key' => 'service_area_state',
'meta_value' => $arch_state
);
$serv_areas = get_posts($args01);
then, I use a foreach loop to go through the query.
foreach($serv_areas as $post) {
setup_postdata($post);
...
wp_reset_postdata();
}
for some reason, this query isn't returning anything. I am able to get the all the posts of that custom post type without an issue when I remove the meta_key and meta_value fields.
But for some reason, as soon as I add the meta key and meta value, nothing...
Any ideas?
Not sure if you need to put two quotations '' between $arch_state so its like this:
$args01 = array(
'post_type' => 'service-area',
'posts_per_page' => -1,
'orderby' => 'title',
'order' => 'ASC',
'meta_key' => 'service_area_state',
'meta_value' => '.$arch_state.'
);
Try to set value there and see if its working 'meta_value'
more you can see here:
https://www.advancedcustomfields.com/resources/query-posts-custom-fields/

WordPress: switch meta_query orderby in the frontoffice

Was looking for a while for a solution, but couldn't find one. So my question is, I have this code that sets posts order by default its DATE:
$args = array(
'post_type'=>'paibcresume',
'posts_per_page' => 10,
'paged' => $paged,
'meta_query' => array(),
'tax_query' => array(),
'orderby' => 'date',
'meta_key' => '',
'order' => 'DESC'
);
I need some kind of a switch on the website, so user can pick how to order posts, for example it could be date to order by date, or modified to order by date of modification, or it could be a custom meta_key. How could I do that?
Check below url
http://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters
$query = new WP_Query( array ( 'post_type' => 'product', 'orderby' => 'meta_value_num', 'meta_key' => 'price' ) );

WordPress WP_Query: Display custom post type based on custom meta value, and also order on another custom meta value

I am using the WordPress plug-ins Advanced Custom Fields, and Custom Post Type UI.
I have built a WP_Query to display a particular post type, filtered by a particular custom field value.
$loop = new WP_Query( array(
'post_type' => 'news',
'meta_key' => 'news_story_type',
'meta_value' => 'release',
'posts_per_page' => 3
) );
I now want to sort the resulting posts by another custom field, date_of_publication rather than use WordPress's menu_order or date. The ACF documentation says to specify orderby and meta_key in the query args.
$args = array(
'post_type' => 'event',
'posts_per_page' => -1,
'meta_key' => 'start_date',
'orderby' => 'meta_value_num',
'order' => 'DESC' );
But alas, doing so conflicts with the meta_key I've already supplied to filter.
Has anyone encountered this before and found a solution?
Try using meta_query
$loop = new WP_Query( array(
'post_type' => 'news',
'meta_key' => 'start_date',
'orderby' => 'meta_value_num',
'order' => 'DESC',
'posts_per_page' => 3,
'meta_query' => array(
array('key' => 'news_story_type', 'value' => 'release')
)
) );

Wordpress orderby meta_value not ordering properly

I have a custom post type called 'events' and I am to display the first 4 more recent events in event date order on the home page. I have got the events to show on the home page however the ordering doesn't seem to be working (see below the example). It seems to be ordering in its own way.
$args = array('post_type' => 'events', 'meta_key' => 'event-date', 'orderby' => 'meta_value', 'order' => 'ASC', 'posts_per_page' => -1);
$events = new WP_Query( $args );
That is my code and here are the results (the dates) I get back.
16/04/2014
16/05/2014
19/03/2014
25/02/2014
27/02/2014
28/02/2014
As you can see, this is not ordering by ASC so what have I done wrong?!
Thanks in advance
Try to use meta_value_num instead of meta_value in orderby parameter. Use following code:
$args = array(
'post_type' => 'events',
'meta_key' => 'event-date',
'orderby' => 'meta_value_num',
'order' => 'ASC',
'posts_per_page' => -1
);
$events = new WP_Query( $args );

Wordpress loop for multiple custom post types

Looking for a way to loop through multiple custom post types and get the 4 most recent posts.
I have the below but doesn't quite work as if you 2 posts of the same post type, it'll show the most recent rather than showing most recent of ALL custom posts.
$args = array('post_type' => array('cs_trainee', 'cs_graduates', 'cs_pros', 'sd_trainee', 'sd_graduates'), 'posts_per_page' => 4, 'orderby' => 'menu_order', 'order' => 'ASC');
$careers = new WP_Query( $args );
Thanks
Just change the query_posts parameters a bit....
query_posts( array(
'post_type' => array( 'post', 'report', 'opinion', bookmark' ),
'cat' => 3,
'orderby' => 'date',
'order' => 'DESC',
'showposts' => 5 )
);
That should take care of it for you.

Categories