post ordering question

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?

Topic listing Wordpress

Category Web


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.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.