Fredmat
Fredmat

Reputation: 958

WP Query custom meta ordering

I'm trying to sort my custom post type by custom meta value (post_views).

<?php

$popular = new WP_Query(
    array(
        'post_type' => 'question',
        'post_status' => 'publish',
        'posts_per_page' => 5,
        'posts_per_archive_page' => 5,
        'nopaging' => true,
        'meta_key' => PREFIX . 'post_views',
        'orderby' => 'meta_value_num',
        'order' => 'DESC'
    )
);

?>

My ten posts are not ordered by post_views.

Any idea? Thank you.

Upvotes: 0

Views: 85

Answers (1)

Narendra
Narendra

Reputation: 464

Same problem was with me i have done it with custom query and its working for.

Hope this on will help you.

<?php
        $querystr = "
    SELECT $wpdb->posts.*
    FROM $wpdb->posts, $wpdb->postmeta
    WHERE $wpdb->posts.ID = $wpdb->postmeta.post_id
    AND $wpdb->postmeta.meta_key = 'year'
    AND $wpdb->posts.post_status = 'publish'
    AND $wpdb->posts.post_type = 'portfolio'
    ORDER BY $wpdb->postmeta.meta_value DESC";
    $pageposts = $wpdb->get_results($querystr, OBJECT);
        ?>

Thanks

Upvotes: 1

Related Questions