简体   繁体   中英

Wordpress php grouping by ACF custom field result renders to many groups with copies of posts

So, I'm basically trying to make an archive of posts which are ordered in groups after year. I'm using the ACF date picker and with a function getProdYear(); I'm extracting the year from each date. Then I've tried to follow this example to create a group for each year.

PHP:

function getProdYear() {
    $dateBeg = get_field('prod_date_beg');
    $yB = substr($dateBeg, 0, 4);
    $mB = substr($dateBeg, 4, 2);
    $dB = substr($dateBeg, 6, 2);
    $timeB = strtotime("{$dB}-{$mB}-{$yB}");

    global $prod_year;
    $prod_year = date('Y' , $timeB );

}   

$args = array(
    'category_name' => 'produktion',
    'numberposts' => -1,
    'order' => 'DSC',
    'orderby' => 'meta_value',
    'meta_key' => 'prod_date_beg',
);

$posts = get_posts($args); 
$group_posts = array();

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

    getProdYear(); //run function to get year only from 'prod-date-beg' in $prod_year 

    if( !isset($group_posts[ $prod_year ]) )
    {
        $group_posts[ $prod_year ] = array();
    }

    $group_posts[ $prod_year ][] = $post; //create group

    //var_dump($group_posts);

    foreach ($group_posts as $year_group => $year_posts) {
        echo '<strong>'. $prod_year . '</strong>';
        echo '<ul>';
        foreach ($year_posts as $post) {
            echo '<li>'. get_the_title($post->ID) .'</li>';
        }
        echo '</ul>';
    } 
endforeach; 
wp_reset_postdata(); 

The result I'm getting now is: 不想要的结果

Ok, I fixed it. I was looping inside a loop, by closing the foreach($posts as $post) before starting foreach ($group_by_year as $year_name => $year_posts) It group everything correctly. I also changed echo '<strong>'. $prod_year . '</strong>'; echo '<strong>'. $prod_year . '</strong>'; to echo '<strong>'. $year_name . '</strong>'; echo '<strong>'. $year_name . '</strong>'; since the first repeated same name for each group.

Here's my working PHP:

    function getProdYear() {
    $dateBeg = get_field('prod_date_beg');
    $yB = substr($dateBeg, 0, 4);
    $mB = substr($dateBeg, 4, 2);
    $dB = substr($dateBeg, 6, 2);
    $timeB = strtotime("{$dB}-{$mB}-{$yB}");

    global $prod_year;
    $prod_year = date('Y' , $timeB );

}   

$args = array(
    'category_name' => 'produktion',
    'numberposts' => -1,
    'order' => 'DSC',
    'orderby' => 'meta_value',
    'meta_key' => 'prod_date_beg',
);

$posts = get_posts($args); 
$group_posts = array();

//loop through posts
foreach($posts as $post) {
    setup_postdata( $post ); 

    //run function to get year only from 'prod-date-beg' in $prod_year 
    getProdYear(); 

    //$add to so
    if( !isset($group_by_year[ $prod_year ]) )
    {
        $group_by_year[ $prod_year ] = array();
    }

    $group_by_year[ $prod_year ][] = $post; //create group

    }

    //loop through group_by_year    
    foreach ($group_by_year as $year_name => $year_posts) {
        echo '<strong>'. $year_name . '</strong>';
        echo '<ul>';
        foreach ($year_posts as $post) {
            echo '<li>'. get_the_title($post->ID) .'</li>';
        }
        echo '</ul>';
    }
wp_reset_postdata(); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM