Skip to main content
replaced http://wordpress.stackexchange.com/ with https://wordpress.stackexchange.com/
Source Link

As @StephenHarris@StephenHarris pointed out in the comments, there's a core function that might come in handy when building the SQL Query: get_meta_sql() - Examples in Codex. Basically this function is just used to build the meta SQL statement that gets used in WP_Query, but you can use it in this case (or others) as well. The argument that you throw into it is an array, the exact same that would add to a WP_Query.

As @StephenHarris pointed out in the comments, there's a core function that might come in handy when building the SQL Query: get_meta_sql() - Examples in Codex. Basically this function is just used to build the meta SQL statement that gets used in WP_Query, but you can use it in this case (or others) as well. The argument that you throw into it is an array, the exact same that would add to a WP_Query.

As @StephenHarris pointed out in the comments, there's a core function that might come in handy when building the SQL Query: get_meta_sql() - Examples in Codex. Basically this function is just used to build the meta SQL statement that gets used in WP_Query, but you can use it in this case (or others) as well. The argument that you throw into it is an array, the exact same that would add to a WP_Query.

Added Stephens comments to the answer. Thanks for the interesting discussion in the comments!
Source Link
kaiser
  • 50.8k
  • 27
  • 150
  • 244

Automatic query building

As @StephenHarris pointed out in the comments, there's a core function that might come in handy when building the SQL Query: get_meta_sql() - Examples in Codex. Basically this function is just used to build the meta SQL statement that gets used in WP_Query, but you can use it in this case (or others) as well. The argument that you throw into it is an array, the exact same that would add to a WP_Query.

$meta_sql = get_meta_sql(
    $meta_query,
    'post',
    $wpdb->posts,
    'ID'
);

The return value is an array:

$sql => (array) 'join' => array(),
        (array) 'where' => array()

So you can use $sql['join'] and $sql['where'] in your callback.

Dependencies to keep in mind

Dependencies to keep in mind

Automatic query building

As @StephenHarris pointed out in the comments, there's a core function that might come in handy when building the SQL Query: get_meta_sql() - Examples in Codex. Basically this function is just used to build the meta SQL statement that gets used in WP_Query, but you can use it in this case (or others) as well. The argument that you throw into it is an array, the exact same that would add to a WP_Query.

$meta_sql = get_meta_sql(
    $meta_query,
    'post',
    $wpdb->posts,
    'ID'
);

The return value is an array:

$sql => (array) 'join' => array(),
        (array) 'where' => array()

So you can use $sql['join'] and $sql['where'] in your callback.

Dependencies to keep in mind

added 209 characters in body
Source Link
kaiser
  • 50.8k
  • 27
  • 150
  • 244
  • "get_{$adjacent}_post_join"

      $join
      // Only if `$in_same_cat`$in_same_cat`
     || // or: ! empty( $excluded_categories` 
      // and then: 
      // " 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"; AND 
     IF // and if $in_same_cat then it APPENDS: 
      // " AND tt.taxonomy = 'category' 
      // AND tt.term_id IN (" . implode(',', $cat_array) . ")";
      $in_same_cat
      $excluded_categories
    
  • "get_{$adjacent}_post_where"

      $wpdb->prepare(
           "
            WHERE p.post_date $op %s // $op = $previous ? '<' : '>'; | $current_post_date
            AND "WHERE p.post_typepost_date =$op %s "
            // $post->post_type
            AND."AND p.post_statuspost_type = 'publish'%s $posts_in_ex_cats_sql"
            // $posts_in_ex_cats_sql = " AND tt.taxonomy = 'category' 
            // AND tt.term_id NOT IN (" . implode($excluded_categories, ',') . ')'; 
            // OR empty string if $in_same_cat || ! empty( $excluded_categories
            ."AND p.post_status = 'publish' $posts_in_ex_cats_sql "
          ",$current_post_date
          $current_post_date,
          $post->post_type
      )
      $in_same_cat
      $excluded_categories
    
  • "get_{$adjacent}_post_sort"

      "ORDER BY p.post_date $order LIMIT 1"`
    
  • "get_{$adjacent}_post_join"

      $join // Only if `$in_same_cat || ! empty( $excluded_categories` and then: " 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"; AND IF $in_same_cat then it APPENDS: " AND tt.taxonomy = 'category' AND tt.term_id IN (" . implode(',', $cat_array) . ")";
      $in_same_cat
      $excluded_categories
    
  • "get_{$adjacent}_post_where"

      $wpdb->prepare(
           "
            WHERE p.post_date $op %s // $op = $previous ? '<' : '>'; | $current_post_date
            AND p.post_type = %s     // $post->post_type
            AND p.post_status = 'publish' $posts_in_ex_cats_sql // $posts_in_ex_cats_sql = " AND tt.taxonomy = 'category' AND tt.term_id NOT IN (" . implode($excluded_categories, ',') . ')'; OR empty string if $in_same_cat || ! empty( $excluded_categories
           "
          ,$current_post_date
          ,$post->post_type
      )
      $in_same_cat
      $excluded_categories
    
  • "get_{$adjacent}_post_sort"

      "ORDER BY p.post_date $order LIMIT 1"`
    
  • "get_{$adjacent}_post_join"

      $join
      // Only if `$in_same_cat`
      // or: ! empty( $excluded_categories` 
      // and then: 
      // " 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";  
      // and if $in_same_cat then it APPENDS: 
      // " AND tt.taxonomy = 'category' 
      // AND tt.term_id IN (" . implode(',', $cat_array) . ")";
      $in_same_cat
      $excluded_categories
    
  • "get_{$adjacent}_post_where"

      $wpdb->prepare(
            // $op = $previous ? '<' : '>'; | $current_post_date
             "WHERE p.post_date $op %s "
            // $post->post_type
            ."AND p.post_type = %s "
            // $posts_in_ex_cats_sql = " AND tt.taxonomy = 'category' 
            // AND tt.term_id NOT IN (" . implode($excluded_categories, ',') . ')'; 
            // OR empty string if $in_same_cat || ! empty( $excluded_categories
            ."AND p.post_status = 'publish' $posts_in_ex_cats_sql "
          ",
          $current_post_date,
          $post->post_type
      )
      $in_same_cat
      $excluded_categories
    
  • "get_{$adjacent}_post_sort"

      "ORDER BY p.post_date $order LIMIT 1"`
    
filters in the plugin didn't work as the arguments were swapped.
Source Link
Loading
added 766 characters in body
Source Link
kaiser
  • 50.8k
  • 27
  • 150
  • 244
Loading
Source Link
kaiser
  • 50.8k
  • 27
  • 150
  • 244
Loading