0

I'm having trouble placing endwhile in the proper place in a shortcode I made to grab data from a custom post type.

I'm using ACF (advanced custom fields) for custom fields.

As of now, the code doesn't fully work and I suspect it has to do with no 'endwhile' , 'endif' , or 'wp_reset_query()' (though I'm new to php).

Here's the shortcode I have for this:

// Add Shortcode
function custom_shortcode() {

    // find todays date
    $date = date('Ymd');

    // args
    $args = array(
        'post_type'     => 'events',
        'meta_key'      => 'event_date',
        'meta_value'    => '$date'
    );


    // query
    $the_query = new WP_Query( $args );

            // The Loop
            if ( $the_query->have_posts() ) {

                $out .= '<ul>';
                while ( $the_query->have_posts() ) {
                    $the_query->the_post();
                    $out .= "<div>";
                    $out .= get_field('event_name') . "<br>";
                $out .='<img class="myimage" style="max-width:180px;" src='. get_field('event_image') . ">" ;
                $out .= " <br>";
                $out .= '<div class="date">' . get_field('event_date') . "</div>" . "<br>";
                $out .= '<div class="time">' . get_field('event_time') . "</div>" ;
                $out .= get_field('buy_ticket_button') . "<br>";
                $out .= "</div>";

                }


            }
           return $out;

}
add_shortcode( 'todaysevent', 'custom_shortcode' );
  • There should be two slashes in the opening comment. e.g. // Also, remove the single quotes around '$date' in the $args array. The syntax otherwise seems fine. Would you please explain 'doesn't work fully'? What are you expecting to happen? What does happen? – Dave Romsey Dec 3 '16 at 23:20
  • Thanks, there is two slashes in the opening comment (I pasted it without accidentally). I'm trying to get 'events' that are happening today and display them with this shortcode. So far, it will display the first event I made (even though I've deleted it) if I remove the date part. With the date part it shows nothing. Without the date part or $the_query->the_post(); it shows the current events in an infinite loop. – Rosenbruger Dec 3 '16 at 23:28
  • 1
    I'd add a call to wp_reset_postdata() before the closing brace of the if statement. From there, divide and conquer. Debug the value of $date. Is it in the same format as what's saved in the custom fields? BTW, you don't need endif or endwhile because the if and while loops are using regular {} brackets, and not the alternate if : endif; & while : endwhile; syntax. – Dave Romsey Dec 3 '16 at 23:50
  • You can write it like this: while( true ): endwhile; or like this: while( true ) { }, you've done the latter, and so you don't need to put endwhile; anywhere, just look at the matching braces – Tom J Nowell Dec 3 '16 at 23:52
  • Your src attribute in the img tag is missing quotes around the value. – Milo Dec 4 '16 at 0:06
0

So I spent like 5 hours trying to figure out why my proper-looking code wasn't giving me the behavior I wanted. Thanks to the fine commentators I was able to get current posts to display without the event_date specified but wasn't able to fetch posts by date.

I started thinking outside of the box and learned that it was showing 'todays' events but the timezone of the server was in a different country so it was tomorrow's events for me. I specified the default timezone and it works perfectly!

Here's the shortcode I made to fetch my custom post type 'event' according to which ones have the custom field 'event_date' value of todays date.

// Add Shortcode
function custom_shortcode() {

date_default_timezone_set("America/New_York");

// find todays date
$today = date("Ymd");


  // args
    $args = array(
        'post_type'     => 'event',
        'meta_key'      => 'event_date',
        'meta_value'    => $today
    );



    // query
    $the_query = new WP_Query( $args );


            // The Loop
            if ( $the_query->have_posts() ) {

                $out .= '<ul>';
                while ( $the_query->have_posts() ) {
                    $the_query->the_post();
                    $out .= "<div>";
                    $out .= get_field('event_name') . "<br>"; 
                    $out .='<img class="myimage" style="max-width:180px;" src="'. get_field('event_image') . '">' ;
                    $out .= " <br>";
                    $out .= '<div class="date">' . get_field('event_date') . "</div>" . "<br>";
                    $out .= '<div class="time">' . get_field('event_time') . "</div>" ;
                    $out .= get_field('buy_ticket_button') . "<br>";
                    $out .= "</div>";

                }

            wp_reset_postdata();
            }

           return $out;

}
add_shortcode( 'todaysevent', 'custom_shortcode' );

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.