function get_date_posts($params, $content){
        $default_params = array(
            'post_type'     => 'post',
            'order'         => 'asc',
            'orderby'       => 'meta_value',
            'posts_per_page'=> 5,
            'meta_key'      => 'fl_date_picker'

        if (isset($params['type']) && !empty($params['type']) && post_type_exists($params['type']))
            $params['post_type'] = $params['type'];

        if (isset($params['taxonomy']) && !empty($params['taxonomy']) && taxonomy_exists($params['taxonomy']) 
            && isset($params['slug']) && !empty($params['slug']))
            $params[$params['taxonomy']] = $params['slug'];

        foreach(array('type', 'slug', 'taxonomy') as $key)
            if (isset($params[$key]))

        $params = wp_parse_args($params, $default_params);

        $params['meta_query'] = array( array(
                'key' => $params['meta_key'],
                'value' => date("Ymd"),
                'compare' => '>=',
                'type'=> 'date'

        $transient_hash = 'get_date_posts_'.substr(md5(serialize($params)),0,10);

        if (false === ($html = get_transient($transient_hash))){
            add_filter('query', 'q');
            global $request_sql;

            // init
            $html = '';

            $rp = new WP_Query($params);
        //  $html .= '<h2>START</h2>';
        //  $html .= '<pre>'.var_export($params, 1).'</pre>';
        //  $html .= $request_sql.'<hr>';

    if ($rp->have_posts()){

                $html .= '<ul>';
                while ( $rp->have_posts() ) { 
                    $html .= "<li>";

                    if (($thumb_id = get_post_thumbnail_id())){
                        $thumb = wp_get_attachment_image_src($thumb_id, 'panel-small', false);
                        $flag = get_field('fl_country');
                        $html .= "<a href=".get_permalink()."><div id=\"calimgf\" style=\"background:url({$thumb['0']}) no-repeat left top;\">";
                        $html .= "<span id=\"loullos\" style=\"float:right;\" class='". $flag ."' >" ."</span>";
                    $html .= "<span class=\"calendate\" >".get_field('fl_event_date')."</span>";
                    $html .= "<span class=\"calenhead\" >".get_field('fl_event_title')."</span>";

                    //  debug   
                    //  $html .= "<small>".get_post_meta(get_the_id(), $params['meta_key'], 1)."</small>";

                    $html .= "</div></a></li>";
                $html .= '</ul>';

            if (!empty($html))
                set_transient($transient_hash, $html, 60);

        return $html;

The function above sorts posts by date using a custom field 'fl_date_picker'

I need to modify this function so that the user specifies the number of posts to show in the shortcode. For that I tried using:

  extract( shortcode_atts( array(
        'post_type'     => 'post',
            'order'         => 'asc',
            'orderby'       => 'meta_value',
            'posts_per_page'=> 5,
            'meta_key'      => 'fl_date_picker'
    ), $atts ) );

However the fl_date_picker does no longer work if I do this and post order is not retained.


You are misusing shortcode_atts().

  1. You need to pass the shortcode an array of defaults in the first argument, which you have done.
  2. Then you need to pass the user supplied arguments in the second argument, which you haven't done. Your shortcode callback uses $params yet for some reason you are passing $atts, which is not defined in your function.

Additionally, you are using extract() with will create variables out of the supplied array. However, your code is not written to use those extracted variables. It still uses $params. extract() is, in my opinion, a confusing way to write code. Stick with $params.

Proof of concept:

function my_sc_cb($params = '') {
  $params = shortcode_atts( 
      'post_type'     => 'post',
      'order'         => 'asc',
      'orderby'       => 'meta_value',
      'posts_per_page'=> 5,
      'meta_key'      => 'fl_date_picker'
  var_dump($params); // debugging
do_shortcode('[msccb post_type="page"]');
  • thanks but this doesn't work. I get an empty result instead. I have tried various ways of calling the shortcode but i always get empty results: [get_date_posts type="os_calendar" taxonomy="calendar_event_type" slug="tennis" posts_per_page="5" meta_key="fl_date_picker"]
    – JoaMika
    Feb 25 '14 at 19:57
  • Read the docs for shortcode_atts. The defaults have to match up with the attributes you are using.
    – s_ha_dum
    Feb 25 '14 at 20: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.