Support » Fixing WordPress » Getting child categories to sort posts like parent category

  • Resolved mfapsj

    (@mfapsj)


    I have a file called category-2.php

    just before the Loop I have this code:

    query_posts('cat=2&order=desc&orderby=meta_value&meta_key=additional_date');

    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?

Viewing 1 replies (of 1 total)
  • 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' );
Viewing 1 replies (of 1 total)
  • The topic ‘Getting child categories to sort posts like parent category’ is closed to new replies.