WordPress query - Order by meta-field value - wordpress

I have a post with three meta-fields.
add_post_meta($my_post, 'times', $times);
And i would like to query this category and sort the posts by the meta field value of one of them.
The args i use right now are:
$args=array(
'post_type' => 'post',
'category_name' => 'players',
'order' => 'DESC',
'orderby' => 'meta_value_num',
'meta_key' => 'times',
'meta_query' => array(
array(
'key' => 'times',
'value' => 0,
'compare' => '>=',
),
'posts_per_page'=> '8'
)
);
Where times is the name of the metafield.The above code isn't returning anything.

You have 'posts_per_page'=> '8' inside your meta_query argument.
Change your code into the following:
$args=array(
'post_type' => 'post',
'category_name' => 'players',
'order' => 'DESC',
'orderby' => 'meta_value_num',
'meta_key' => 'times',
'meta_query' => array(
array(
'key' => 'times',
'value' => 0,
'compare' => '>=',
)
),
'posts_per_page'=> '8'
);

Related

Why is numeric ACF custom field order by isn't working in WP_Query

The order by isn't working correctly.
Here is the query:
$args = array(
'post_type' => 'product',
'posts_per_page' => '-1',
'post_status' => 'publish',
/*'meta_key' => 'order',
'orderby' => 'meta_value',
'order' => 'ASC',*/
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'active',
'value' => 'yes',
'compare' => '=',
),
array(
'key' => 'category',
'value' => 'data',
'compare' => '=',
),
),
'orderby' => array(
'order' => 'ASC'
)
);
I have ACF field Order (order) which select with choices from 1 to 15
When the result is displayed they aren't from 1 to 15 but randomly displayed.

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

Wordpress Advanced custom fields meta query filter in post object

I have the following JSON format for event post type
JSON Format
Event Channel Fields
Event Details Fields
I am trying to filter all upcoming events (i.e. future date) and having "Michael Kay" as an event_channel.
Below is my custom query and is not working. The issue is with the event_channel as it is a post object of 'channel' custom posts. The second portion of the meta query is not working.
<pre>
$today_r = date_i18n('j M Y g:i A');
$today = strtotime($today_r);
$upcoming_events = new WP_QUERY(array(
'post_type' =>'event',
'post_status' => 'publish',
'order' => 'ASC',
'orderby' => 'meta_value',
'meta_key' => 'event_start_date',
'meta_value' => $today,
'meta_compare' => '>=',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'event_start_date',
'value' => $today,
'compare' => '>=',
),
array(
'relation' => 'AND',
'key' => 'event_channel',
array(
'key' => 'post_title',
'value' => 'Michael Kay',
'compare' => 'LIKE',
),
)
),
"posts_per_page" => 6
));
</pre>
You need to adjust $args array. You should not pass post_title as meta key.
Use this one:
$upcoming_events = new WP_QUERY(array(
'post_type' =>'event',
'post_status' => 'publish',
'order' => 'ASC',
'orderby' => 'meta_value',
'meta_key' => 'event_start_date',
'meta_value' => $today,
'meta_compare' => '>=',
's' => 'Michael Kay',
"posts_per_page" => 6
));
Update:
Here is the query which filters it by ACF subfield:
$upcoming_events = new WP_QUERY(array(
'post_type' =>'event',
'post_status' => 'publish',
'order' => 'ASC',
'orderby' => 'meta_value',
'meta_key' => 'event_start_date',
'meta_value' => $today,
'meta_compare' => '>=',
'meta_query' => array(
array(
'key' => 'event_channel_0_post_title',
'value' => 'Michael Kay',
'compare' => 'LIKE',
),
)));
To know exact correct name of your event_channel_0_post_title key, go to PHPMYADMIN->wp_postmeta table, and search for "Michael Kay" there. You will see correct name there if event_channel_0_post_title is incorrect.

order by two value in WP_Query - wordpress

I want to order posts first by date and second by views
New update :
$trending_songs = new WP_Query(array(
'post_type' => 'songs',
'showposts' => $songs_count,
'cat' => 'songs_cat-689',
'orderby' => 'meta_value_num',
'meta_query' => array(
array(
'orderby' => 'date',
'order' => 'DESC',
),array(
'key' => 'views',
'order' => 'DESC',
)
),
));
the date is not working in the post (they just order by views)
You can use something like below code (this is just for sample you need to change according to your requirement)
<?php
$args = array(
'post_type' => 'words',
'meta_query' => array(
array(
'key' => 'word_count',
'orderby' => 'date',
'order' => DESC
,
),
array(
'key' => 'title',
'orderby' => 'date',
'order' => DESC
),
array(
'key' => 'word_type',
'orderby' => 'date',
'order' => DESC
),
),
);
$query = new WP_Query( $args );
?>

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