WordPress.org

Ready to get started?Download WordPress

Forums

Category Description for Ratings (20 posts)

  1. rhoi
    Member
    Posted 1 year ago #

    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!

  2. keesiemeijer
    moderator
    Posted 1 year ago #

    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.

  3. rhoi
    Member
    Posted 1 year ago #

    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;
    		}
    	}
    }
    ?>
  4. rhoi
    Member
    Posted 1 year ago #

    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;
  5. keesiemeijer
    moderator
    Posted 1 year ago #

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

  6. rhoi
    Member
    Posted 1 year ago #

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

  7. keesiemeijer
    moderator
    Posted 1 year ago #

    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;
    			}
    		}
    	}
    }
    ?>
  8. keesiemeijer
    moderator
    Posted 1 year ago #

    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;
    		}
    
    	}
    }
    ?>
  9. rhoi
    Member
    Posted 1 year ago #

    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

  10. keesiemeijer
    moderator
    Posted 1 year ago #

    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);
  11. rhoi
    Member
    Posted 1 year ago #

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

  12. rhoi
    Member
    Posted 1 year ago #

    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;
  13. keesiemeijer
    moderator
    Posted 1 year ago #

    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;
    		}
    
    	}
    }
    ?>
  14. rhoi
    Member
    Posted 1 year ago #

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

  15. keesiemeijer
    moderator
    Posted 1 year ago #

    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).

  16. rhoi
    Member
    Posted 1 year ago #

    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

  17. rhoi
    Member
    Posted 1 year ago #

    I noticed that since we did this in the loop our output are sorted by Category Name Ascending. Not sorted by ratings. weird right.

  18. keesiemeijer
    moderator
    Posted 1 year ago #

    Can you post the full code of the template file, see the Forum Welcome on how to post large excerpts of code.

    Is taxonomy picker a plugin?

  19. rhoi
    Member
    Posted 1 year ago #

    Yes it's a plugin.

    I paste the code here
    http://pastebin.com/gFdk9Wxd

  20. rhoi
    Member
    Posted 1 year ago #

    hi keesiemeijer do you know now how to solve my problem?

Topic Closed

This topic has been closed to new replies.

About this Topic