Hello! Where and how does WP make an array of all posts (where is the query to the database)? There is such a code to display all posts:

<?php while (have_posts()) : the_post(); ?> <?php appthemes_before_post(); ?> <?php get_template_part('content', APP_POST_TYPE); ?> <?php appthemes_after_post(); ?> <?php endwhile; ?> <?php appthemes_after_endwhile(); ?> <?php else: ?> <?php appthemes_loop_else(); ?> <?php endif; ?> 

I need to redo the request to the database to change the order of output posts

    1 answer 1

    Use the pre_get_posts hook. Here is an example with sorting by user field and date:

     add_action( 'pre_get_posts', 'custom_order' ); function custom_order( $query ) { if ( ! is_admin() && $query->is_main_query() ) { $query->set('meta_key', 'my-custom-field' ); $query->set('orderby', array('meta_value' => 'ASC', 'date' => 'DESC')); } return $query; } 

    Of course, you can specify your sorting method. The code must be inserted into the functions.php file of your theme.