Support

Account

Home Forums General Issues Query relationship fields based on field value.

Solved

Query relationship fields based on field value.

  • Hey folks,

    I have two post types:
    reviews
    games

    I’m using the relationship field to link reviews to games via the field related_games

    On the reviews post type, I have a field for the total score of the review, and a field for the version of the game the review is based upon.

    However, there may be multiple reviews from different writers, based upon that same version of the game.

    On the games post type, I want to query the total scores from reviews, based on the most recent version of the game, and give an average score base on the individual review scores.

    After many hours of banging my head against my desk, I have this:

    
    <?php
    $game_versions = get_posts(array(
        'post_type'     => 'reviews',
        'meta_query'    => array(
            array(
                'key'         => 'related_games',
                'value'       => '"' . get_the_ID() . '"',
                'compare'     => 'LIKE'
            )
        )
    ));
    
    $highest_version = 0;
    
    $version_array = [];
    foreach( $game_versions as $game_version):
        $version_array[] = get_field('review_game_version', $game_version->ID );
    endforeach;
    
    foreach ($version_array as $version):
        if ($version > $highest_version) :
            $highest_version = $version;
        endif;
    endforeach;
    

    $highest_version gives me the latest (highest) version number.

    However, this is where I’m fumbling and Google isn’t helping.

    The final aim is to have a div with the average score of all reviews based upon the latest version. When the user hovers over it, they will see a panel that shows the individual reviews that make up the average and include links to them, for more details.

    Any help or tips would be appreciated. My forehead would be very thankful.

    Thanks 🙂

  • If I am not missing anything, your problem is that you grab the reviews version numbers, but once you have found the highest version, you no longer know, which review that version number applies to, right?

    <?php
    // Fetch all reviews dealing with this game.
    $reviews = get_posts(array(
        'post_type'     => 'reviews',
        'meta_query'    => array(
            array(
                'key'         => 'related_games',
                'value'       => '"' . get_the_ID() . '"',
                'compare'     => 'LIKE'
            )
        )
    ));
    
    // An associative array of each review of each version.
    $version_array = [];
    
    foreach ( $reviews AS $k => $v ) {
    	
    	// Fetch the version.
    	$version = get_field( 'review_game_version' , $v->ID ) ?? false;
    	
    	// Ignore reviews that have no version.
    	if ( $version === false ) continue;
    	
    	// Save the version in the post object for later use.
    	$reviews[$k]->review_game_version = $version;
    	
    	// File this review by its game's version.
    	if ( $version_array[$version] ) {
    		$version_array[$version] = [];
    	}
    	$version_array[$version] = $v;
    }
    unset( $version );
    
    // Put the highest version at the end.
    ksort( $version_array );
    $highest_version = array_key_last() ?? false;
    
    // Make sure there is a highest version.
    if ( $highest_version !== false ) {
    	
    	// Iterate all reviews that are dealing with the highest version.
    	foreach ( $version_array[$highest_version] AS $review ) {
    		// Do something...
    	}
    }
  • Thank you very much for that @daniel-buth

    That is amazingly helpful, I really appreciate it.

  • Good to hear. One correction though: I was missing an isset(). Otherwise, if there are multiple reviews, they will overwrite each other. Good luck!

    <?php
    // File this review by its game's version.
    if ( !isset( $version_array[$version] ) ) {
    	$version_array[$version] = [];
    }
    $version_array[$version] = $v;
Viewing 4 posts - 1 through 4 (of 4 total)

The topic ‘Query relationship fields based on field value.’ is closed to new replies.