0

I would like to display posts with a 'year' parameter. For example: to display posts from 2020, the shortcode should look like [archived-posts year="2020"]. I have tried my code below but it's not working. Maybe the 'meta_value' is not correct? Any help would be appreciate it.

Thanks!

//get archived posts
add_shortcode( 'archived-posts', 'archived_posts' );
function archived_posts() {
$atts = shortcode_atts( array( 'year' => date('Y') ), $atts );
    $buffer = '<h3>Post Titles</h3>';
    $q = new WP_Query(array(
        'post_type' => 'post',
        'posts_per_page' => -1 ,
        'meta_key'       => 'post_date',
        'meta_value'     => $atts['year'],
        'post_status' => 'archive' ,
        //'year' => 2021,
    ));
    while ($q->have_posts()) {
        $q->the_post();
        $buffer = $buffer.get_the_title().'<br>';
    }
    wp_reset_postdata();
    return $buffer;
}

**EDIT June 20th 2021

I was able to make the code work the the year parameter, but now it only shows one result for each year. So maybe the loop is broken or something?! Please see updated code below.

//get archived posts per year
add_shortcode( 'archived-posts', 'archived_posts' );
function archived_posts($atts) {
    $a = shortcode_atts( array( 'year' => date('Y') ), $atts );

    $q = new WP_Query(array(
        'post_type' => 'post',
        'posts_per_page' => -1 ,
        'post_status' => 'archive' ,
        'year' => $a['year'],
    ));
    while ($q->have_posts()) {
        $q->the_post();
        $buffer = '<div class="archived-post-item"><a href="'.get_permalink().'">'.get_the_title().'</a></div>';
    }
    wp_reset_postdata();
    return $buffer;
}

**

9
  • Check out the WP_Query documentation page - specifically, the bit on Date Parameters. You'll also need to actually define the $atts variable as a function argument, as detailed in the Shortcode section of the Plugin Handbook
    – bosco
    Commented Jun 18, 2021 at 4:31
  • @bosco Thanks! Regarding your comment about $atts, that's not what I did below the function? Thanks!
    – Marcio
    Commented Jun 21, 2021 at 5:14
  • @bosco I just added an edit from my code. Please let me know what you think. Thanks!
    – Marcio
    Commented Jun 21, 2021 at 5:55
  • Looks good to me! Well done :)
    – bosco
    Commented Jun 22, 2021 at 2:08
  • 1
    @bosco That worked perfectly! Thank you so much!
    – Marcio
    Commented Jun 25, 2021 at 0:13

1 Answer 1

1

With help from @bosco , I was able to figure it out. Below is the working code:

//get archived posts per year
add_shortcode( 'archived-posts', 'archived_posts' );
function archived_posts($atts) {
    $a = shortcode_atts( array( 'year' => date('Y') ), $atts );

    $q = new WP_Query(array(
        'post_type' => 'post',
        'posts_per_page' => -1 ,
        'post_status' => 'archive' ,
        'year' => $a['year'],
    ));
    while ($q->have_posts()) {
        $q->the_post();
        $buffer .= '<div class="archived-post-item"><a href="'.get_permalink().'">'.get_the_title().'</a></div>';
    }
    wp_reset_postdata();
    return $buffer;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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