- This topic has 3 replies, 2 voices, and was last updated 6 years, 3 months ago by Tom.
-
AuthorPosts
-
January 15, 2018 at 5:06 pm #471843Denise
Hi GP Friends,
I have a widget in the footer that shows the most recent posts. I also have some code in the functions.php that controls the sort order for certain categories. Instead of using the default order by date descending, it sorts certain categories by Title. I noticed that this is affecting the sort order of the footer widget for recent posts. If the user is on a category archive page that sorts by title, then the recent posts footer also sorts by title. Can you suggest some code for how I might control the footer widget sort order separately from the category archive query?Many thanks,
DeniseHere is the sort code I have in the functions.php:
/* Change query to sort by title for selected categories */ add_action( 'pre_get_posts', 'mdp_modify_query_order' ); function mdp_modify_query_order( $query ) { $cat_title = single_cat_title("",false); if ( is_search() || is_category( array('robots','websites','organizations','journals','blogs','collections')) || stripos( $cat_title,'robot') > 0) { $query->set( 'orderby', 'title' ); $query->set( 'order', 'ASC' ); } }
January 16, 2018 at 9:31 am #472411TomLead DeveloperLead DeveloperHi there,
You should be able to check if it’s the main query or not in your conditional:
add_action( 'pre_get_posts', 'mdp_modify_query_order' ); function mdp_modify_query_order( $query ) { $cat_title = single_cat_title("",false); if ( ! $query->is_main_query() && ( is_search() || is_category( array('robots','websites','organizations','journals','blogs','collections')) || stripos( $cat_title,'robot') > 0 ) ) { $query->set( 'orderby', 'title' ); $query->set( 'order', 'ASC' ); } }
January 20, 2018 at 9:32 am #475768DeniseThanks so much, Tom. That did the trick.
P.S.
As you have rightly heard so often, but it can’t be said too many times, your Generate Press theme is absolutely wonderful. This is exactly the tool I needed to build a lightweight resource directory. I searched everywhere and tried many different themes and plug-ins, including starting with underscores, which was just a bit too much. I’m a database developer and unfortunately, don’t have time to do all the work needed to build and test a theme from scratch. Generate Press gives me just the right amount of control. It’s well tested with fantastic support. A million thanks, once again. You’ve made an incredible contribution to the WordPress Community.Best wishes,
DeniseJanuary 20, 2018 at 8:48 pm #476064TomLead DeveloperLead DeveloperWow, thank you so much Denise! I really appreciate the kind words 🙂
-
AuthorPosts
- You must be logged in to reply to this topic.