Order posts by highest rated in Wordpress - php

I'm creating a site with overviews of posts which should possibly be ordered by their rating. The rating is set up in a way that people can comment on a post and submit a rating with that comment of they want. I want to create a filter that gets all the posts and shows the highest rated posts first.
The way I get the comments for a single post:
get_comments( array('post_id' => $post->ID) );
The way I get the rating from that post:
get_comment_meta($comment->comment_ID, 'cijfer', true );
Now keep in mind that not every comment will have an actual rating attached to it. How can I modify this bit of code that gets my posts to order them by rating High -> Low.
$order = array (
'order' => 'ASC',
'cat' => $cat,
'post_type'=> 'adressen',
'paged' => $paged,
);

From the docs https://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters
'orderby' with 'meta_value' and custom post type
Display posts of type 'my_custom_post_type', ordered by 'age', and filtered to show only ages 3 and 4 (using meta_query).
$args = array(
'post_type' => 'my_custom_post_type',
'meta_key' => 'age',
'orderby' => 'meta_value_num',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'age',
'value' => array( 3, 4 ),
'compare' => 'IN',
),
),
);
$query = new WP_Query( $args );
Hope this helps

Related

Wordpress sort by number custom fields gives different results

I have a query which gives me custom post type posts which are sorted by category and a custom fields which has a number that indicates the amount of votes for the post. The problem is, if 2 posts have 3 votes, sometimes when you refresh, it changes their position. Here is the code:
$args = array(
'numberposts' => 10,
'post_type' => 'nominees',
'meta_query' => array(
array(
'key' => 'category',
'value' => get_the_ID(),
'compare' => '=',
)
),
'orderby' => 'meta_value_num',
'order' => 'DESC',
'meta_key' => 'count_nominees'
);
I tried adding order on the category meta query, but nothing changes, I still sometimes get different results when refreshing, if the posts have same amount of votes. What is the right way to add second order, by ID or something?
Thanks.
As mentioned in the comments, I think this might help, but you might be able to extend it to be a little more specific in the search query for your use case.
$args = array(
'numberposts' => 10,
'post_type' => 'nominees',
'meta_query' => array(
array(
'key' => 'category',
'value' => get_the_ID(),
'compare' => '='
)
),
'meta_key' => 'count_nominees',
'orderby' => array(
'count_nominees' => 'DESC',
'ID' => 'DESC'
)
);
That should get 10 posts in the nominees post type, only if they're part of category xyz, and have post meta of count_nominees and order by count_nominees first in descending order and then by the post ID in descending order.
You can use the WP_Query documentation on the WordPress codex for more information about complex queries using post meta data.

WordPress sort wp_query multiple meta_values

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.

Show posts from particular category first then remaining wpQuery

i want to show the posts from particular category and after that all remaining posts from other remaining category.
this is my query. what changes to done in $args array for achieving my goal.
note that i can only use one loop.
this is my query.
$args = array(
'post_type' => 'event',
'meta_key' => '_event_end_date',
'meta_compare' => '>',
'meta_value' => $today,
'order' => 'DESC',
'orderby' =>'category',
'paged' =>$paged1,
);

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.

Displaying only custom post types that's custom date fields values are either today or in the future

I'm using the following custom WordPress query to display data (events) from a custom post type on the homepage of my site:
$posts = new WP_Query(array('post_type' => 'events',
'posts_per_page' => '3',
'meta_key' => 'event_date',
'orderby' => 'meta_value_num',
'order' => 'ASC'
));
This works as expected,however, as the nature of events is that they are always in the future i have a requirement that within the above query i need to filter out any post that's custom field 'event_date' is in the past, I also still wish to display, events occurring on the current day.
I've tried post_status => future but this only displays unpublished posts whereas all of my events are already published. Any insight is much appreciated Thank you
You should use meta_query custom field parameters.
$args = array(
'post_type' => 'events',
'posts_per_page' => '3',
'orderby' => 'meta_value',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'event_date',
'value' => date( YOUR-FORMAT-HERE ),
'compare' => '>='
)
)
);
$query = new WP_Query( $args );
Hope it helps!

Categories