orderby
defaults to date, so we can always leave that out if we want to order by date. so the only thing we need to detect is if $_GET['sort']
is set and it’s equal to title.
here we set the query args for all queries, and just add on the orderby
if it’s title:
$args = array(
'category_name' => get_queried_object()->post_name,
'order' => 'DESC'
);
if( isset( $_GET['sort'] ) && "title" == $_GET['sort'] ){
$args['orderby'] = 'title';
}
$q = new WP_Query( $args );
Related Posts:
- How to display page content in a page template?
- Ignoring initial articles (like ‘a’, ‘an’ or ‘the’) when sorting queries?
- Sorting: custom query with orderby meta_value_num THEN by title
- Sort posts by category name and title
- How to get order of posts?
- How to get Page/Post Gallery attachment images in order they are set in backend using WP_Query()?
- WP Query orderby meta key natural sort?
- Sorting Posts by custom field
- Single page theme
- Single page theme that uses pages for the content
- Sorting meta_value as integer doesn’t work
- Trying to list out child pages with WP_Query
- Sort in WP_Query(), not filter? Is it possible?
- Woocommerce custom loop to show all the products [closed]
- Sort by meta key on archive page
- Query Set Order By Author
- What are the differences between “Latest Posts” and “Static Page”?
- WordPress: Keep order of query_posts list of post ID’s using post__in
- How to make orderby ‘meta_value_num’ OPTIONAL?
- Meta query with order by another custom field
- WP_Query doesn’t get sticky post at the top, when used in a page template
- How to orderby meta_value_num with dollar ($) sign
- How do I sort posts with multiple pages
- WP_Query posts by distance based on LAT & LNG in Database
- list pages using page.php and NOT page-{slug}.php
- Changing WP_Query params with url Query Var
- Get pagination working for custom loops within page templates
- How is WP_Query parsed to determine which template to request?
- WordPress Pagination changes my template
- Sort wordpress custom posts based on meta value
- WordPress Comments sort by custom meta key
- WP_query sorting can’t sort danish letters (æ, ø, å)
- add_filter(‘query_vars’) not working in custom template
- Help ordering Post loop by two meta values
- How to sort a custom wordpress query by combination of meta values?
- Gallery shortcode numerical sorting
- WP Group posts by year(desc) > month(desc) > date(asc)
- Does wp track views for posts?
- Sort WordPress Posts Meta value by Week not Day
- Pass $page_id to new WP_Query
- How to order WP_Query to group results?
- I am having problem sorting custom post type using WP_Query
- Pagination on template page for custom query redirecting to index.php
- Create a page template for “top rated posts” but show full content and not just a list
- WP_Query returns different results from get_posts() [closed]
- inserting content of 1 Post to in another with a template hierarchy
- Sorting search results with custom dropdown
- How to use a dropdown to filter posts by custom field
- Display all posts in a custom post type, grouped by a custom taxonomy. How to sort the posts alphabetically and the terms by ID?
- How to generate feed for custom template
- How do I sort this custom list of sticky posts
- WP_Query with multiple orderby NOT working with ASC and DESC, what’s wrong?
- WP Query and date format
- wp_query on page template not working (Genesis framework)
- Pagination for custom loop on custom page template is not displaying anything
- How to create Page templates for showing Blog posts in different layouts?
- orderby ignored by wp_query
- Page template doesn’t display simple HTML if inserted into other page?
- How to separate two columns from one content on a page template?
- What’s the best way to make a blog page on WordPress?
- Order (by ASC) posts with meta_key so posts without values are last
- How to use custom page for all posts with custom url, call another directory?
- How to sort store location by specific category order in WP store locations
- how to include orderby value that is empty?
- How to sort wordpress posts already selected by WP_QUERY
- Sort by price wpdb
- Custom Woocommerce Product Query
- How sort products by calculate value? ( custom post meta, price, option)
- Get authors list and sort them by recent posts
- Move some posts to end of sort order, even if there is a sort in the wp_Query already
- INNER JOIN custom tables – SQL Query
- Order by value of Custom Field using url string
- Pagination Shows Up But Won’t Work on Custom Gallery Page
- Order 2 meta_queries differently in WP_Query?
- Case insensitive ORDERBY in wpquery
- Sort posts on custom field AND after that sort on date?
- How to mix two orderby-parameters into one ordered result
- Sorting Posts by Taxonomy thats not within the query’s $args
- How to exclude posts by ID within a category/archive loop
- Ignoring ‘a’ when sorting posts
- Display three sequential posts on each page load, without repeating previous
- How can I use a specific wordpress page template if certain words are used in page title [closed]
- Combine WP_Query with array of custom data to single loop without breaking the pagination
- Wrap group of wp query posts to parent div by date/year
- Fetching $_POST from Page Template into functions.php
- Using URL variables on a custom WP_Query
- Sorting posts by meta values: 2 different orders
- Sorting is not working in WordPress WP_Query
- Pagination is not working on custom query inside a custom home page template
- How to sort a WP_Query array by post_name after an array_merge();
- Passing query variables to a custom page template
- Sorting: custom query with orderby meta_value_num THEN by title
- query_posts with sorting on a custom datestamp
- Sort Posts with custom meta key by default which is currently set as optional
- When importing a database any page with a wp_query in doesn’t work
- Having trouble using this post category query on multiple pages?
- wordpress sorting using array merge by price in ascending order but price with 0 must be show last
- Adding css tweak based on page template
- WordPress pagination returns the same posts
- Sort by multiple columns using get_posts