Marcin Polski
Marcin Polski

Reputation: 13

Sorting alphabetically on tag page Wordpress

I need to get my posts sorted alphabetically on tag page, in tag.php there are posts displayed on site, also no typical query, because this is tag site, so there is no way to sort it using query args, do you have any idea to do it easily or should I just store every post in array and then sort them?

Upvotes: 0

Views: 907

Answers (1)

xw48hf
xw48hf

Reputation: 447

Use filter to modify wp_query only on tag pages. You can hook into pre_get_posts to modify the query.

function sort_alphabetically_tag_page( $query ) {
       if ( $query->is_tag() && $query->is_main_query() ) {
          $query->set( 'orderby', 'title' );
       }
    }
add_action( 'pre_get_posts', 'sort_alphabetically_tag_page' );

Upvotes: 2

Related Questions