Skip to main content
deleted 2411 characters in body
Source Link

Okay! So, having spent all the night in order to find a right solution... finally.. I did it. Here is the right method, that works in my situation.

The $wpbd method is pretty cool and complicated at the same time. While using this method do not forget to cache queries properly with php-apc or memcache.

//Week events
$query_date = $_GET['date'];
$month_start = date('Ym01',strtotime('this month'));
$month_end = date('Ymt',strtotime('this month'));
//IF query Day is empty
if ( empty($query_date) ) {

  //Custom query
  $postids = $wpdb->get_col( $wpdb->prepare(
    "
    SELECT p.* FROM $wpdb->posts p
    JOIN $wpdb->postmeta pm1 ON (p.ID = pm1.post_id)
    JOIN $wpdb->postmeta pm2 ON (p.ID = pm2.post_id)
    WHERE p.post_type = 'post'
      AND p.post_status = 'publish'
      AND ( ( pm1.meta_key = 'event_start_date' 
      AND CAST( pm1.meta_value AS DATE) BETWEEN '$month_start' AND '$month_end' )
      OR ( pm2.meta_key = 'event_end_date' 
      AND CAST( pm2.meta_value AS DATE ) BETWEEN '$month_start' AND '$month_end' ) )
    "
  ) );
  
  //Insert Posts ID array and get their data
  $args = array(
    'post__in' => $postids,
    'meta_key' => 'event_start_date',
    'orderby' => 'meta_value_num',
    'order' => 'ASC'
  );

} else {

  //Date
  $query_date = date('Ymd', strtotime($query_date));

  //Custom query
  $postids = $wpdb->get_col( $wpdb->prepare(
    "
    SELECT p.* FROM $wpdb->posts p
    JOIN $wpdb->postmeta pm1 ON (p.ID = pm1.post_id)
    JOIN $wpdb->postmeta pm2 ON (p.ID = pm2.post_id)
    WHERE p.post_type = 'post'
    AND p.post_status = 'publish'
    AND ( ( pm1.meta_key = 'event_start_date'
    AND CAST( pm1.meta_value AS DATE) BETWEEN '$query_date' AND '$month_end' )
    XOR ( pm2.meta_key = 'event_end_date' 
    AND CAST( pm2.meta_value AS DATE ) BETWEEN '$query_date' AND '$month_end' ) )
    "
  ) );
  
  //Insert Posts ID array and get their data
  $args = array(
    'post__in' => $postids,
    'meta_key' => 'event_start_date',
    'orderby' => 'meta_value_num',
    'order' => 'ASC'
  );

}

$wp_query = new WP_Query( $args );
//End of custom query

Okay! So, having spent all the night in order to find a right solution... finally.. I did it. Here is the right method, that works in my situation.

The $wpbd method is pretty cool and complicated at the same time. While using this method do not forget to cache queries properly with php-apc or memcache.

//Week events
$query_date = $_GET['date'];
$month_start = date('Ym01',strtotime('this month'));
$month_end = date('Ymt',strtotime('this month'));
//IF query Day is empty
if ( empty($query_date) ) {

  //Custom query
  $postids = $wpdb->get_col( $wpdb->prepare(
    "
    SELECT p.* FROM $wpdb->posts p
    JOIN $wpdb->postmeta pm1 ON (p.ID = pm1.post_id)
    JOIN $wpdb->postmeta pm2 ON (p.ID = pm2.post_id)
    WHERE p.post_type = 'post'
      AND p.post_status = 'publish'
      AND ( ( pm1.meta_key = 'event_start_date' 
      AND CAST( pm1.meta_value AS DATE) BETWEEN '$month_start' AND '$month_end' )
      OR ( pm2.meta_key = 'event_end_date' 
      AND CAST( pm2.meta_value AS DATE ) BETWEEN '$month_start' AND '$month_end' ) )
    "
  ) );
  
  //Insert Posts ID array and get their data
  $args = array(
    'post__in' => $postids,
    'meta_key' => 'event_start_date',
    'orderby' => 'meta_value_num',
    'order' => 'ASC'
  );

} else {

  //Date
  $query_date = date('Ymd', strtotime($query_date));

  //Custom query
  $postids = $wpdb->get_col( $wpdb->prepare(
    "
    SELECT p.* FROM $wpdb->posts p
    JOIN $wpdb->postmeta pm1 ON (p.ID = pm1.post_id)
    JOIN $wpdb->postmeta pm2 ON (p.ID = pm2.post_id)
    WHERE p.post_type = 'post'
    AND p.post_status = 'publish'
    AND ( ( pm1.meta_key = 'event_start_date'
    AND CAST( pm1.meta_value AS DATE) BETWEEN '$query_date' AND '$month_end' )
    XOR ( pm2.meta_key = 'event_end_date' 
    AND CAST( pm2.meta_value AS DATE ) BETWEEN '$query_date' AND '$month_end' ) )
    "
  ) );
  
  //Insert Posts ID array and get their data
  $args = array(
    'post__in' => $postids,
    'meta_key' => 'event_start_date',
    'orderby' => 'meta_value_num',
    'order' => 'ASC'
  );

}

$wp_query = new WP_Query( $args );
//End of custom query
added 2411 characters in body
Source Link

Okay! So, having spent all the night in order to find a right solution... finally.. I did it. Here is the right method, that works in my situation.

The $wpbd method is pretty cool and complicated at the same time. While using this method do not forget to cache queries properly with php-apc or memcache.

//Week events
$query_date = $_GET['date'];
$month_start = date('Ym01',strtotime('this month'));
$month_end = date('Ymt',strtotime('this month'));
//IF query Day is empty
if ( empty($query_date) ) {

  //Custom query
  $postids = $wpdb->get_col( $wpdb->prepare(
    "
    SELECT p.* FROM $wpdb->posts p
    JOIN $wpdb->postmeta pm1 ON (p.ID = pm1.post_id)
    JOIN $wpdb->postmeta pm2 ON (p.ID = pm2.post_id)
    WHERE p.post_type = 'post'
      AND p.post_status = 'publish'
      AND ( ( pm1.meta_key = 'event_start_date' 
      AND CAST( pm1.meta_value AS DATE) BETWEEN '$month_start' AND '$month_end' )
      OR ( pm2.meta_key = 'event_end_date' 
      AND CAST( pm2.meta_value AS DATE ) BETWEEN '$month_start' AND '$month_end' ) )
    "
  ) );
  
  //Insert Posts ID array and get their data
  $args = array(
    'post__in' => $postids,
    'meta_key' => 'event_start_date',
    'orderby' => 'meta_value_num',
    'order' => 'ASC'
  );

} else {

  //Date
  $query_date = date('Ymd', strtotime($query_date));

  //Custom query
  $postids = $wpdb->get_col( $wpdb->prepare(
    "
    SELECT p.* FROM $wpdb->posts p
    JOIN $wpdb->postmeta pm1 ON (p.ID = pm1.post_id)
    JOIN $wpdb->postmeta pm2 ON (p.ID = pm2.post_id)
    WHERE p.post_type = 'post'
    AND p.post_status = 'publish'
    AND ( ( pm1.meta_key = 'event_start_date'
    AND CAST( pm1.meta_value AS DATE) BETWEEN '$query_date' AND '$month_end' )
    XOR ( pm2.meta_key = 'event_end_date' 
    AND CAST( pm2.meta_value AS DATE ) BETWEEN '$query_date' AND '$month_end' ) )
    "
  ) );
  
  //Insert Posts ID array and get their data
  $args = array(
    'post__in' => $postids,
    'meta_key' => 'event_start_date',
    'orderby' => 'meta_value_num',
    'order' => 'ASC'
  );

}

$wp_query = new WP_Query( $args );
//End of custom query

Okay! So, having spent all the night in order to find a right solution... finally.. I did it. Here is the right method, that works in my situation.

The $wpbd method is pretty cool and complicated at the same time. While using this method do not forget to cache queries properly with php-apc or memcache.

//Week events
$query_date = $_GET['date'];
$month_start = date('Ym01',strtotime('this month'));
$month_end = date('Ymt',strtotime('this month'));
//IF query Day is empty
if ( empty($query_date) ) {

  //Custom query
  $postids = $wpdb->get_col( $wpdb->prepare(
    "
    SELECT p.* FROM $wpdb->posts p
    JOIN $wpdb->postmeta pm1 ON (p.ID = pm1.post_id)
    JOIN $wpdb->postmeta pm2 ON (p.ID = pm2.post_id)
    WHERE p.post_type = 'post'
      AND p.post_status = 'publish'
      AND ( ( pm1.meta_key = 'event_start_date' 
      AND CAST( pm1.meta_value AS DATE) BETWEEN '$month_start' AND '$month_end' )
      OR ( pm2.meta_key = 'event_end_date' 
      AND CAST( pm2.meta_value AS DATE ) BETWEEN '$month_start' AND '$month_end' ) )
    "
  ) );
  
  //Insert Posts ID array and get their data
  $args = array(
    'post__in' => $postids,
    'meta_key' => 'event_start_date',
    'orderby' => 'meta_value_num',
    'order' => 'ASC'
  );

} else {

  //Date
  $query_date = date('Ymd', strtotime($query_date));

  //Custom query
  $postids = $wpdb->get_col( $wpdb->prepare(
    "
    SELECT p.* FROM $wpdb->posts p
    JOIN $wpdb->postmeta pm1 ON (p.ID = pm1.post_id)
    JOIN $wpdb->postmeta pm2 ON (p.ID = pm2.post_id)
    WHERE p.post_type = 'post'
    AND p.post_status = 'publish'
    AND ( ( pm1.meta_key = 'event_start_date'
    AND CAST( pm1.meta_value AS DATE) BETWEEN '$query_date' AND '$month_end' )
    XOR ( pm2.meta_key = 'event_end_date' 
    AND CAST( pm2.meta_value AS DATE ) BETWEEN '$query_date' AND '$month_end' ) )
    "
  ) );
  
  //Insert Posts ID array and get their data
  $args = array(
    'post__in' => $postids,
    'meta_key' => 'event_start_date',
    'orderby' => 'meta_value_num',
    'order' => 'ASC'
  );

}

$wp_query = new WP_Query( $args );
//End of custom query
Paragraphs for readability. Code markdown.
Source Link
brasofilo
  • 22.1k
  • 8
  • 69
  • 264

So, I'm trying to resolve the problem with event displaying. An

An event has its start and end dates, written in meta. For For example, event starts on (dmY) 03.04.2013 and ends up on 08.04.2013. Using WP_queryWP_query and meta_querymeta_query I can exclude this event if I am trying to get data by 09.04.2013. 

At the same time this event will show up each day in the interval, that is cool too. But, I can't really exclude this event if I'm trying to get data by 02.04.2013. 
The interval I use is this month, I mean, for example from 01.04.2013 to 30.04.2013. Here

Here is the code. 
In my particullarparticular situation I need XOR functionality for meta_relationmeta_relation. The best way is to create a custom $wpdb$wpdb query, but I'm not too strong in MySQL....

So, I'm trying to resolve the problem with event displaying. An event has its start and end dates, written in meta. For example, event starts on (dmY) 03.04.2013 and ends up on 08.04.2013. Using WP_query and meta_query I can exclude this event if I am trying to get data by 09.04.2013. At the same time this event will show up each day in the interval, that is cool too. But, I can't really exclude this event if I'm trying to get data by 02.04.2013. The interval I use is this month, I mean, for example from 01.04.2013 to 30.04.2013. Here is the code. In my particullar situation I need XOR functionality for meta_relation. The best way is to create a custom $wpdb query, but I'm not too strong in MySQL....

I'm trying to resolve the problem with event displaying.

An event has its start and end dates, written in meta. For example, event starts on (dmY) 03.04.2013 and ends up on 08.04.2013. Using WP_query and meta_query I can exclude this event if I am trying to get data by 09.04.2013. 

At the same time this event will show up each day in the interval, that is cool too. But, I can't really exclude this event if I'm trying to get data by 02.04.2013. 
The interval I use is this month, I mean, for example from 01.04.2013 to 30.04.2013.

Here is the code. 
In my particular situation I need XOR functionality for meta_relation. The best way is to create a custom $wpdb query, but I'm not too strong in MySQL....

added 69 characters in body
Source Link
Loading
Source Link
Loading