0

i', try added to my site upcoming events, but when i try add via get_post_meta - not working, for example: http://prntscr.com/exe8oi i added date, then i use this my code:

<?php $today = date('U')*1000;?>
                        <?php $event = new WP_Query(array(
                          'post_type'=>'event_init',
                          'meta_query' => array(
                            array(
                            'key' => 'date_events',
                            'value' =>$today,
                            'compare' => '>' ,
                            'order'=>'ASC'
                            )
                          ),
                          'showposts' => -1
                        ));?>
                        <h2><?php the_title();?></h2>
                        <?php if($event->have_posts()): while($event->have_posts()): $event->the_post();?>
                                        <?php $events_date = get_post_meta(get_the_ID(), 'date_events', true);?>
                                        <h3 class="uppper" style="font-weight:500;">
                                            <?php echo date('l d F, Y', $events_date/1000 + 86400 )?>
                                        </h3>
                                        <h4><?php the_title();?></h4>
                                        <?php echo mb_substr( strip_tags( get_the_content() ), 0, 120 ); ?>...
                                    <?php endwhile; endif; wp_reset_postdata(); ?>

but posts not display in frontend, what wrong? and little additional question: how i can output date in german language (need name of week and need months output in german language). Thanks

0

The problem seems to be the comparison. Wordpress compares the post meta using SQL, so you have to use its syntax.

Try setting:

<?php $today = date("Y-m-d H:i:s"); ?>

Which will return something like 2017-12-1 12:12:12

In your post meta make sure to use the same syntax. You can set dates in various formats, this is the SQL default.

I would suggest using Advanced Custom Fields, or setring your own metaboxes to avoid typos and future headaches.

Here is a snippet of code from a project I worked with that dealt with exactly that. The date field was set in the admin using ACF;

$today= date('Ymd');  // ACF formatting
$args = array (
    'cat' => '3',
    'meta_query' => array(
         array(
                'key'       => 'event_date',
                'compare'   => '>=',
                'value'     => $today,
        )
    ),
);
  • ok thanks, what about language name of week and months in german language format date? – user89886 Apr 17 '17 at 19:40
  • MySQL doesn't 'really' understand different languages (to keep things short). A better practice is to save all fields in a standard format, and localize on the interface. So if you are saving a date in chinese, german, english US/UK/AU it always works. Thats why its best to use ACF, Metaboxes or other metabox system. Again, ACF is your best bet in terms of ease of use, flexibility etc. It provides a date picker that is translated to your local WP install, but save in its default (Ymd) format in the database. – Pedro Coitinho Apr 18 '17 at 17:27
0

i'm use this code with you modification, but nothing, i added field via ACF plugin, but not working, what wrong?

<?php $today = date("d-m-Y H:i:s"); 
    $args = array('post_type' => 'event_init', 
    'order' => 'ASC',
    'orderby' => 'date',
    'meta_query' => array(
    array(
    'key'       => 'date_acf_field',
    'compare'   => '>',
    'value'     => $today,
        )
    ),
                                                    'numberposts' => -1
        );
    $loop = new WP_Query($args);
    if($loop->have_posts()) {
    while($loop->have_posts()) : $loop->the_post();?>
    <?php //here my posts sturcture ?>
    <?php endwhile; } ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy