Hi I have a custom wordpress page using a query for a meta key if the product is on special for a specific store, however I need to order them on page by a different meta_key called wpcf-order-by.
The following query does not work, it displays my items but not according to the wpcf-order-by.
what am I doing wrong, I have searched and I cannot find anything that suits my case
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'meta_key' => 'store_opening',
'meta_value' => 'yes',
'orderby' => 'wpcf-order-by',
'order' => 'asc',
'posts_per_page' => '-1'
);
You need to use meta_query for that.
meta_query fits to cases where you are working with more than one meta keys in your wp query requests.
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'meta_key' => 'wpcf-order-by',
'orderby' => 'meta_value',
'meta_query' => array(array(
'key' => 'store_opening',
'value' => 'yes',
)),
'order' => 'asc',
'posts_per_page' => '-1'
);
If the values of 'wpcf-order-by' are numbers, then set 'orderby' => 'meta_value_num' instead of 'meta_value'.
Related
I used wp ulike in my site to add vote function per custom post.
I really need to know how can I display the new custom post in my site. All post are showing at first but after I sort it with the code given in How To Sort Most Liked Posts?, new post is not showing. Only the posts showing is the one created before using the sorting code.
here’s my current code
$args = array(
'post_type' => 'ico',
'post_status' => 'published',
'orderby' => 'meta_value_num',
'meta_key' => '_liked',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'sponsored',
'value' => '0',
'compare' => '==' // not really needed, this is the default
)
)
);
sorry for the grammar. Any help will be much appreciated.
Make sure the new post is published and add "numberposts = -1" like so:
$args = array(
'post_type' => 'ico',
'post_status' => 'published',
'orderby' => 'meta_value_num',
'meta_key' => '_liked',
'posts_per_page' => -1,
'numberposts' => -1,
'meta_query' => array(
array(
'key' => 'sponsored',
'value' => '0',
'compare' => '==' // not really needed, this is the default
)
)
);
So I've been looking around here and other sites for a solution. I found lots of really helpfull posts but for some reason I just cant get this to work.
What I have:
WP posts with custom fields.
One is "rating" which is given a number between 1-5
The other is "flash" with either a 1 or a 0.
What I want to do:
Show all posts with a 1 on flash, in ORDER descending by the "rating"...
I currently have:
$args = array(
'posts_per_page' => 11,
'post_status' => 'publish',
'meta_key' => 'rating',
'orderby' => 'meta_value_num',
'order' => 'DESC',
'meta_query' => array(
'meta_key' => 'flash',
'meta_value' => '1',
)
);
$ultimos = new WP_Query( $args );
This does NOT filter the flash custom field.
however if I do:
$args = array(
'posts_per_page' => 11,
'post_status' => 'publish',
'meta_key' => 'rating',
'orderby' => 'meta_value_num',
'order' => 'DESC',
'meta_key' => 'flash',
'meta_value' => '1',
);
$ultimos = new WP_Query( $args );
This DOES filter flash, but does not order them properly.
Any thoughts?
I believe you need to take a look at using the relationship feature of the WP_Query: https://codex.wordpress.org/Class_Reference/WP_Query
$args = array(
'posts_per_page' => 11,
'post_status' => 'publish',
'meta_key' => 'rating',
'orderby' => 'meta_value_num',
'order' => 'DESC',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'flash',
'value' => '1',
'compare' => 'LIKE',
),
);
$ultimos = new WP_Query( $args );
I have a custom field called "sale_status" and the values ("For Sale", "Sold", "Let") are displayed in a radio button in admin. Now each of them can be assigned to a single property.
Currently the query fetches the properties order by post date but I want it to be by sale_status and then date.
My code is as below--
$args = array(
'post_type' => 'zoo-property',
'posts_per_page' => $query__per_page,
'post_status' => 'publish',
'paged' => $query__page,
'meta_key' => 'sale_status',
'orderby' => 'meta_value_num',
'order' => 'DESC',
'meta_query' => array(
'relation' => 'AND',
$query__types,
$query__locations,
$query__statuses,
$query__investments,
$query__price
)
);
$properties_wp_query = new WP_Query($args);
echo "Last SQL-Query: {$properties_wp_query->request}";
But it not showing in correct order.
Any help is highly appreciated. Thanks in advance.
When you want to include multiple sort criterion in the same query the orderby value must be an array. See WP_Query documentation for details. Try something like this:
$args = array(
'post_type' => 'zoo-property',
'posts_per_page' => $query__per_page,
'post_status' => 'publish',
'paged' => $query__page,
'meta_key' => 'sale_status',
'orderby' => array( 'meta_value_num' => 'DESC', 'post_date' => 'DESC' ),
'meta_query' => array(
'relation' => 'AND',
$query__types,
$query__locations,
$query__statuses,
$query__investments,
$query__price
)
);
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' ) );
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')
)
) );