0

Not working on WordPress 6.0: https://thealliance.health/for-providers/resources/provider-news-archives/

Working dev site on WordPress 5.9.3: https://ccahdev.wpengine.com/for-providers/resources/provider-news-archives/

Here is the function if somebody can spot what may have been deprecated:

function wpb_postsbycategory2022() {

$posts = get_posts( array(
    'category_name' => 'Provider News',
    'nopaging'  => true,
    'orderby'   => 'date',
    'order'     => 'DESC', // it's DESC; not DSC
    // There's no use setting posts_per_page when nopaging is enabled.
    // Because posts_per_page will be ignored when nopaging is enabled.
) );

$_year_year = '';   // previous years value
$_year_mon = '';   // previous year-month value
$_has_grp = false; // TRUE if a group was opened
$_has_grpy = false; // TRUE if a group was opened
    
    
foreach ( $posts as $post ) {
    setup_postdata( $post );

    $time = strtotime( $post->post_date );
    $year = date( 'Y', $time );
    $mon = date( 'F', $time );
    $year_mon = "$year-$mon";
    $year_year = "$year";
    
    // Open a new group for year.
    if ( $year_year !== $_year_year ) {
        // Close previous group, if any.
        if ( $_has_grpy ) {
            echo '</div><!-- .year -->';
        }
        $_has_grpy = true;

        echo "<div class='year' id='year$year'>";
        echo "<h2>$year</h2>";
    }
    
    
    if ( $year_mon !== $_year_mon) {
        // Close previous group, if any.
        $_has_grp = true;

        echo '<div class="month">';
        echo "<span>$mon</span>";
    }

// Display post title.
if ( $title = get_the_title() ) {
    echo "<div><a href='/?p={$post->ID}'>{$post->post_title}</a></div>";
    }   
    
    if ( $year_mon !== $_year_mon) {
        // Close previous group, if any.
            echo '</div><!-- .month -->';
    }
    
    $_year_mon = $year_mon;
    $_year_year = $year_year;
}

// Close the last group, if any.
// 
    
if ( $_has_grpy ) {
    echo '</div>';
}
    
    
/* Restore original Post Data */
    
$output_string = ob_get_contents();
ob_end_clean();
return $output_string;
    
wp_reset_postdata();
}
// Add a shortcode
add_shortcode('categoryposts2022', 'wpb_postsbycategory2022');

Thank you for any assistance!

1
  • The pages you linked to are identical.
    – vancoder
    Jun 6, 2022 at 21:55

1 Answer 1

0

Can't see anything deprecated, but I have spotted these:

  1. category_name expects (confusingly) the category slug, so the value should (most likely) be provider-news and not Provider News

  2. If you're using setup_postdata it's best to also manually set the global $post too to ensure interoperability of template functions - just put global $post; inside the start of your wpb_postsbycategory2022 function.

  3. The line wp_reset_postdata() is after a return statement at the end of the function, so it will never be called! Move the return $output_string; line to the very end of your function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.