0

I've read a lot, tried a lot more, but can't get what I want: I have custom post types, which I want to order in DESC, orderby its meta value:

The complete query with its arguments:

wp_reset_query();
$args = array(
            'meta_key'          => 'completeness',
            'order_by'          => 'meta_value_num',
            'posts_per_page'    => -1,
            'order'             => 'DESC',
            'post_type'         => 'datensatz',
            'post_status'       => 'draft'
        );
$the_query = new WP_Query( $args );
if ( $the_query -> have_posts() ):
    while ( $the_query -> have_posts()) : 
        $the_query -> the_post();
        global $post;
        echo get_the_title().':'.get_post_meta( $post->ID, "completeness", TRUE ).'<br/>';
    endwhile;
endif;
wp_reset_query();

This query does not work. Any help would be greatly appreciated.

I have just had a look at the last db query by $wpdb->last_query and got this:

 "SELECT post_id, meta_key, meta_value FROM wp_postmeta WHERE post_id IN (206,200,199,198,197,[..]) ORDER BY meta_id ASC" 

So my WP query MUST be wrong...

5
  • Do you really need to get draft. I think it should be publish. Also, does your meta key completeness have only numbers?
    – Robert hue
    Oct 28, 2014 at 11:21
  • @Robert hue Yep. I need to get the draft and completeness is a non decimal, numeric value.
    – user998163
    Oct 28, 2014 at 12:01
  • It should be working. Nothing is wrong in it. Why don't you try it step by step and see where is the problem. First add only post_type then add post_status, and so on. Also post complete query.
    – Robert hue
    Oct 28, 2014 at 12:14
  • @Robert hue I did it stepwise. Right now I am on the part with the meat query and it does not work. Have a look at my edit above, there you can see the whole query.
    – user998163
    Oct 28, 2014 at 12:36
  • Why there're two wp_reset_query(); there? Both are wrong by context. Use wp_reset_postdata(); once, only after the WP_Query(), not before it. Oct 28, 2014 at 13:12

1 Answer 1

1

Your query seems fine but there are couple of issues. Instead of your code (from wp_reset_query(); to wp_reset_query();) use following code and let us know what is the output.

$new_args = array(
    'post_type'         => 'datensatz',
    'post_status'       => 'draft',
    'posts_per_page'    => -1,
    'meta_key'          => 'completeness',
    'orderby'          => 'meta_value_num',
    'order'             => 'DESC',
);

$my_new_query = new WP_Query( $new_args );

var_dump( $my_new_query->request );

EDIT

Now try this. This should also work.

$new_args = array(
    'post_type'         => 'datensatz',
    'post_status'       => 'draft',
    'posts_per_page'    => -1,
    'meta_key'          => 'completeness',
    'orderby'          => 'meta_value_num',
    'order'             => 'DESC',
);

$my_new_query = new WP_Query( $new_args );

if ( $my_new_query -> have_posts() ) :
    while ( $my_new_query -> have_posts()) : $my_new_query -> the_post();
        echo get_the_title() . ': ' . get_post_meta( $post->ID, "completeness", TRUE ). '<br/>';
    endwhile;
endif;

wp_reset_postdata();
8
  • this is what I get: "SELECT wp_posts.* FROM wp_posts INNER JOIN wp_postmeta ON (wp_posts.ID = wp_postmeta.post_id) WHERE 1=1 AND wp_posts.post_type = 'datensatz' AND ((wp_posts.post_status = 'draft')) AND (wp_postmeta.meta_key = 'completeness' ) GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC "`
    – user998163
    Oct 28, 2014 at 15:06
  • That's working perfectly. No problem in the query. I have made EDIT in my answer, use that instead. And let me know the result.
    – Robert hue
    Oct 28, 2014 at 15:30
  • :Nope, this does not work, they are still not getting sorted by "completeness"....I#ve build a workaround: I write them all into anrray and sort it by subkey. Not the ebst solution but it works.
    – user998163
    Oct 28, 2014 at 15:45
  • If you are getting posts from this query but not sorting is not correct then remove order parameter from above code or change it to ASC
    – Robert hue
    Oct 28, 2014 at 15:48
  • I tried both but it fif not work.
    – user998163
    Oct 28, 2014 at 15:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.