• Skip to primary navigation
  • Skip to content
  • Skip to primary sidebar
  • Skip to user navigation

PopWP

WordPress and Genesis Tutorials

  • Get Started
  • About
  • Archive
  • Services
  • Membership
  • My Account

Order Posts by $query in WordPress

Last Updated on June 13, 2019 Favorited: 0 times

Have you ever wished to set blog post to show by modified date? It should not be available by default in WordPress in Settings > Reading?

One can Order the archive post to show by various parameters with the help of action hook pre_get_posts (rewrite default loop) with the help of  wp_query and conditional tag.

Order Post by Modified Date

Use the following in functions.php of your child theme.

add_action ( 'pre_get_posts', function ( $query ) 
{
    if (    !is_admin()
         && $query->is_main_query()
         && (    $query->is_home() 
              || $query->is_category()
            )
    ) {
        $query->set( 'orderby', 'modified' );
    }
});

Here, parameter orderby is set to “modified”. Find all the available parameters and use with a custom $query and restrict it with Conditional Tags.

 

Categories: Free Content, WordPress Tutorials Tags: post order, pre_get_posts

Reader Interactions

Comments

  1. Fred says

    January 16, 2020 at 9:08 pm

    How do I also add costom field – so it would order Post with costom field + modified being first on top?

    • Aryan Raj says

      January 19, 2020 at 4:49 am

      You will need to create a custom loop to output custom fields.

Primary Sidebar

Search

WPEngine WordPress Hosting, Perfected.

Hosting You are looking for?.
Perfect solution for small business to global enterprise.

Learn more

StudioPress Genesis Theme Framework

The No.1 Theme Provider.
Creative, SEO rich Theme for all niche projects.

Learn more

Categories

  • Free Content
  • Genesis Tutorials
  • Premium Content
  • Snippets
  • What's New?
  • WordPress Tutorials

Tag Cloud

Archive Background Section blog canvas menu center logo columns conditional tags CSS CSS Grid custom Customizer custom post type Custom Post Types custom template Custom Widget effect eNews Extended Featured Image front-page Genesis Genesis Sample header right hero section Image Background js layout left menu Logo menu Navigation Menu newsletter post page related posts responsive menu search search widget Shrinking Logo site header slide in-out Stylesheet Template Utility Bar Video Background widgets WordPress

Built with Genesis Framework + WordPress by Aryan Raj

  • Contact
  • FAQ
  • Disclaimer
  • Privacy Policy
  • Copyright Policy
  • Terms of Service