Order post by meta value with semicolon - php

I had a post type music. For which i created custom field Music length. Which store music duration in this format like 01:50
I want to display post on order by Music length. I had use both order by meta_value and meta_value_num. But it does not work. My code to get post is:
<code>
$args = array(
'post_type' => 'music',
'meta_key' => 'length',
'orderby' => 'meta_value_num',
'order' => 'DESC',
'posts_per_page' => 10
);
$pop_posts = new WP_Query( $args );
</code>
My question is how to order the post by its meta value in semicolon format(01:50, 02:46, 03:04, 02:37). So the post with meta value 03:04 comes first then 02:46 and so on! Is there any way?

Use following code
$args = array(
'post_type' => 'music',
'meta_key' => 'length',
'orderby' => "REPLACE(meta_value_num, ':', '')",
'order' => 'DESC',
'posts_per_page' => 10
);
$pop_posts = new WP_Query( $args );
But note that it will be very slow for large datasets as it has to recompute the new string for every row.

Related

Calling a Custom Field Value in the Wordpress Loop?

I am trying to call a custom field's metadata, and want to use it as a flag field for a Custom Post Type's Loop for a page. The field is 'tt_freemium'. The code I have below pulls everything and ignores the flag field. Uuuugh. What am I doing wrong ?
<?php $args = array( 'post_type' => 'membercontent', 'tt_freemium' => 'true', 'orderby' => 'post_date', 'order' => 'DESC', 'posts_per_page' => '200' );
$ourposts = new WP_Query( $args );?>
The answer in case anyone wants to know is to add a meta_query and an array to fill the meta query. It works now. Sorry to bother anyone that read this. ;-) Have a nice day y'all.
<?php $args = array(
'post_type' => 'membercontent',
'meta_query' => array(
array(
'key' => 'tt_freemium',
'value' => 'true',
),
'orderby' => 'post_date',
'order' => 'DESC',
'posts_per_page' => '200' );
$ourposts = new WP_Query( $args );?>

Order posts by highest rated in Wordpress

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

determine posts per page in second loop wpQuery

I have multiple loops using wp_query() in WordPress.
I want to show the posts from first loop first (post per page=10),
and if there are no posts from first loop show, then show the second loop.
For example, if I only have 8 posts from first loop, the second loop should show 2 posts.
The loop is working properly, but I can't solve the post per page issue. How can I do this? I also need pagination for the remaining posts.
$args = array(
'post_type' => 'event',
'event-categories' => 'featured',
'orderby' => 'meta_value_num',
'order' => 'ASC',
);
$loop = new WP_Query( $args );
$args1 = array(
'post_type' => 'event',
'event-categories' => 'abc'
'orderby' => 'meta_value_num',
'order' => 'ASC',
);
$loop1 = new WP_Query( $args1);

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