0

I want to create a shortcode, which shows only the posts that have the definite meta_value. I use the ACF Plugin for different meta-fields. I have the post type "publication" and e.g. want to show only the Publications, which are released in 2017 with this shortcode [Publication 2017] or Publication release-Date 2017].

function shortcode_display_publication_details(){

$args = array(
    'post_type' => 'publication',
    'posts_per_page' => -1,
    'meta_key'          => 'release-date',
    'meta_value' => '2017',
    'orderby'           => 'meta_value',
    'order'             => 'DESC',
    'post_status' => 'publish'
);

$query = new WP_Query( $args );
while ( $query->have_posts() ) : $query->the_post();
    echo  the_field('autor'). " (" ;
    echo  the_field('release-date'). ") ";
    echo  the_field('buch'). ". ";
    echo '<b>' . get_the_title() . '</b>'. ", ";
    echo  the_field('verleger'). ". " ;
    if (get_field('seiten')){
    echo  "S. ";
    echo the_field('seiten'). ". <p />" ;
    }
    echo '<p>';
    echo '&nbsp;';

endwhile;

// Restore original Post Data
wp_reset_postdata();

}
// Register the shortcodes.
add_shortcode( 'publication', 'shortcode_display_publication_details' );

This Shortcode shows only the Publications of 2017. But I don't want to build a extra Shortcode for every year. How does it work, that I can create a Shortcode with custom meta_value like this [Publication YEARXXXX] ?

Thank you very much for your help!

0

You need your shortcode to accept attributes. A shortcode with attributes would look like:

[publication year="2017"]

Then in your add_shortcode callback function you receive attributes in the first parameter of the function:

function wpse_279094_publication_shortcode( $atts ) {
    echo $atts['year'];
}
add_shortcode( 'publication', 'wpse_279094_shortcode' );

You want to be able to account for a user not using any attributes, so you use the shortcode_atts() function to populate $atts with defaults if they aren't set. In your use case the current year seems like a logical default. So that would look like:

function wpse_279094_publication_shortcode( $atts ) {
    $atts = shortcode_atts( 
        array(
            'year' => date('Y')
        ),
        $atts
    );
}
add_shortcode( 'publication', 'wpse_279094_shortcode' );

So, all together that your code should look like:

function wpse_279094_publication_shortcode(){
    $atts = shortcode_atts( array( 'year' => date('Y') ), $atts );

    $args = array(
        'post_type'      => 'publication',
        'posts_per_page' => -1,
        'meta_key'       => 'release-date',
        'meta_value'     => $atts['year'],
        'orderby'        => 'meta_value',
        'order'          => 'DESC',
        'post_status'    => 'publish'
    );

    $query = new WP_Query( $args );

    ob_start();

    while ( $query->have_posts() ) : $query->the_post();
        echo the_field( 'autor' ). " (" ;
        echo the_field( 'release-date' ). ") ";
        echo the_field( 'buch' ). ". ";
        echo '<b>' . get_the_title() . '</b>'. ", ";
        echo the_field( 'verleger' ). ". " ;
        if ( get_field( 'seiten' ) ) {
            echo "S. ";
            echo the_field( 'seiten' ). ". <p />" ;
        }
        echo '<p>';
        echo '&nbsp;';
    endwhile;

    wp_reset_postdata();

    return ob_get_clean();
}
add_shortcode( 'publication', 'wpse_279094_publication_shortcode' );

Now you can use [publication year="2017"], [publication year="2012"] etc. to get publications from different years, or [publication] to get publications from the current year.

Also note one other change I made to your code. In your code you just echoed your output. Shortcodes need to return their output, otherwise the contents won't appear in the right place.

One way to do this is to just build up a large string:

$output = get_the_title();
$output .= get_the_content();

return $output;

But I find that cumbersome. Instead, I used ob_start() which will 'capture' all the output without echoing it to the screen. Then, I return ob_get_clean() to return the captured output.

  • My Comment were deleted... So I try it once again: – Rike Sep 12 '17 at 7:03
  • Thank you very much for your answer. I tried out your code, but it works only for 2017. I used the shortcode: [publication year="2016"], but I get only the publications of 2017. Maybe I have to get the meta_value otherwise? Like this: 'meta_key' => get_field('release-date'), 'meta_value' => $atts['year'], This dosn't work, but maybe it's the right approach? would be very happy, if you culd help me. Thank you very much!!! – Rike Sep 12 '17 at 7:05
  • No that's not how meta_key works. What type of ACF field is release-date? – Jacob Peattie Sep 12 '17 at 7:10
  • Now it works, I forget to add the $atts here: function wpse_279094_publication_shortcode( $atts ). Thank you a lot for your help! – Rike Sep 12 '17 at 7:35

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.