WordPress.org

Support

Support » How-To and Troubleshooting » Category Description for Ratings

Category Description for Ratings

  • I use the description box as a star rating per category. Only admin can rate the categories by inputting the numbers 1 to 5 in the description box.

    My Problem is:
    How could I call the description box in my array and display it in ascending order. Here’s my array.

    $cats = get_terms( 'branding', array(
    		'order'  => 'asc',
    		'number' => '6'
    	 ) );

    Hope someone can help me.
    Thanks in advance!

Viewing 15 replies - 1 through 15 (of 19 total)
  • Moderator keesiemeijer

    @keesiemeijer

    Try it with this in your functions:

    function query_term_ratings( $pieces, $taxonomies, $args) {
    
      $pieces['where'] .= " AND tt.description != ''";
      $pieces['orderby'] = " ORDER BY tt.description";
    
      return $pieces;
    }

    And this in your template file:

    <?php
    add_filter( 'terms_clauses', 'query_term_ratings', 10, 3 );
    get_terms( 'branding', array(
    		'order'  => 'asc',
    		'number' => '6'
    	 ) );
    remove_filter( 'terms_clauses', 'query_term_ratings', 10, 3 );
    
    if ( !( is_wp_error( $cats ) || empty( $cats ) ) ) {
    	foreach ( $cats as $cat ) {
    		$desc = absint( $cat->description );
    		if ( $desc ) {
    			echo 'category = ' . $cat->name;
    			echo 'rating = ' . $desc;
    		}
    	}
    }
    ?>

    btw:
    consider creating a child theme instead of editing your theme directly – if you upgrade the theme all your modifications will be lost.

    It’s f*****g awesome. It works.
    Thank for the code keesiemeijer

    I just added $cats before the get_terms

    <?php
    add_filter( 'terms_clauses', 'query_term_ratings', 10, 3 );
    $cats = get_terms( 'branding', array(
    		'order'  => 'asc',
    		'number' => '6'
    	 ) );
    remove_filter( 'terms_clauses', 'query_term_ratings', 10, 3 );
    
    if ( !( is_wp_error( $cats ) || empty( $cats ) ) ) {
    	foreach ( $cats as $cat ) {
    		$desc = absint( $cat->description );
    		if ( $desc ) {
    			echo 'category = ' . $cat->name;
    			echo 'rating = ' . $desc;
    		}
    	}
    }
    ?>

    What if I’m going to use this in loop. I tried everything to insert the above code in loop but it didn’t work. please help me.

    if ( have_posts() ) :
     while ( have_posts() ) : the_post();
    endwhile;
    endif;
    Moderator keesiemeijer

    @keesiemeijer

    What are you trying to do, show the categories (and ratings) assigned to the posts in the loop?

    Yes, I want to show the branding categories and ratings in the loop. The loop is sorted by ratings.

    Moderator keesiemeijer

    @keesiemeijer

    Try it with this inside the loop:

    <?php
    // get the post terms
    $post_terms = get_the_terms( $post->ID, 'branding' );
    
    if ( $post_terms ) {
    	// get all term ids
    	$include = wp_list_pluck( $post_terms, 'term_id' );
    
    	add_filter( 'terms_clauses', 'query_term_ratings', 10, 3 );
    	// get the terms in the correct order
    	$cats = get_terms( 'branding', array(
    			'order'  => 'asc',
    			'include' => $include,
    		) );
    	remove_filter( 'terms_clauses', 'query_term_ratings', 10, 3 );
    
    	if ( !( is_wp_error( $cats ) || empty( $cats ) ) ) {
    		foreach ( $cats as $cat ) {
    			$desc = absint( $cat->description );
    			if ( $desc ) {
    				echo 'category = ' . $cat->name;
    				echo 'rating = ' . $desc;
    			}
    		}
    	}
    }
    ?>

    Moderator keesiemeijer

    @keesiemeijer

    Or try it with this inside the loop (one less query to the database):

    <?php
    // get the post terms
    $post_terms = get_the_terms( $post->ID, 'branding' );
    
    if ( $post_terms ) {
    
    	$post_terms = array_values( $post_terms );
    	$include = array();
    
    	// terms that have a numeric description bigger than 0
    	foreach ( $post_terms as $key => $term ) {
    		$desc = absint( $term->description );
    		if ( $desc ) {
    			$include[$key] = $desc;
    		} else {
    			unset( $post_terms[ $key ] );
    		}
    	}
    
    	if ( $post_terms ) {
    
    		// sort the terms
    		// SORT_ASC - sort items ascendingly.
    		// SORT_DESC - sort items descendingly.
    		array_multisort( $include, SORT_ASC, $post_terms );
    
    		foreach ( $post_terms as $term ) {
    			echo 'category = ' . $term->name;
    			echo 'rating = ' . $term->description;
    		}
    
    	}
    }
    ?>

    Hi,keesiemeijer
    The loop works on my part except the sort items for the ratings.
    The sorting couldn’t change whether I use SORT_DESC or SORT_ASC.

    The rating always like this.
    CategoryName, rating = 2.1
    CategoryName, rating = 3.3
    CategoryName, rating = 4.45

    I would like to be like this
    The rating always like this.
    CategoryName, rating = 4.45
    CategoryName, rating = 3.3
    CategoryName, rating = 2.1

    Thanks keesiemeijer in advance

    Moderator keesiemeijer

    @keesiemeijer

    That’s strange. Try changing this:

    array_multisort( $include, SORT_ASC, $post_terms );

    to this:

    array_multisort( $include, SORT_ASC, $post_terms );
    $post_terms = array_reverse($post_terms);

    Still no luck.
    sorry I’m not really good in programming.

    This is the code, still didn’t work

    if ( have_posts() ) :
    while ( have_posts() ) : the_post();
    
    $post_terms = get_the_terms( $post->ID, 'branding' );
    
    if ( $post_terms ) {
    
    	$post_terms = array_values( $post_terms );
    	$include = array();
    
    	// terms that have a numeric description bigger than 0
    	foreach ( $post_terms as $key => $term ) {
    		$desc = absint( $term->description );
    		if ( $desc ) {
    			$include[$key] = $desc;
    		} else {
    			unset( $post_terms[ $key ] );
    		}
    	}
    
    	if ( $post_terms ) {
    
    		// sort the terms
    		// SORT_ASC - sort items ascendingly.
    		// SORT_DESC - sort items descendingly.
    		array_multisort( $include, SORT_ASC, $post_terms );
    		$post_terms = array_reverse($post_terms);
    
    		foreach ( $post_terms as $term ) {
    			echo  $term->name;
    			echo ' rating = ' . $term->description;
    			echo ' - ';
    			the_title();
    			echo '<Br>';
    		}
    
    	}
    }
    
    endwhile;
    endif;
    Moderator keesiemeijer

    @keesiemeijer

    Try it with this in your theme’s functions.php

    function sort_category_rating_terms($item1,$item2) {
    
        if ($item1->description == $item2->description) return 0;
    
        return ($item1->description < $item2->description) ? 1 : -1;
    }

    And this inside the loop:

    <?php
    // get the post terms
    $post_terms = get_the_terms( $post->ID, 'category' );
    
    if ( $post_terms ) {
    
    	$post_terms = array_values( $post_terms );
    
    	foreach ( $post_terms as $key => $term ) {
    		$desc = absint( $term->description );
    		if ( !$desc ) {
    			unset($post_terms[ $key ]);
    		}
    	}
    
    	if ( $post_terms ) {
    		usort($post_terms,'sort_category_rating_terms');
    
    		foreach ( $post_terms as $term ) {
    			echo 'category = ' . $term->name;
    			echo 'rating = ' . $term->description;
    		}
    
    	}
    }
    ?>

    Sir still not working, the arrangement are still the same.
    The result is sorted by Category name not by ratings.

    Moderator keesiemeijer

    @keesiemeijer

    I’ve got this working on my test site, so I’m not sure what is causing this not to work.

    You do see the output of “echo ' rating = ' . $term->description;“?

    try:
    – deactivating all plugins to see if this resolves the problem? If this works, re-activate the plugins one by one until you find the problematic plugin(s).

    I did what you said, I deactivated all the plugins but the sorting are still the same. Yes the output of the rating is visible in my page but the sorting is not correct.

    BTW I put this loop inside the taxonomy page. coz I’m using the taxonomy picker so the result should display in the taxonomy page.

    wordpress 3.5.2

Viewing 15 replies - 1 through 15 (of 19 total)
  • The topic ‘Category Description for Ratings’ is closed to new replies.
Skip to toolbar