bardius
Forum Replies Created
-
Full fixed code for cpto_get_next_post_where
function cpto_get_next_post_where($where, $in_same_cat, $excluded_categories)
{
global $post, $wpdb;if ( empty( $post ) )
return null;$current_post_date = $post->post_date;
$join = ”;
$posts_in_ex_cats_sql = ”;
if (isset($in_same_cat))
if ( $in_same_cat || !empty($excluded_categories) )
{
$join = ” INNER JOIN $wpdb->term_relationships AS tr ON p.ID = tr.object_id INNER JOIN $wpdb->term_taxonomy tt ON tr.term_taxonomy_id = tt.term_taxonomy_id”;if ( $in_same_cat ) {
$cat_array = wp_get_object_terms($post->ID, ‘category’, array(‘fields’ => ‘ids’));
$join .= ” AND tt.taxonomy = ‘category’ AND tt.term_id IN (” . implode(‘,’, $cat_array) . “)”;
}$posts_in_ex_cats_sql = “AND tt.taxonomy = ‘category'”;
if ( !empty($excluded_categories) ) {
$excluded_categories = array_map(‘intval’, explode(‘ and ‘, $excluded_categories));
if ( !empty($cat_array) ) {
$excluded_categories = array_diff($excluded_categories, $cat_array);
$posts_in_ex_cats_sql = ”;
}if ( !empty($excluded_categories) ) {
$posts_in_ex_cats_sql = ” AND tt.taxonomy = ‘category’ AND tt.term_id NOT IN (” . implode($excluded_categories, ‘,’) . ‘)’;
}
}
}$current_menu_order = $post->menu_order;
//check if there are more posts with lower menu_order
$query = “SELECT p.* FROM $wpdb->posts AS p
$join WHERE p.menu_order > ‘”.$current_menu_order.”‘ AND p.post_type = ‘”. $post->post_type .”‘ AND p.post_status = ‘publish’ $posts_in_ex_cats_sql”;
$results = $wpdb->get_results($query);if (count($results) > 0)
{
$where = “$join WHERE p.menu_order > ‘”.$current_menu_order.”‘ AND p.post_type = ‘”. $post->post_type .”‘ AND p.post_status = ‘publish’ $posts_in_ex_cats_sql”;
}
else
{
$where = “$join WHERE p.post_date > ‘”.$current_post_date.”‘ AND p.post_type = ‘”. $post->post_type .”‘ AND p.post_status = ‘publish’ AND p.ID != ‘”. $post->ID .”‘ $posts_in_ex_cats_sql”;
}return $where;
}