is it possible to order posts while keeping the standard Wordpress loop intact (i.e. without having to create a whole new WP_Query?
By standard loop I mean:
<?php if ( have_posts() ) : ?>
<?php /* The loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
Can I specify the order within this code?
After activating it click into “Post Types Order” under settings and you can enable the types of posts you want the reorder interface to show up on. Then under that post type you will see a new menu called “Re-order.” You can then drag and drop the posts within according to the order you want them to appear in.
The WordPress Loop is used by WordPress to publish content. In its simplest form, the WordPress Loop simply checks if there are posts or pages to be displayed and then displays them. By using multiple loops and modifying loops using the WP_Query class, theme developers can design complex website themes.
As documented at query_posts
function page:
It is strongly recommended that you use the
pre_get_posts
filter instead, and alter the main query by checkingis_main_query
.
You can add a new action on pre_get_posts
in your theme functions.php
file, like:
function homepage_posts($query)
{
if ($query->is_home() && $query->is_main_query())
{
$query->set( 'orderby', 'title' );
}
}
add_action('pre_get_posts', 'homepage_posts');
wp_reset_query() is the way to go
Example snippet
<?php query_posts(array('orderby'=>'title','order'=>'DESC'));
if ( have_posts() ) :
while ( have_posts() ) : the_post(); ?>
<a href="<?php the_permalink() ?>"><?php the_title() ?></a><br /><?php
endwhile;
endif;
wp_reset_query();
But keep in mind: query_posts() will change your main query and is not recommended. Only use if absolutely necessary (see query_posts: Caveats). Creating a new instance of WP_Query or get_posts() is preferred for secondary loops.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With