WordPress.org

Ready to get started?Download WordPress

Forums

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

  1. mfapsj
    Member
    Posted 7 months ago #

    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?

  2. mfapsj
    Member
    Posted 7 months 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' );

Reply

You must log in to post.

About this Topic