0

So, im kinda new to this shortcode but i've almost got working what i need now. (check below of the code)

I would like to filter my content within my shortcode, i'm having a CPT with fields added by the ACF plugin. Now what i need is when i place [speaker_overview_2017 year=2017] it shows only the items with value 2017, and when i'm doing [speaker_overview_2017 year=2018] only 2018, and when i'm doing [speaker_overview_2017] it show's all the items.

Here is my shortcode code, could someone help me out with this?

add_shortcode('speaker_overview_2017', 'speakers_overview_shortcode');

function speakers_overview_shortcode($atts){

    $args = array(
        'post_type' => 'qp_speakers',
        'post_status'           => 'publish',
        'orderby'               => 'menu_order',
        'order'                 => 'ASC',
        'posts_per_page'        => -1,
        'meta_query' => array(
            array(
                'key' => 'speakers_jaar',
                'value' => '2017',
            )
        )
    );

    ob_start();

    $locaties = new WP_Query( $args );

    if ( $locaties->have_posts() ) : ?>
        <?php while ( $locaties->have_posts() ) : $locaties->the_post(); ?>
            <?php q2_speakers_template(); ?>
        <?php endwhile; // end of the loop. ?>
    <?php endif;

    wp_reset_postdata();

    return ob_get_clean();
}
2
  • edit the question to show us the real code you test because this code cannot work, there is twice "return". then if you put year=2018 in the shortcode, you will retrieve it in $atts
    – mmm
    Commented Feb 18, 2018 at 10:46
  • mb, the 1st return wasn't supposed to be there. Commented Feb 18, 2018 at 17:18

1 Answer 1

1

The year attribute you added to shortcode [speaker_overview_2017 year=2017] will be available through $atts in this way $atts['year']. I assume, you want to do a meta query and the key is speakers_jaar and the value will be $atts['year']. Please check the following code. When a year is set in shortcode, query will be limited to that year otherwise not limited.

function speakers_overview_shortcode( $atts ) {
    $atts = shortcode_atts( array(
        'year' => ''
    ), $atts );

    $args = array(
        'post_type'      => 'qp_speakers',
        'post_status'    => 'publish',
        'orderby'        => 'menu_order',
        'order'          => 'ASC',
        'posts_per_page' => -1,
    );

    if ( ! empty( $atts['year'] ) ) {
        $args['meta_query'] = array(
            array(
                'key'   => 'speakers_jaar',
                'value' => absint( $atts['year'] ),
            )
        );
    }

    $locaties = new WP_Query( $args );

    ob_start();

    if ( $locaties->have_posts() ) :
        while ( $locaties->have_posts() ) {
            $locaties->the_post();
            q2_speakers_template();
        }
    }
    wp_reset_postdata();

    return ob_get_clean();
}
add_shortcode( 'speaker_overview_2017', 'speakers_overview_shortcode' );
2
  • excellent mate, exactly what i needed. You missed a ; tho in your code, but it's fine, it's working :) Commented Feb 18, 2018 at 17:37
  • Glad to hear that it worked perfectly except the ; :D
    – obiPlabon
    Commented Feb 18, 2018 at 18:34

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.