-2

I have a custom type posts and I need to make it ordering by something different way. not by date, id, names.

Is there any way to put ordering numbers and listing by the numbers?

Because we have a several new posts should be placed 2nd, 3rd,...etc but I don't it's easy to do.

So I was thinking if there is any place to put ordering numbers and order by the numbers would be the best.

Is there anyway or plugin for it?

2

1 Answer 1

1

Use a custom field named 'order' in each post. Remove the field from posts you do not want to display. Then call the WP_Query object to sort the order field numerically (meta_value_num) skipping any values less or equal to 0 (like in the meta_query below).

$posts = new WP_Query( array(
    'orderby'    => 'meta_value_num',
    'meta_key'   => 'order',
    'order'      => 'ASC',
    'meta_query' => array(
        array(
            'key'     => 'order',
            'value'   => 0,
            'compare' => '>',
        ),
    ),
) );

if ( $posts->have_posts() ) {
    echo '<ul>';
    while ( $posts->have_posts() ) {
        $posts->the_post();
        printf( '<li><a href="%s">%s (%s)</a></li>', get_permalink(), get_the_title(), get_post_meta( get_the_ID(), 'order', true ) );
    }
    echo '</ul>';
}

wp_reset_postdata();

In this example, the number in parenthesis in each link is the value in the 'order' custom field.

1
  • this is what I have been looking for. I am using the plugin, but I will work on it very soon :)
    – pulla
    Oct 14, 2013 at 3:16

Your Answer

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

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