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 check if I’m on the last page of posts?
- Order get_terms() By Custom Field
- Should category.php and The Loop be used if the query needs to be customizable?
- How to Change the Categories Order in the Admin Dashboard?
- Exclude the category from the WordPress loop
- How do I get the category URL from get_the_category?
- get_field not displaying my custom field value
- Default WP Gallery – show only galleries of a certain category
- Rearranging posts based on categories
- WP_Query not looking at child category
- Sort posts by tags in category pages
- Randomise results from a category page?
- Show div only if post is in specific category
- How can I custom order the results from wp_list_categories?
- How to show a category post to a specific registered user
- Display list of Sub-Categories and the posts they contain, within one main Category
- Loop with Dynamic Categories
- I want to order categories based upon the latest post time
- How can I create a loop to build slides based on multiple categories using Coda Slider
- 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?
- Categories list loop – add separator every year
- How would you create a “weekly” archive?
- How to exclude categories from recent posts, recent comments & category widgets?
- Listing all posts from current category on page
- 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
- Listing Parent, Child and GrandChild Categories and then the PostTitles on Page Template !
- Category archive page – loop through posts of certain tag (with pagination) – pre_get_posts
- If newest post of category is newest post in general, skip first post of category
- a-z list, categories and sub categories in loop
- Why would in_category only return one post in a specific category?
- Is it possible to select and edit the way the most recent post from a certain category is displayed on the page?
- Custom order categories in admin dashboard
- 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
- Class active for Archive link – looped out
- Taxonomy name repeating when showing 2 posts from each category
- How to redefine the sorting of product categories by menu_order?
- Display the last post by each category?
- Remove product category placeholder image (Woocomerce)
- Order categories by name or view count
- Counter in loop in foreach
- Multiple Loops on category.php with pagination on last loop
- How to exclude the last post from a category(featured for example) from the main loop?
- Custom taxonomy/category order
- I want to display the all the posts that are inside a certain subcategory
- Order by category titles
- wp e-commerce group product by category [closed]
- List posts in alphabetical order
- ajax for filtering posts by category in wordpress loops
- 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]
- Limit get_categories to show each category once
- Display single category name in woocommerce loop
- Show Posts from Single Category Not Working
- Is there a reason I cannot get the current category in a loop?
- Category archive is displaying all posts rather than the specific category
- Custom loop of a single category, cannot order by date. What am I missing?
- Pagination on category.php and tag.php not working
- new WP_Query all post in a category inside the loop
- Trying to get variables in hacked category dropdown
- Problem with different query loops (and “main loop”) on category template page!
- How to do paging in the loop?
- How to add orderby parameter for sorting on category.php
- category__not_in and id values from variable
- How to change the order of the subcategory and category in a post?
- How to loop custom post_type from (a) specific category(ies)?
- Do not show sub categories in the loop on archive-product.php
- How to display the parent category if only the child category has been chosen
- Category tags with comma’s
- Display posts of certain categories to specific user roles
- Allow user to select categories that will display in post loop
- Insert HTML content in WP Query at specific point
- How do order product categories – on a parent category page – in Woocommerce?
- Simple way to style posts of a single category differently in the loop and in single
- Remove any product that is featured from regular display loop [WooCommerce]
- Two Sections on Woocommerce Catergory Page [closed]