INNER JOIN skipping key with the same value

Im trying to filter my next_post_link and previous_post_link so it can be used on a custom post type called filmfremvisning with a meta_key field with a date in it. The filmfremvisning CPT is some kind of “events” CPT where the event posts have an expiration date on them. The expiration date has the following format of YYYYMMDD and is named dato_for_fremvisningen.

I also have a custom post status called expired and a cronjob that gives all expired posts that status every night.

Read More

I have tried these two links:

And I got it working quite nice. The problem however is that the INNER JOIN is skipping posts that has the same date.

For example if I have the following posts and dates:

  • Event 1 – 20140102
  • Event 2 – 20140105
  • Event 3 – 20140105
  • Event 4 – 20140107

It returns:

  • Event 1 – 20140102
  • Event 2 – 20140105
  • Event 4 – 20140107

The SQL statement is for furture post is:

INNER JOIN $wpdb->postmeta AS m ON p.ID = m.post_id 
WHERE p.post_type = 'filmfremvisning' 
  AND p.post_status = 'publish' 
  AND m.meta_key = 'dato_for_fremvisningen' 
  AND m.meta_value > '$current_filmfremvisning_date' 
GROUP BY p.ID ORDER BY m.meta_value ASC`

The code im using in functions.php is down below:

function get_adjacent_past_events_join($join) {
  if(is_singular('filmfremvisning')) {
    global $wpdb;
    $new_join = $join."INNER JOIN $wpdb->postmeta AS m ON p.ID = m.post_id ";
    return $new_join;
  }
  return $join;
}
add_filter('get_previous_post_join', 'get_adjacent_past_events_join');
add_filter('get_next_post_join', 'get_adjacent_past_events_join');


function get_future_filmfremvisnings_where($where) {
  if(is_singular('filmfremvisning')) {
    global $wpdb, $post;
    $id = $post->ID;
    $current_filmfremvisning_date = get_field('dato_for_fremvisningen', $id);
    $new_where = "WHERE p.post_type = 'filmfremvisning' AND p.post_status = 'publish' AND m.meta_key = 'dato_for_fremvisningen' AND m.meta_value > '$current_filmfremvisning_date'";
    return $new_where;
  }
  return $where;
}
add_filter('get_next_post_where', 'get_future_filmfremvisnings_where');


function get_past_filmfremvisnings_where($where) {
  if(is_singular('filmfremvisning')) {
    global $wpdb, $post;
    $id = $post->ID;
    $current_filmfremvisning_date = get_field('dato_for_fremvisningen', $id);
    $new_where = "WHERE p.post_type = 'filmfremvisning' AND p.post_status = 'publish' AND m.meta_key = 'dato_for_fremvisningen' AND m.meta_value < '$current_filmfremvisning_date'";
    return $new_where;
  }
  return $where;
}
add_filter('get_previous_post_where', 'get_past_filmfremvisnings_where');


function get_prev_past_filmfremvisnings_sort($sort) {
  if(is_singular('filmfremvisning')) {
    global $wpdb;
    $new_sort = " GROUP BY p.ID ORDER BY m.meta_value DESC";
    return $new_sort;
  }
  return $sort;
}
add_filter('get_previous_post_sort', 'get_prev_past_filmfremvisnings_sort');


function get_next_future_filmfremvisnings_sort($sort) {
  if(is_singular('filmfremvisning')) {
    global $wpdb;
    $new_sort = " GROUP BY p.ID ORDER BY m.meta_value ASC";
    return $new_sort;
  }
  return $sort;
}
add_filter('get_next_post_sort', 'get_next_future_filmfremvisnings_sort');

Related posts

Leave a Reply