WordPress.org

Forums

Coraline
Loop for custom post type (2 posts)

  1. jonathanmogrovejo
    Member
    Posted 1 year ago #

    All my post types show based on the date, newest first.

    But there is one CPT that I am trying to show in the order of comment_count. I have been on this for the past few days now and I have had no luck.

    For example, when I go into a custom taxonomy 'recom_cats' I want all the CPT's under that Taxonomy to show up in order of comment count DESC. Can someone please give me a hand with this or a push in the right direction?

    Thanks in advance.

  2. paulwpxp
    Font hero
    Posted 1 year ago #

    Not sure if this would work, but it doesn't hurt to try (change my_taxonomy_slug to yours).

    function mytheme_special_tax( $query ) {
    	if( $query->is_tax( 'my_taxonomy_slug' ) && $query->is_main_query() && !is_admin() ) {
    		$query->set( 'orderby', 'comment_count' );
    		$query->set( 'order', 'DESC' );
    	}
    }
    add_action( 'pre_get_posts', 'mytheme_special_tax' );

Topic Closed

This topic has been closed to new replies.

About this Theme

About this Topic