统计ACF中的关系帖子数

Count the number of relation posts in ACF

我正在为一个乐队开发一个网站,您可以在其中添加演出并添加在该特定演出中播放的歌曲。

所以我创建了两个自定义 post 类型: - 演出 - 歌曲

我得到了一个 "Relationship" 类型的自定义字段 "Songs"。此字段显示在自定义 Post 类型上。这样我就可以将歌曲添加到特定的演出中。这非常有效。

但我想在该网站的主页上显示一些统计数据:我想计算特定歌曲的播放次数并显示前 10 名。所以我想我必须循环演出自定义 post 键入并计算与 'songs' 的关系。

我认为这可以解决问题:

<?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(); ?>

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

例如:歌曲 "A memory" 有 2 场演出。这就是为什么你可以在数组中找到它两次的原因。歌曲 "Wasted" 只能找到一次,因为它在 1 场演出中。

希望得到帮助:

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

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

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

    <?php 
    $song_count = count(get_field('songs', get_the_ID())); <-- add 
    echo $song_count . " ";

    the_title(); 

    ?><br />

<?php endwhile; ?>

<?php else: ?>
    <!-- No gigs available -->
<?php endif; ?>
<?php wp_reset_postdata(); ?>

我对你的问题的理解是,你正在寻求生成具有最相关演出的歌曲的前 10 名列表。解决这个问题的最佳方法是生成一个集合,该集合映射一个唯一标识符和一个歌曲被播放次数的计数值。

这是一个例子:

 <?php

// Get all the posts
$gigs = get_posts([
    'post_type' => 'gigs',
    'numberposts' => -1
]);

// We will use this array to key a running tally of
$set = [];

// If the key doesn't exist yet on the array, then we will initialize it, otherwise, increment the count
function add_set_element(&$set, $key) {
    if (!isset($set[$key])) {
        $set[$key] = 1;
    } else {
        $set[$key]++;
    }
}

function iterate_songs($songs, &$set){
    /** @var WP_Post $song */
    foreach($songs as $song) {
        $key = $song->post_title;// This can be what ever unique identifier you want to get from $song object, such as ID or title
        add_set_element($set, $key);
    }
}

foreach($gigs as $gig) {
    setup_postdata($gig);
    $songs = get_the_field('songs');
    iterate_songs($songs, $set);
}

之后,您可以根据需要对 $set 变量进行排序和操作,以从中获取所需的数据。

如果我误解了您的问题,请告诉我,我可以提供另一个答案。

你可以用简单而简短的方式这样做:

$args = array(
    'post_type' => 'gig'
); 
$gigs =  get_posts($args);
$songsarr = array();
foreach($gigs  as $gig) {
   $posts = get_field('songs', $gig->ID);
   array_push($songsarr,$posts[0]);
}
//echo "<pre>;
//print_r($songsarr);

$countsongs = array_count_values($songsarr);
echo 'No. of Duplicate Items: '.count($countsongs).'<br><br>';
// print_r($countsongs);

foreach($countsongs as $songID => $songname){
    echo get_the_title( $songID );
    echo $songname;
}

我已经尝试制作两个自定义 Post 类型(演出、歌曲),我通过这种方式获得了歌曲的数量,您可以在主页上显示这些歌曲,也可以在最后 [=17] 中给出条件=]foreach 循环 如果歌曲不止一首等..

希望对您有所帮助!

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

<?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; ?>

上面的代码将创建一个数组,其中包含 post 首歌曲的 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  
}

所以完整的代码是:

<?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(); ?>