• With the help of below code i am able to output archive templet/loop with sorting order of the custom field ‘overall-rating’ correctly and i need the same sorting order in my Custom Taxonomy as well. Your support is greatly appreciated.

    Notes:
    Custom Post Slug is ‘reviews’
    Custom Taxonomy Slug is ‘review_categories’

    function sort_reviews_archive_loop($query) {
    if ($query->is_post_type_archive(‘reviews’) && $query->is_main_query()) {
    $query->set(‘order’, ‘DESC’);
    $query->set(‘meta_key’, ‘overall-rating’);
    $query->set(‘orderby’, ‘meta_value_num’);
    }
    }
    add_action(‘pre_get_posts’, ‘sort_reviews_archive_loop’);

    The page I need help with: [log in to see the link]

Viewing 3 replies - 1 through 3 (of 3 total)
  • Hi there,

    Can you do a var_dump of overall-rating?

    To be sure if it’s actually returning a integer rather than a string. This is important to know to determine if meta_value_num (integer) or meta_value (string) has to be used. ??

    Let us know.

    Example:

    function sort_reviews_archive_loop($query) {
      if ( $query->is_post_type_archive('reviews') && $query->is_main_query() ) {
        $query->set('order', 'DESC');
        $query->set('meta_key', 'overall-rating');
        $query->set('orderby', 'meta_value_num');
        $query->set('meta_query', array(
            array(
              'key' => 'overall-rating',
              'compare' => 'EXISTS'
            )
        ));
      }
    }
    add_action('pre_get_posts', 'sort_reviews_archive_loop');
    Thread Starter boban777

    (@boban777)

    Dear Elvin thanks for your kind reply, The meta value is number and even I tried above code but still no sorting seen. Please find below links for ref.

    Archive list – https://whichisgood.com/index.php/reviewss/

    Note- Rating Numbers are showing correctly in Descenting order

    Category List –
    https://whichisgood.com/index.php/review_categories/testing_1/

    Note- Not in Descenting order

    Note- ‘overall-rating’ number feild highlighted in green color

    Were you trying to apply the code to the page /review_categories/testing_1/?

    If yes, you’ll have to change the if condition because the condition of the current code is specific to reviews post type archive.

    /review_category/testing_1 is not a post type archive but a custom taxonomy term archive.

    If you’re dealing w/ custom taxonomy you’ll need is_tax() function instead. –
    https://developer.www.ads-software.com/reference/functions/is_tax/

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Custom Taxonomy Post List’ is closed to new replies.