Navigation
This is archived content. Visit our new forum.

Tagged: 

  • Author
    Posts
  • #1022558
    daveshu1234
    Participant

    Hi,
    I want to change the order of the posts in just 1 category to alphabetical.

    I used the code below that I found on the site in my functions file, but it changes the order in ALL categories. What is the correct code to just change the order in category id “4”?

    
    add_action( 'pre_get_posts', 'x_category_order' );
    function x_category_order($query){
    	if(!$query->is_main_query()) return $query;
    	if(is_category('4')) $query->set( 'orderby', 'title' );$query->set( 'order', 'ASC' );
    	return $query;
    	
    }
    
    #1023179
    Rad
    Moderator

    Hi there,

    Thanks for writing in.

    Please change it into this,

    add_action( 'pre_get_posts', 'x_category_order' );
    function x_category_order($query){
    	if(!$query->is_main_query()) return $query;
    	if(is_category('4')) {
                $query->set( 'orderby', 'title' ); $query->set( 'order', 'ASC' );
            }
    	return $query;
    }

    Cheers!

    #1023918
    daveshu1234
    Participant

    Perfect – thank you!

    #1024161
    Joao
    Moderator

    You are welcome.

    Let us know if we can help you with anything else.

    Thanks

    Joao

  • <script> jQuery(function($){ $("#no-reply-1022558 .bbp-template-notice, .bbp-no-topic .bbp-template-notice").removeClass('bbp-template-notice'); }); </script>