0

I'm struggling to get a working solution with wp_query for Event management, Currently post order by event start date & time work well. Now I want some custom settings.

Have two meta boxes working with meta_key=> start_datetime and meta_key=> end_datetime

1- key=> end_datetime is a date and time for the post to end (no longer display in the results)

2- Additionally print some text in posts as two hours remain to start event. e.g <p>LIVE</p>

How to hide post when date&time end, and add some text as remain 2 hours to start?

$today = date("Y/m/j");
$args (array(
  'post_type' => 'event',
  'posts_per_page' => 10,
  'meta_key' => 'start_datetime',
  'orderby' => 'meta_value',
  'order' => 'ASC',
  'meta_query' => array(
    array(
      'key' => 'start_datetime',
      'value' => $today,
      'compare' => '>=',
      'type' => 'CHAR'
    )
   ),
  'tax_query' => array(
  'relation' => 'AND',
  array(
    'key' => 'end_datetime',
    'meta-value' => $value,
    'compare' => '=<'
  )
 )
));
$query = new WP_Query( $args );
  if ( $query->have_posts() ) {
  while( $query->have_posts() ) {
    $query->the_post();
    the_title();
  }
}
2
  • 1
    You've lost relation in the query.
    – Max Yudin
    Commented Feb 25, 2018 at 8:45
  • Relation is default set to AND so it doesn't need to be set, see WP docs.
    – Beee
    Commented Feb 25, 2018 at 14:21

1 Answer 1

1

I checked your query and there's a lot of junk in there. You define meta keys 2x. Once as meta_key and once as meta_query. Only keep the query, it has more options.

Second, the tax query is all wrong. The parameters for a tax query are not the same as a meta query. You need taxonomy, field, terms and possible compare. I don't see a reason to use a tax query, based on your info, so I removed it in my example.

I am a fan of storing dates/times as a unix timestamp. Is a lot easier to compare and order than a date string. By looking at your today string, I assume the rest is in that format as well, so I will use that for the example.

I wrote the code below by heart since I had no events to work with (with that meta key), but I think you'll get most of the desired result and if not, maybe tweak it a little.

$today = date( 'Y/m/d' );

$args = array(
    'post_type'      => 'event',
    'posts_per_page' => 10,
    'order'          => 'ASC',
    'meta_query'     => array(
        // get posts which have an end_datetime which is bigger than or equal to $today (and thus hide the ones which have ended before today)
        'end_clause' => array(
            'key'     => 'end_datetime',
            'value'   => $today,
            'compare' => '>=',
            'type'    => 'DATE'
        )
    ),
    'orderby'        => array( 'end_clause' => 'DESC' ),
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) {
    while( $query->have_posts() ) {
        $query->the_post();

        the_title();

        // check if start_datetime is not empty
        if ( ! empty( get_post_meta( get_the_ID(), 'start_datetime', 1 ) ) ) {
            // make unix timestamp of start_datetime
            $start_datetime = strtotime( date( 'U', get_post_meta( get_the_ID(), 'start_datetime', 1 ) ) );
            // make unix timestamp of now
            $now            = date( 'U', current_time( 'timestamp' ) );
            // if (start_time - now) is smaller than 7200 seconds
            if ( ( $start_datetime - $now ) < 2 * HOUR_IN_SECONDS ) {
                echo '<p>LIVE</p>';
            }
        }
    }
}

Let me know if this helps.

6
  • Sorry for late. After copy & past its not show anything, I change type=>CHAR then this code give output which have issues. 1- Query print Live in each post. 2- (no longer display in the results also not work.
    – FRQ6692
    Commented Feb 26, 2018 at 8:02
  • The 'type' DATE works with the 'compare' value BETWEEN only. see here codex.wordpress.org/Class_Reference/WP_Meta_Query
    – FRQ6692
    Commented Feb 26, 2018 at 8:30
  • Oh snap, @FRQ6692 you're right, forgot that.
    – Beee
    Commented Feb 26, 2018 at 10:54
  • After a little bit modification this code work. The only thing remain to adjust is not to display posts when time end please take a look. Thanks
    – FRQ6692
    Commented Feb 26, 2018 at 15:21
  • I think you need to change $start_datetime - $now to $end_datetime - now (my error).
    – Beee
    Commented Feb 26, 2018 at 15:42

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.