{"id":270,"date":"2020-09-10T08:59:22","date_gmt":"2020-09-10T08:59:22","guid":{"rendered":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/http\/code.oxyset.com\/?p=270"},"modified":"2026-10-10T20:52:06","modified_gmt":"2026-10-10T20:52:06","slug":"change-the-default-loop-with-function-php","status":"publish","type":"post","link":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/change-the-default-loop-with-function-php\/","title":{"rendered":"Change the Main Loop Order in WordPress With pre_get_posts"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">WordPress lists posts newest first on the blog page and in every archive. For a course, a story told in parts or a changelog you want the oldest post first, or an order by title.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This snippet changes the WordPress main loop order on the blog page and the archives with <code>pre_get_posts<\/code>. The theme templates stay untouched, and pagination keeps working.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The snippet<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">&lt;?php\n\/**\n * Change the order of the main post loop on the blog and archive pages.\n *\/\nfunction dpc_loop_order() {\n\t\/\/ ===== Config =====\n\t\/\/ Oldest posts first. Other examples:\n\t\/\/ array( 'title' =&gt; 'ASC' ) or array( 'menu_order' =&gt; 'ASC', 'date' =&gt; 'DESC' ).\n\t$orderby = array( 'date' =&gt; 'ASC' );\n\t\/\/ ===== End of config =====\n\n\treturn (array) apply_filters( 'dpc_loop_order', $orderby );\n}\n\nadd_action( 'pre_get_posts', 'dpc_change_loop_order' );\n\nfunction dpc_change_loop_order( $query ) {\n\tif ( is_admin() || ! $query-&gt;is_main_query() ) {\n\t\treturn;\n\t}\n\t\/\/ Only the blog page and archives, not search results or feeds.\n\tif ( ! ( $query-&gt;is_home() || $query-&gt;is_archive() ) || $query-&gt;is_search() || $query-&gt;is_feed() ) {\n\t\treturn;\n\t}\n\t\/\/ Keep an order a visitor or another plugin chose, and WooCommerce's shop sorting.\n\tif ( '' !== $query-&gt;get( 'orderby' ) || '' !== $query-&gt;get( 'wc_query' ) ) {\n\t\treturn;\n\t}\n\n\t$query-&gt;set( 'orderby', dpc_loop_order() );\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">How it works<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>pre_get_posts<\/code> runs before WordPress queries the database, so changing <code>orderby<\/code> there changes the posts, the page count and the pagination in one go. Running a second query in the template does not.<\/li>\n\n\n<li><code>$query-&gt;is_main_query()<\/code> limits it to the page&#8217;s own loop. Widgets, related posts and Query Loop blocks with their own settings keep their order.<\/li>\n\n\n<li>It applies to the blog page and archives (category, tag, author, date, custom post type and taxonomy archives), not to search results, feeds or wp-admin. The old version ran on every main query, so it also reordered the post lists in wp-admin and the search results.<\/li>\n\n\n<li>If the URL already has an <code>orderby<\/code> (<code>?orderby=title<\/code>) or WooCommerce runs its shop query, the snippet leaves the order alone.<\/li>\n\n\n<li>The old version ordered by <code>menu_order<\/code>. Posts have no order field in the editor, so every post had 0 and the order was effectively random. The array form <code>array( 'menu_order' =&gt; 'ASC', 'date' =&gt; 'DESC' )<\/code> gives a tie-breaker when you do use it.<\/li>\n\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Add it with Scripts Organizer<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In <a href=\"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/dplugins.com\/downloads\/scripts-organizer\/\">Scripts Organizer<\/a> go to <strong>Code Blocks \u2192 Add New<\/strong> and give the block a name. Set <strong>Trigger location<\/strong> to <strong>Everywhere<\/strong>, tick <strong>PHP<\/strong> under <strong>Script location<\/strong>, paste the code into the PHP editor and publish. Leave <strong>Action\/Hook<\/strong> empty. <code>pre_get_posts<\/code> fires after Scripts Organizer has loaded the code.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The same code also works unchanged in a small plugin or an mu-plugin.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Customise it<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Alphabetical: <code>array( 'title' =&gt; 'ASC' )<\/code>.<\/li>\n\n\n<li>Only one archive: add a condition such as <code>if ( ! $query-&gt;is_category( 'course' ) ) { return; }<\/code> or <code>$query-&gt;is_post_type_archive( 'event' )<\/code>.<\/li>\n\n\n<li>Custom order for a custom post type: register it with <code>'supports' =&gt; array( 'page-attributes' )<\/code>, set the <strong>Order<\/strong> field per post and use <code>array( 'menu_order' =&gt; 'ASC', 'date' =&gt; 'DESC' )<\/code>.<\/li>\n\n\n<li>To restrict which posts appear rather than their order, see <a href=\"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/limit-search-to-post-type\/\">Limit search to post type<\/a> for a <code>pre_get_posts<\/code> example on search.<\/li>\n\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Gotchas<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Sticky posts are still shown first on the blog page. Unstick them, or set <code>ignore_sticky_posts<\/code> to <code>true<\/code> in the same callback.<\/li>\n\n\n<li>Block themes: a Query Loop block with <strong>Inherit query from template<\/strong> follows this order, one with its own settings does not.<\/li>\n\n<\/ul>\n\n","protected":false},"excerpt":{"rendered":"<p>Change the order of the WordPress blog and archive loop, for example oldest posts first, with pre_get_posts and without touching templates.<\/p>\n","protected":false},"author":1,"featured_media":46822,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"slim_seo":{"title":"Change the Main Loop Order in WordPress With pre_get_posts","description":"Change the order of the WordPress blog and archive loop, for example oldest posts first, with pre_get_posts and without touching templates."},"_slim_seo_primary_term_category":0,"_slim_seo_primary_term_post_tag":0,"footnotes":""},"categories":[3],"tags":[],"dpc_library":[204],"class_list":["post-270","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-wordpress"],"_links":{"self":[{"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/posts\/270","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/comments?post=270"}],"version-history":[{"count":1,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/posts\/270\/revisions"}],"predecessor-version":[{"id":46821,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/posts\/270\/revisions\/46821"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/media\/46822"}],"wp:attachment":[{"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/media?parent=270"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/categories?post=270"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/tags?post=270"},{"taxonomy":"dpc_library","embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/dpc_library?post=270"}],"curies":[{"name":"wp","href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/api.w.org\/{rel}","templated":true}]}}