phpwordpresswordpress-themingcustom-wordpress-pageswordpress-admin

Wordpress how to order by order input value


When you create a post in Wordpress, there's a field called "order".

How can i make it work to for example, i put 5 and the post goes to the 5th position when displayed on the page?

I tried this but didn't work:

<?php
            $args = array(
               'post_type' => 'team',
               'posts_per_page' => 30,
               'orderby' => 'meta_value_num',
               'order' => 'ASC'
            );
            $the_query = new WP_Query($args);
            if ($the_query->have_posts()) : while ($the_query->have_posts()) : $the_query->the_post();
            ?>

Solution

  • When the post is saved, WordPress takes the value from the "order" field and saves it to the wp_posts.menu_order column within the database. With that being said, you'll want to set the "orderby" parameter to "menu_order".

    $args = array(
      'post_type' => 'team',
      'posts_per_page' => 30,
      'orderby' => 'menu_order',
      'order' => 'ASC'
    );