Support » Fixing WordPress » Order taxonomy term by meta value number of assigned posts

  • Hey,

    I try to make a list with my taxonomy terms “brand” of custom post type “phones” ordered by meta value number “post_view” of assigned posts. So, taxonomy term witch have posts with most views, will be first.
    Ordered by this formula: total number of views of all posts / number of posts.

    Anyone know?

    Thanks a lot.

Viewing 7 replies - 1 through 7 (of 7 total)
  • Thread Starter gomymusic

    (@gomymusic)

    Nobody?

    C W (VYSO)

    (@cyril-washbrook)

    In your loop, use the orderby parameter with meta_value_num, and the meta_key parameter with post_view. (I assume that the number of views for each post is stored in the custom field post_view.)

    See also the Codex page.

    Thread Starter gomymusic

    (@gomymusic)

    In this way I will list posts ordered by meta_value_num.
    I want to list taxonomy terms sorted by views of posts assigned.

    Anyone?

    C W (VYSO)

    (@cyril-washbrook)

    Here is a general idea for how you could do this. There may be better ways to do it:

    (1) For each taxonomy term, use the Loop and get_post_meta() to retrieve the post_view value for each post.

    (2) Add these post_view values together and store the total number in a variable for each taxonomy.

    (3) Use an array to store each taxonomy term (ID or name) as a key and each variable as a value. You will then have an array of the form:

    taxonomy_term1 => value1,
    taxonomy_term2 => value2,
    taxonomy_term3 => value3,
    taxonomy_term4 => value4,
    ...

    (4) Use a PHP sorting function and the in-built WordPress functions to echo the taxonomies in order of the array values.

    You should use caching so that these calculations do not have to be done every time.

    Thread Starter gomymusic

    (@gomymusic)

    Can you make an example please?

    I ran into a similar situation that I solved with the direction given by Cyril. I needed to sort my taxonomy term “artist” from the post type “items” alphabetically by the custom field “last-name”. I set up the custom field using Elliot Condon’s excellent Advanced Custom Fields plugin. Here is my solution. You will need to adapt it for your own situation.

    ENGLISH
    1. Get the terms for your taxonomy. The orderby passed to WP’s get_terms function is irrelevant because we will resort the returned PHP object later on.

    2. Get the custom field value and append it to your PHP object.

    3. Add your comparison function to your functions.php file that you will call for sorting the object.

    4. Use PHP’s usort() function to sort your object by the comparison criteria you defined in step 3.

    5. Use a foreach loop to output the sorted array in whatever way you need.

    That’s it! My code is below. Please let me know if there is any problem with my code. Everything seems to work well for me with my own installation.

    CODE

    // STEP 1
    $artistsList = get_terms( 'artists', $args );
    // STEP 2
    $t = 0;
    
    // expand the array to include the last name field
    foreach ( $artistsList as $artist ) {
    	$termID = $artistsList[$t]->term_id;
    
    	// get the field last name for this ID
    	$lastName = get_field('last_name', 'artists_'.$termID);
    
    	// plop it into the object
    	$artistsList[$t]->lastName = $lastName;
    
    	$t ++;
    }
    // STEP 3
    function cmp($a, $b)
    {
    	return strcmp($a->lastName, $b->lastName);
    }
    // STEP 4
    usort($artistsList, "cmp");
    // STEP 5
    foreach ( $artistsList as $artist ) {
    // code to output your awesome list of taxonomy terms sorted by custom field here
    }

    @prunly I also had the same problem and was searching like 1 day or so for a solution and then found your post. You Mr, deserve a cookie.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Order taxonomy term by meta value number of assigned posts’ is closed to new replies.