0

In the following shortcode, I have two variable, $today and $tommorrow that are being called inside a custom meta query.

function display_prognostika_home_loop($atts, $content = null) {
    // Get all the categories
    $taxonomy = 'diorganosi';
    $today = current_time("Y-m-d");
    $tomorrow = date("Y-m-d", strtotime('tomorrow'));

    $parent_terms = get_terms ( 
    array (
        'taxonomy' => $taxonomy,
        'parent' => 0,
        'fields' => 'ids',
        ) 
    );
    
    
    $terms_list = get_terms($taxonomy,
    array (
        'hide_empty' => true, // change to true if you don't want empty terms
        'orderby' => 'name',
        'order' => 'ASC',
        'exclude' => $parent_terms,
        )
    );  

    ob_start();
    
    // Loop through the $category terms
    foreach ( $terms_list as $term ):
        
    
        $prognostika_loop = new WP_Query(
            array(
                'post_type' => 'prognostika',
                'showposts' => -1,
                'order' => 'ASC',
                'orderby' => 'prog_date_time',
                'tax_query' => array(
                    array(
                        'taxonomy'  => $taxonomy,
                        'terms'     => array( $term->term_id ),
                        'field'     => 'term_id',
                    )
                ),
                'meta_query' => array(
                    array(
                        'key'       => 'prog_date_time',
                        'value'     => $today,
                        'compare'   => '=',
                        'type'      => 'DATE',
                    ),
                ),  
            )
        );
        // output category title and posts
        if($prognostika_loop->have_posts()) {
        ?>
        
        <div class="gb-button-wrapper prognostiko-diorganosi-wrapper">

            <span class="gb-button prognostiko-diorganosi"><span class="gb-button-text"><?php echo $term->name; ?></span>
                <span class="gb-icon">
                    <svg xmlns="http://www.w3.org/2000/svg" xml:space="preserve" style="enable-background:new 0 0 27.965 27.965" viewBox="0 0 27.965 27.965"><path d="M23.43 12.929c-.021-.021-.043-.034-.064-.052-.025-.029-.047-.061-.07-.09l-6.273-6.269c-.812-.813-2.099-.845-2.871-.071-.773.772-.741 2.061.071 2.872l3.054 3.056H5.967a1.981 1.981 0 1 0 0 3.962h11.254l-3.041 3.037c-.824.826-.867 2.122-.094 2.896.775.772 2.069.731 2.895-.095l6.354-6.354c.826-.824.868-2.12.095-2.892z" style="fill:#030104"></path><path d="M13.983 0C6.262 0 0 6.261 0 13.985c0 7.721 6.262 13.979 13.983 13.979 7.724 0 13.982-6.259 13.982-13.979C27.966 6.261 21.707 0 13.983 0zm0 26.535c-6.933 0-12.551-5.619-12.551-12.549 0-6.934 5.619-12.551 12.551-12.551s12.551 5.617 12.551 12.551c0 6.93-5.619 12.549-12.551 12.549z" style="fill:#030104"></path>
                    </svg>
                </span>
            </span>

        </div>
                
        <?php
        //$cat_object = $prognostika_loop->get_queried_object();
        //$cats = $cat_object->count;
        //var_dump ($cat_object);
    
        while ($prognostika_loop->have_posts()) : $prognostika_loop->the_post();
        do_action('prognostika_loop');
        endwhile;
        }
    
        // Reset query
        $customQuery = null;
        wp_reset_postdata();

    // end the loop
    endforeach;
    return ob_get_clean();
}

add_shortcode('prognostika_home_loop', 'display_prognostika_home_loop');

I would like to set up attributes so that

[prognostika_home_loop today] - Display today's posts
[prognostika_home_loop tomorrow] - Display tomorrow's posts

meaning today and tomorrow would affect the 'value'=> $today pair. When today is used, pair would become 'value' => $today. When tomorrow is used, pair would become 'value' => $tomorrow.

1 Answer 1

2

Use the attribute value to decide today and tomorrow like:

Code explanation: Instead of today and tomorrow naming in shortcode, I'm using show_for in this answer. Based on show_for value we can update the meta query later on. In the code, I checked if show_for is set by user or it exists in the $atts array. If yes, I set the $prog_time variable based on the $atts['show_for'] value. If no, I set the default value for $prog_time.

if(isset($atts['show_for'])) { 
 switch($atts['show_for']) {
   case 'today' : $prog_time = current_time("Y-m-d"); break; 
   case 'tomorrow' : $prog_time = date("Y-m-d", strtotime('tomorrow')); break;
   default : $prog_time = current_time("Y-m-d"); break; 
 }
} else { 
  $prog_time = current_time("Y-m-d"); 
}

Now, there is no need to use $today and $tomorrow separately. Use only $prog_time instead of $today in the meta query.

        'meta_query' => array(
            array(
                'key'       => 'prog_date_time',
                'value'     => $prog_time, // updated here; will have dynamic time as decided in switch clause
                'compare'   => '=',
                'type'      => 'DATE',
            ),
        ),

Your shortcode call will be like:

  • [prognostika_home_loop show_for="today"]
  • [prognostika_home_loop show_for="tomorrow"]
  • and you can configure many in switch clause for passed, yesterday, etc.

Additional Notes:

  • Since you're using Y-m-d format, make sure prog_date_time (while saving post meta) value is also set/updated with the same format for matching.
  • If you've multiple attributes, setting default values for attributes is recommended.
2
  • 1
    Apart form a parenthesis and a break; missing, the code works great! if( isset($atts['show_for']) ) { switch($atts['show_for']) { case 'today' : $prog_time = current_time("Y-m-d"); break; case 'tomorrow' : $prog_time = date("Y-m-d", strtotime('tomorrow')); break; default : $prog_time = current_time("Y-m-d"); break; } } else { $prog_time = current_time("Y-m-d"); } Commented Dec 7, 2022 at 13:35
  • Thanks, updated the answer. :)
    – Aqsa J.
    Commented Dec 7, 2022 at 13:40

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.