0

Having a Custom Post Type loop like this, I am able to return the result on page BUT I am not able to set Order By of display using my own Custom Field.

As you can see here

$topic_number = get_post_custom_values($key = 'topic_order');
echo $topic_number[0];

I am able to get the value of Custom Field and echo it to page but the Order System not doing the job on ordering them. Can you please let me know how to fix this?

$args = array(
    'tax_query' => array(
        array(
            'taxonomy' => 'topic',
            'field'    => 'slug',
            'terms'    => 'movie',
        ),
    ),
        'post_type' => 'essay',
        'orderby' => 'topic_number',
        'order' => 'DESC',
);

    $loop = new WP_Query( $args ); 
        
    while ( $loop->have_posts() ) : $loop->the_post(); 
$topic_number = get_post_custom_values($key = 'topic_order');

echo $topic_number[0];
        print the_title() ."<br />"; 
        the_excerpt(); 
    endwhile;
1

Your query args are not quite correct. When you want to order by a custom field, you need to specify a meta_key argument, and set your orderby to the kind of ordering you need. For example:

$args = array(
    'tax_query' => array(
        array(
            'taxonomy' => 'topic',
            'field'    => 'slug',
            'terms'    => 'movie',
        ),
    ),
    'post_type' => 'essay',
    'meta_key'  => 'topic_number',
    'orderby'   => 'meta_value_num',
    'order'     => 'DESC',
);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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