Here’s a simplified combination of both @PieterGoosen and @ialocin fine answers by using the loop_start
hook:
add_action( 'loop_start', function( $q ) {
if( $q->is_main_query() && $q->is_category( 'car' ) )
usort( $q->posts, function( $a, $b ){
return -1 * has_tag( 'bmw', $a ) + 1 * has_tag( 'bmw', $b ); });
}, 10, 2 );
We could also use the same method for the the_posts
filter.
Update:
To sort posts on the home page, by the first category name, we can try for example:
add_action( 'loop_start', function( $q ) {
if( $q->is_main_query() && $q->is_home() )
usort( $q->posts, function( $a, $b ){
return strcasecmp(
get_the_category( $a->ID )[0]->name,
get_the_category( $b->ID )[0]->name
);
});
}, 10, 2 );
where we use strcasecmp
for case-insensitive string comparison.
Related Posts:
- WooCommerce – How to show specific category first-rest of products-specific category last
- How can I order all subcategories alphabetical independent of the parent categories?
- Configure query with multiple categories in a custom order?
- Get the children of the parent category
- How to Change the Categories Order in the Admin Dashboard?
- How do I get the category URL from get_the_category?
- Default WP Gallery – show only galleries of a certain category
- Rearranging posts based on categories
- WP_Query not looking at child category
- Randomise results from a category page?
- Display list of Sub-Categories and the posts they contain, within one main Category
- I want to order categories based upon the latest post time
- Remove child products from woocommerce category page [closed]
- Get a list of commas separated categories inside a loop
- WordPress Ordering Problem. How to fix ordering 1-10-100 issue?
- Order posts by category name
- Display one latest post from multiple categories
- How do I get the total number of categories in a list of search results?
- Check if post belongs to any category
- How do I display the posts from a custom post type on a category.php page?
- How to group the loop posts in a particular by author?
- Display products for a category, sorted by post_excerpt
- Exclude category from loop not working
- If newest post of category is newest post in general, skip first post of category
- a-z list, categories and sub categories in loop
- Is it possible to select and edit the way the most recent post from a certain category is displayed on the page?
- Arrange posts by date in front page
- Displaying multiple loops based off of category
- How to loop only categories without posts (+ show category featured image with acf) [closed]
- Additional featured post on first page
- Show all posts for a specific category/author
- Redirect category to url with /category
- create a page which displays a list of categories title+ short description?
- Enter a variable in the ‘category_name’ parameter
- Second level subcategory
- how to define category loop last class in the third
- Query Posts Exclude Entire Category
- Remove product category placeholder image (Woocomerce)
- Custom taxonomy/category order
- I want to display the all the posts that are inside a certain subcategory
- List posts in alphabetical order
- Inside a loop, how to change CSS class based on category name?
- Exclude current post from loop
- How to insert category list into post creation page, and retrieve chosen categories?
- Categories list loop – add separator every 3 categories [closed]
- Custom loop of a single category, cannot order by date. What am I missing?
- Pagination on category.php and tag.php not working
- Trying to get variables in hacked category dropdown
- How to do paging in the loop?
- category__not_in and id values from variable
- Do not show sub categories in the loop on archive-product.php
- Allow user to select categories that will display in post loop
- Insert HTML content in WP Query at specific point
- Simple way to style posts of a single category differently in the loop and in single
- Getting the Most Recent Posts from Multiple Categories
- Display category title and three latest post inside the category
- Have posts that belong to multiple categories, exclude some categories from homepage
- Show multiple categories in query using redux framework variable
- Get the ID of the current post’s child category
- multiple values in an array for category__and does not work with WP_Query
- Limit Loop to One Category
- Display post thumbnail for specific category outside the loop
- List post only under the category, exclude child category content
- Natural sort / ordering wp_dropdown_categories
- the_category() doesn’t working in wp_query loop
- Conditional category query breaking?
- How to get posts using category slug in ClassiPress?
- Category sticky latest
- Exclude posts without category from loop
- Show the number of the post
- Ordering terms whilst in loop
- Template category.php with page (no posts)
- How to create a category loop on my blog (ACF Plugin)
- Displaying Category in sidebar post widget but not in the loop on home
- Querying posts from two different categories while looping inside another loop
- Loop doesn’t work
- Display Taxonomies in loop with template args
- How to loop through a custom field for each post, & display?
- show category name before first posts in each category
- Loop within category’s posts
- Display one post on category.php: Wrong featured image
- Loop through posts of CatA and store value of CatB in separate array
- How to add an empty entry to masonry?
- WordPress Numeric Pagination with Query String [duplicate]
- Loops in category description
- Several loop in search result
- Printing direct descendants of a category with WP_Query
- Category slug in in loop always the same?
- how to get number of posts in a category on a specific date
- Show posted on date only for posts in a certain category
- 2 loops in archive.php (one for each category)
- Detecting top parent category fails on home page
- Using pagination with multiple loops causes it to break
- How do I hide posts in a category from all listings but still allow the posts to be viewed?
- How to create a custom loop ordered by Categories on a Page Template?
- Woocommerce – Display product category of product in Orders page
- How can I implement pagination on custom category page?
- Post list category plugin alphabetical order [closed]
- Order Categories by Character Count
- Get categories by title descendant