计算ACF中的关系职位数

Den*_*nis 5 php wordpress relationship custom-post-type advanced-custom-fields

我正在一个乐队的网站上,您可以在其中添加演出,也可以添加在该演出中播放的歌曲。

因此,我创建了两种自定义帖子类型:-演出-歌曲

我得到了一个“关系”类型的自定义字段“歌曲”。此字段显示在“自定义帖子类型”上。这样,我可以将歌曲添加到特定的演出。这很完美。

但是我想在该网站的首页上显示一些统计信息:我想计算一首特定歌曲的播放次数并显示前10首。所以我想我必须遍历演出的自定义帖子类型并计算这种关系与“歌曲”。

我认为这可以解决问题:

<?php 
    $args = array(
        'post_type' => 'gig'
    ); 
?>

<?php $loop = new WP_Query($args); ?>

<?php if ( $loop->have_posts() ) : while ( $loop->have_posts() ) : $loop->the_post(); ?>

    <?php 
    print_r(get_field('songs'))
    //$song_count = count(get_field('songs')); 
    //echo $song_count . " ";

    the_title(); 

    ?><br />

<?php endwhile; ?>

<?php else: ?>
    <!-- No gigs available -->
<?php endif; ?>
<?php wp_reset_postdata(); ?>
Run Code Online (Sandbox Code Playgroud)

您可以在此处找到print_r的结果:http : //snippi.com/s/njzg3uu

例如:歌曲“ A memory”是2场演出。这就是为什么您可以在数组中找到它两次的原因。歌曲“浪费”只能找到一次,因为它已录制1场。

Den*_*nis 5

您可以使用此代码创建所有歌曲的数组:

<?php 
    $args = array(
        'post_type' => 'gig'
    ); 

    $countArray = []; //create an array where you can put all the song id's and the number of times played
?>

<?php $loop = new WP_Query($args); ?>

<?php if ( $loop->have_posts() ) : while ( $loop->have_posts() ) : $loop->the_post(); ?>

    <?php
        $posts = get_field('songs');

        if( $posts ): 
                foreach( $posts as $post):
                    setup_postdata($post); 

                    //if the song id already exists -> count + 1
                    if (array_key_exists($post->ID, $countArray)){
                        $countArray[$post->ID]++;
                    }
                    else { // otherwise the song is played 1 time
                        $countArray[$post->ID] = 1;    
                    } 
                endforeach;

            wp_reset_postdata();
        endif;
    ?>

<?php endwhile; ?>
Run Code Online (Sandbox Code Playgroud)

上面的代码将创建歌曲的帖子 ID 数组以及它在 post_type "gig" 中使用的次数。

现在您可以使用该数组$countArray并使用它做任何您想做的事情。在您的示例中,您想对其进行排序,因此您必须这样做arsort($countArray);,数组按其值(播放次数)从高到低排序。

然后你必须遍历数组: foreach ($countArray as $key => $value) { ?>

<?php echo get_post_permalink($key); //=the permalink of the song ?>
<?php echo get_the_title($key); //= the title of the song ?>
<?php echo $value; //number of times play in a gig ?>

<?php  
}
Run Code Online (Sandbox Code Playgroud)

所以完整的代码是:

<?php 
    $args = array(
        'post_type' => 'gig'
    ); 

    $countArray = [];
?>

<?php $loop = new WP_Query($args); ?>

<?php if ( $loop->have_posts() ) : while ( $loop->have_posts() ) : $loop->the_post(); ?>

    <?php
        $posts = get_field('songs');

        if( $posts ): 
                foreach( $posts as $post):
                    setup_postdata($post); 

                    if (array_key_exists($post->ID, $countArray)){
                        $countArray[$post->ID]++;
                    }
                    else {
                        $countArray[$post->ID] = 1;    
                    } 
                endforeach;

            wp_reset_postdata();
        endif;
    ?>

<?php endwhile; ?>

<?php
    arsort($countArray);

    foreach ($countArray as $key => $value) {
    ?>

        <?php echo get_post_permalink($key); //=the permalink of the song ?>
        <?php echo get_the_title($key); //= the title of the song ?>
        <?php echo $value; //number of times play in a gig ?>

    <?php  
    }
?>

<?php else: ?>
    <!-- No gigs available -->
<?php endif; ?>
<?php wp_reset_postdata(); ?>
Run Code Online (Sandbox Code Playgroud)