[resolved] Getting child categories to sort posts like parent category (2 posts)

  1. mfapsj
    Posted 1 year ago #

    I have a file called category-2.php

    just before the Loop I have this code:


    It works fine. However I have a number of child (and even more grandchildren) categories for category 2. How can I tell all of them to sort posts the same way?

  2. mfapsj
    Posted 1 year ago #

    I managed to solve this by adding a function in functions.php which makes having a category template redundant. Not sure if someone is interested, but here it is:

    function orderby_additionaldate($query) {
        if (is_category( array( 2,57 ) ) && $query->is_main_query() || cat_is_ancestor_of(2, get_query_var('cat') ) && $query->is_main_query() || cat_is_ancestor_of(57, get_query_var('cat') ) && $query->is_main_query() ) {
            if ( $query->query_vars ) {
                $query->set( 'order', 'DESC' );
                $query->set( 'meta_key', 'additional_date' );
                $query->set( 'orderby', 'meta_value' );
    add_action( 'pre_get_posts', 'orderby_additionaldate' );

Topic Closed

This topic has been closed to new replies.

About this Topic