{"id":211,"date":"2020-06-29T12:25:32","date_gmt":"2020-06-29T12:25:32","guid":{"rendered":"https:\/\/codexjunction.com\/?p=211"},"modified":"2023-10-29T16:28:14","modified_gmt":"2023-10-29T16:28:14","slug":"wp-args-order-in-wordpress","status":"publish","type":"post","link":"https:\/\/codexjunction.com\/wp-args-order-in-wordpress\/","title":{"rendered":"WP Args Order in WordPress"},"content":{"rendered":"<p data-sourcepos=\"3:1-3:202\">WP Args Order in WordPress refers to the order in which arguments are passed to the <code>WP_Query()<\/code> class constructor. The order of the arguments is important, as it can determine the results of the query.<\/p>\n<p data-sourcepos=\"5:1-5:211\">The <code>WP_Query()<\/code> class is used to retrieve posts from the <a href=\"https:\/\/www.hostinger.in\/tutorials\/wordpress-database?ppc_campaign=google_search_generic_hosting_all&amp;bidkw=defaultkeyword&amp;lo=9151332&amp;gclid=Cj0KCQjwhfipBhCqARIsAH9msbl0wmB2CQYwwvHGOFwRaJWfKFuth0t91ObW16EnJdVdqzHXiFgbcSUaAiawEALw_wcB\" rel=\"nofollow\">WordPress database<\/a>. The constructor of the <code>WP_Query()<\/code> class takes a number of arguments, which can be used to filter and sort the results of the query.<\/p>\n<p data-sourcepos=\"7:1-7:57\">The following table lists the WP Args Order in WordPress:<\/p>\n<p data-sourcepos=\"9:1-28:69\">| Argument | Description | |&#8212;|&#8212;|&#8212;|<\/p>\n<p data-sourcepos=\"9:1-28:69\">| <code>orderby<\/code> | Specifies the field to order the posts by. |<\/p>\n<p data-sourcepos=\"9:1-28:69\">| <code>order<\/code> | Specifies the order in which to return the posts. |<\/p>\n<p data-sourcepos=\"9:1-28:69\">| <code>posts_per_page<\/code> | Specifies the number of posts to return. |<\/p>\n<p data-sourcepos=\"9:1-28:69\">| <code>offset<\/code> | Specifies the number of posts to skip over. |<\/p>\n<p data-sourcepos=\"9:1-28:69\">| <code>post_type<\/code> | Specifies the post type to return. |<\/p>\n<p data-sourcepos=\"9:1-28:69\">| <code>post_name<\/code> | Specifies the post name to return. |<\/p>\n<p data-sourcepos=\"9:1-28:69\">| <code>post_id<\/code> | Specifies the post ID to return. |<\/p>\n<p data-sourcepos=\"9:1-28:69\">| <code>post_parent<\/code> | Specifies the parent post ID to return. |<\/p>\n<p data-sourcepos=\"9:1-28:69\">| <code>author<\/code> | Specifies the author ID to return. |<\/p>\n<p data-sourcepos=\"9:1-28:69\">| <code>category_name<\/code> | Specifies the category name to return. |<\/p>\n<p data-sourcepos=\"9:1-28:69\">| <code>tag<\/code> | Specifies the tag slug to return. |<\/p>\n<p data-sourcepos=\"9:1-28:69\">| <code>taxonomy<\/code> | Specifies the taxonomy name to return. |<\/p>\n<p data-sourcepos=\"9:1-28:69\">| <code>meta_key<\/code> | Specifies the meta key to return. |<\/p>\n<p data-sourcepos=\"9:1-28:69\">| <code>meta_value<\/code> | Specifies the meta value to return. |<\/p>\n<p data-sourcepos=\"9:1-28:69\">| <code>date_query<\/code> | Specifies the date query to return. |<\/p>\n<p data-sourcepos=\"9:1-28:69\">| <code>tax_query<\/code> | Specifies the taxonomy query to return. |<\/p>\n<p data-sourcepos=\"9:1-28:69\">| <code>post_status<\/code> | Specifies the post status to return. |<\/p>\n<p data-sourcepos=\"9:1-28:69\">| <code>ignore_sticky_posts<\/code> | Specifies whether to ignore sticky posts. |<\/p>\n<p data-sourcepos=\"30:1-30:196\">The order in which the arguments are passed to the <code>WP_Query()<\/code> class constructor is important. The order in which the arguments are passed will determine the order in which the query is executed.<\/p>\n<p data-sourcepos=\"32:1-32:171\">For example, if you pass the <code>orderby<\/code> argument before the <code>post_type<\/code> argument, the posts will be ordered by the <code>orderby<\/code> field before the <code>post_type<\/code> field is filtered.<\/p>\n<p data-sourcepos=\"34:1-34:167\">If you pass the <code>post_type<\/code> argument before the <code>orderby<\/code> argument, the posts will be filtered by the <code>post_type<\/code> field before they are ordered by the <code>orderby<\/code> field.<\/p>\n<p data-sourcepos=\"36:1-36:166\">It is important to note that some arguments are mutually exclusive. For example, you cannot pass the <code>post_id<\/code> argument and the <code>post_name<\/code> argument at the same time.<\/p>\n<p data-sourcepos=\"38:1-38:11\"><strong>Example<\/strong><\/p>\n<p data-sourcepos=\"40:1-40:103\">The following example shows how to use the <code>WP_Query()<\/code> class to retrieve all posts of the type <code>post<\/code>:<\/p>\n<div class=\"code-block ng-star-inserted\">\n<div class=\"code-block-decoration header gmat-subhead-2 ng-star-inserted\">PHP<\/div>\n<pre><code class=\"code-container\" role=\"text\"><span class=\"hljs-variable\">$args<\/span> = <span class=\"hljs-keyword\">array<\/span>(\r\n  <span class=\"hljs-string\">'post_type'<\/span> =&gt; <span class=\"hljs-string\">'post'<\/span>,\r\n);\r\n\r\n<span class=\"hljs-variable\">$<span class=\"citation-0\">query<\/span><\/span><span class=\"citation-0\"> = <\/span><span class=\"hljs-keyword\"><span class=\"citation-0\">new<\/span><\/span><span class=\"citation-0\"> WP_Query( <\/span><span class=\"hljs-variable\"><span class=\"citation-0\">$args<\/span><\/span><span class=\"citation-0\"> );\r\n\r\n<\/span><span class=\"hljs-keyword\"><span class=\"citation-0\">if<\/span><\/span><span class=\"citation-0\"> ( <\/span><span class=\"hljs-variable\"><span class=\"citation-0\">$query<\/span><\/span><span class=\"citation-0\">-&gt;have_posts() ) {\r\n  <\/span><span class=\"hljs-keyword\"><span class=\"citation-0\">while<\/span><\/span><span class=\"citation-0\"> ( <\/span><span class=\"hljs-variable\"><span class=\"citation-0\">$query<\/span><\/span><span class=\"citation-0\">-&gt;have_posts() ) {\r\n    <\/span><span class=\"hljs-variable\"><span class=\"citation-0\">$query<\/span><\/span><span class=\"citation-0 citation-end-0\">-&gt;the_post();\r\n\r\n    the_title();\r\n    the_content();\r\n  }\r\n\r\n  wp_reset_postdata();<\/span>\r\n}\r\n<\/code><\/pre>\n<\/div>\n<p data-sourcepos=\"61:1-61:163\">The following example shows how to use the <code>WP_Query()<\/code> class to retrieve all posts of the type <code>post<\/code> and order them by the <code>post_title<\/code> field in ascending order:<\/p>\n<div class=\"code-block ng-star-inserted\">\n<div class=\"code-block-decoration header gmat-subhead-2 ng-star-inserted\">PHP<\/div>\n<pre><code class=\"code-container\" role=\"text\"><span class=\"hljs-variable\">$args<\/span> = <span class=\"hljs-keyword\">array<\/span>(\r\n  <span class=\"hljs-string\">'post_type'<\/span> =&gt; <span class=\"hljs-string\">'post'<\/span>,\r\n  <span class=\"hljs-string\">'orderby'<\/span> =&gt; <span class=\"hljs-string\">'post_title'<\/span>,\r\n  <span class=\"hljs-string\">'order'<\/span> =&gt; <span class=\"hljs-string\">'ASC'<\/span>,\r\n);\r\n\r\n<span class=\"hljs-variable\">$<span class=\"citation-1\">query<\/span><\/span><span class=\"citation-1\"> = <\/span><span class=\"hljs-keyword\"><span class=\"citation-1\">new<\/span><\/span><span class=\"citation-1\"> WP_Query( <\/span><span class=\"hljs-variable\"><span class=\"citation-1\">$args<\/span><\/span><span class=\"citation-1\"> );\r\n\r\n<\/span><span class=\"hljs-keyword\"><span class=\"citation-1\">if<\/span><\/span><span class=\"citation-1\"> ( <\/span><span class=\"hljs-variable\"><span class=\"citation-1\">$query<\/span><\/span><span class=\"citation-1\">-&gt;have_posts() ) {\r\n  <\/span><span class=\"hljs-keyword\"><span class=\"citation-1\">while<\/span><\/span><span class=\"citation-1\"> ( <\/span><span class=\"hljs-variable\"><span class=\"citation-1\">$query<\/span><\/span><span class=\"citation-1\">-&gt;have_posts() ) {\r\n    <\/span><span class=\"hljs-variable\"><span class=\"citation-1\">$query<\/span><\/span><span class=\"citation-1 citation-end-1\">-&gt;the_post();\r\n\r\n    the_title();\r\n    the_content();\r\n  }\r\n\r\n  wp_reset_postdata();<\/span>\r\n}\r\n<\/code><\/pre>\n<\/div>\n<p data-sourcepos=\"84:1-84:19\"><strong>Additional tips<\/strong><\/p>\n<ul data-sourcepos=\"86:1-91:0\">\n<li data-sourcepos=\"86:1-86:77\">You can use multiple arguments to filter and sort the results of the query.<\/li>\n<li data-sourcepos=\"87:1-87:81\">You can use the\u00a0<code>WP_Query()<\/code>\u00a0class to retrieve posts from any custom post type.<\/li>\n<li data-sourcepos=\"88:1-88:73\">You can use the\u00a0<code>WP_Query()<\/code>\u00a0class to retrieve posts from any taxonomy.<\/li>\n<li data-sourcepos=\"89:1-89:75\">You can use the\u00a0<code>WP_Query()<\/code>\u00a0class to retrieve posts from any date range.<\/li>\n<li data-sourcepos=\"90:1-91:0\">You can use the\u00a0<code>WP_Query()<\/code>\u00a0class to retrieve posts from any post status.<\/li>\n<\/ul>\n<p data-sourcepos=\"92:1-92:157\">If you are not sure how to use the <code>WP_Query()<\/code> class, you can consult the WordPress documentation or contact your <a href=\"https:\/\/codexjunction.com\/do-you-really-need-a-vps-best-wordpress-vps-hosting-compared\/\">WordPress hosting<\/a> provider for assistance.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>WP Args Order in WordPress refers to the order in which arguments are passed to the WP_Query() class constructor. The order of the arguments is important, as it can determine the results of the query. The WP_Query() class is used to retrieve posts from the WordPress database. The constructor of the WP_Query() class takes a number of arguments, which can be used to filter and sort the results of the query. The following table lists the WP Args Order in WordPress: | Argument | Description | |&#8212;|&#8212;|&#8212;| | orderby | Specifies the field to order the posts by. | | [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":876,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[51],"tags":[241,512,508,510,511,509,239,240],"class_list":["post-211","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-wordpress","tag-args-order","tag-a-online-course-web-tutorial","tag-learn-java-online","tag-learn-laravel-online","tag-learn-php-online","tag-learn-python-online","tag-wp-args-order","tag-wp_query"],"_links":{"self":[{"href":"https:\/\/codexjunction.com\/wp-json\/wp\/v2\/posts\/211","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codexjunction.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codexjunction.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codexjunction.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codexjunction.com\/wp-json\/wp\/v2\/comments?post=211"}],"version-history":[{"count":6,"href":"https:\/\/codexjunction.com\/wp-json\/wp\/v2\/posts\/211\/revisions"}],"predecessor-version":[{"id":2647,"href":"https:\/\/codexjunction.com\/wp-json\/wp\/v2\/posts\/211\/revisions\/2647"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codexjunction.com\/wp-json\/wp\/v2\/media\/876"}],"wp:attachment":[{"href":"https:\/\/codexjunction.com\/wp-json\/wp\/v2\/media?parent=211"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codexjunction.com\/wp-json\/wp\/v2\/categories?post=211"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codexjunction.com\/wp-json\/wp\/v2\/tags?post=211"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}