{"id":165030,"date":"2011-01-15T08:00:00","date_gmt":"2011-01-15T08:00:00","guid":{"rendered":"https:\/\/www.weixiaoduo.com\/plugins\/2024\/02\/22\/sort-query-posts\/"},"modified":"2012-09-04T20:11:00","modified_gmt":"2012-09-04T20:11:00","slug":"sort-query-posts","status":"publish","type":"post","link":"https:\/\/www.weixiaoduo.com\/plugins\/sort-query-posts\/","title":{"rendered":"Sort Query Posts"},"content":{"rendered":"<p>A really simple and lightweight plugin (73 lines of code \u2013 comments included) to sort posts on-the-fly without making a new query to the database (improves performance).<\/p>\n<h4>Features:<\/h4>\n<ul>\n<li>Supports <strong>all <code>order_by<\/code> and <code>order<\/code> values of the <a href=\"https:\/\/codex.wordpress.org\/Function_Reference\/query_posts#Orderby_Parameters\" rel=\"nofollow ugc\">query_posts function<\/a><\/strong> except <code>meta_value<\/code> and <code>meta_value_num<\/code> (which require a database query).<\/li>\n<li>Supports <strong>changing the order of all types of posts<\/strong>, including custom post type posts and custom post type \u201carchive\u201d posts.<\/li>\n<\/ul>\n<h4>Documentation<\/h4>\n<p>This plugin adds the function <code>sort_query_posts_by(string $order_by [, string $order])<\/code> to the global context.<br \/>\nThe second <code>$order<\/code> parameter is optional. Its default value is <code>asc<\/code> (ascending order).<\/p>\n<p><strong>Call this function before <a href=\"https:\/\/codex.wordpress.org\/The_Loop\" rel=\"nofollow ugc\">the loop<\/a> to change how posts are ordered.<\/strong><br \/>\nAfter calling this function you can show the posts as you normally would.<\/p>\n<p>You can sort posts by:<\/p>\n<ul>\n<li>author<\/li>\n<li>comment_count<\/li>\n<li>date<\/li>\n<li>id<\/li>\n<li>menu_order<\/li>\n<li>modified<\/li>\n<li>parent<\/li>\n<li>title<\/li>\n<\/ul>\n<p>This is specially useful in two cases:<\/p>\n<ul>\n<li>When you need to reorder the posts returned by the query that WordPress creates from your given URL. Custom post type \u201carchive\u201d posts are a great example of this case.<\/li>\n<li>When you need the posts returned by your customized query (e.g. <code>query_posts()<\/code>) to be shown more than once on the same page and ordered differently.<\/li>\n<\/ul>\n<p><strong>Examples:<\/strong><\/p>\n<pre><code><?php sort_query_posts_by('title', 'desc'); ?>\n<\/code><\/pre>\n<p>The example above will sort posts by their title in descending order without making a new query to the database.<br \/>\nThis way sorting is performance friendly.<\/p>\n<pre><code><?php sort_query_posts_by('ID'); ?>\n<\/code><\/pre>\n<p>The example above will sort posts by their ID in ascending order.<\/p>\n<pre><code><?php sort_query_posts_by('rand'); ?>\n<\/code><\/pre>\n<p>The example above will sort posts randomly. When sorting randomly <code>$order<\/code> is ignored.<\/p>\n<p>Plugin developed by T\u00fabal Mart\u00edn at <a href=\"http:\/\/www.margenn.com\" rel=\"nofollow ugc\">www.margenn.com<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sort posts on-the-fly without making a new SQL query<\/p>\n","protected":false},"author":65,"featured_media":349179,"comment_status":"open","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"slim_seo":{"title":"Sort Query Posts - \u8587\u6653\u6735\u63d2\u4ef6\u76ee\u5f55","description":"Sort posts on-the-fly without making a new SQL query"},"footnotes":""},"categories":[1],"tags":[42525,71,1361,8628,5721],"class_list":["post-165030","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-uncategorized","tag-order_by","tag-post","tag-query","tag-reorder","tag-sort"],"_links":{"self":[{"href":"https:\/\/www.weixiaoduo.com\/plugins\/wp-json\/wp\/v2\/posts\/165030","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.weixiaoduo.com\/plugins\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.weixiaoduo.com\/plugins\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.weixiaoduo.com\/plugins\/wp-json\/wp\/v2\/users\/65"}],"replies":[{"embeddable":true,"href":"https:\/\/www.weixiaoduo.com\/plugins\/wp-json\/wp\/v2\/comments?post=165030"}],"version-history":[{"count":0,"href":"https:\/\/www.weixiaoduo.com\/plugins\/wp-json\/wp\/v2\/posts\/165030\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.weixiaoduo.com\/plugins\/wp-json\/wp\/v2\/media\/349179"}],"wp:attachment":[{"href":"https:\/\/www.weixiaoduo.com\/plugins\/wp-json\/wp\/v2\/media?parent=165030"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.weixiaoduo.com\/plugins\/wp-json\/wp\/v2\/categories?post=165030"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.weixiaoduo.com\/plugins\/wp-json\/wp\/v2\/tags?post=165030"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}