posts_clauses (ok)

https://wordpress.stackexchange.com/questions/78649/using-meta-query-meta-query-with-a-search-query-s

function custom_search_where($pieces) {

    // filter for your query
    if (is_search() && !is_admin()) {

        global $wpdb;

        $keywords = explode(' ', get_query_var('s'));
        $query = "";
        foreach ($keywords as $word) {

            // skip possible adverbs and numbers
            if (is_numeric($word) || strlen($word) <= 2) 
                continue;

            $query .= "((mypm1.meta_key = 'speel')";
            $query .= " AND (mypm1.meta_value  LIKE '%{$word}%')) OR ";
        }

        if (!empty($query)) {
            // add to where clause
            $pieces['where'] = str_replace("(((wp_posts.post_title LIKE '%", "( {$query} ((wp_posts.post_title LIKE '%", $pieces['where']);

            $pieces['join'] = $pieces['join'] . " INNER JOIN {$wpdb->postmeta} AS mypm1 ON ({$wpdb->posts}.ID = mypm1.post_id)";
        }
    }
    return ($pieces);
}
add_filter('posts_clauses', 'custom_search_where', 20, 1);

https://wordpress.stackexchange.com/questions/17135/usage-of-the-new-posts-clauses-filter-in-wordpress-3-1

code

<?php
function intercept_query_clauses($pieces) {
  echo '<style>#post-clauses-dump { display: block; background-color: #777; color: #fff; white-space: pre-line; }</style>';
  if (current_user_can('manage_options')) {
    $dump = var_export($pieces, true);
    echo "< PRE id='post-clauses-dump'>{$dump}</ PRE >";
  }
  return $pieces;
}
add_filter('posts_clauses', 'intercept_query_clauses', 20, 1);
?>
<?php
array(
  'where'    => ' AND wp_posts.post_type = "post" AND (wp_posts.post_status = "publish" OR wp_posts.post_status = "private")',
  'groupby'  => '',
  'join'     => '',
  'orderby'  => 'wp_posts.post_date DESC',
  'distinct' => '',
  'fields'   => 'wp_posts.*',
  'limits'   => 'LIMIT 0, 10',
);
array(
  'where'    => ' AND wp_posts.post_name IN ("front-page") AND (0 = 1) AND wp_posts.post_type = "wp_template" AND ((wp_posts.post_status = "publish"))',
  'groupby'  => 'wp_posts.ID',
  'join'     => '',
  'orderby'  => 'wp_posts.post_date DESC',
  'distinct' => '',
  'fields'   => 'wp_posts.*',
  'limits'   => '',
);
array(
  'where'    => ' AND wp_posts.post_name IN ("home","index") AND (0 = 1) AND wp_posts.post_type = "wp_template" AND ((wp_posts.post_status = "publish"))',
  'groupby'  => 'wp_posts.ID',
  'join'     => '',
  'orderby'  => 'wp_posts.post_date DESC',
  'distinct' => '',
  'fields'   => 'wp_posts.*',
  'limits'   => '',
);
?>
<!-- Nếu bạn chỉ sửa đổi một mệnh đề cụ thể, bạn có thể nên sử dụng một trong các bộ lọc dành riêng cho mệnh đề sau: -->
* posts_where_paged
* posts_groupby
* posts_join_paged
* posts_orderby
* posts_distinct
* post_limits
* posts_fields
<?php
function intercept_query_clauses($pieces) {
  if (is_search() && !is_admin()) {
    global $wpdb;
    $keywords = explode(' ', get_query_var('s'));
    $query    = "";
    foreach ($keywords as $word) {
      if (is_numeric($word) || strlen($word) <= 2) {
        continue;
      }
      $query .= "((mypm1.meta_key = 'speel')";
      $query .= " AND (mypm1.meta_value  LIKE '%{$word}%')) OR ";
    }
    if (!empty($query)) {
      // add to where clause
      $pieces['where'] = str_replace("(((wp_posts.post_title LIKE '%", "( {$query} ((wp_posts.post_title LIKE '%", $pieces['where']);
      $pieces['join'] = $pieces['join'] . " INNER JOIN {$wpdb->postmeta} AS mypm1 ON ({$wpdb->posts}.ID = mypm1.post_id)";
    }
  }
  echo '<style>#post-clauses-dump { display: block; background-color: #777; color: #fff; white-space: pre-line; }</style>';
  if (current_user_can('manage_options')) {
    $dump = var_export($pieces, true);
    echo "<pre id='post-clauses-dump'>{$dump}</pre>";
  }
  return $pieces;
}
add_filter('posts_clauses', 'intercept_query_clauses', 20, 1);
?>
<?php  
array(
  'where'    => ' AND (((wp_posts.post_title LIKE "{e3751311a396b2efd50f99a4aeed6a3f4f1abbf7c44949c843bac87c7527f6cb}a{e3751311a396b2efd50f99a4aeed6a3f4f1abbf7c44949c843bac87c7527f6cb}") OR (wp_posts.post_excerpt LIKE "{e3751311a396b2efd50f99a4aeed6a3f4f1abbf7c44949c843bac87c7527f6cb}a{e3751311a396b2efd50f99a4aeed6a3f4f1abbf7c44949c843bac87c7527f6cb}") OR (wp_posts.post_content LIKE "{e3751311a396b2efd50f99a4aeed6a3f4f1abbf7c44949c843bac87c7527f6cb}a{e3751311a396b2efd50f99a4aeed6a3f4f1abbf7c44949c843bac87c7527f6cb}"))) AND wp_posts.post_type IN ("post", "page", "attachment") AND (wp_posts.post_status = "publish" OR wp_posts.post_author = 1 AND wp_posts.post_status = "private")',
  'groupby'  => '',
  'join'     => '',
  'orderby'  => 'wp_posts.post_title LIKE "{e3751311a396b2efd50f99a4aeed6a3f4f1abbf7c44949c843bac87c7527f6cb}a{e3751311a396b2efd50f99a4aeed6a3f4f1abbf7c44949c843bac87c7527f6cb}" DESC, wp_posts.post_date DESC',
  'distinct' => '',
  'fields'   => 'wp_posts.*',
  'limits'   => 'LIMIT 0, 10',
);
array(
  'where'    => ' AND wp_posts.post_name IN ("search") AND (0 = 1) AND wp_posts.post_type = "wp_template" AND ((wp_posts.post_status = "publish"))',
  'groupby'  => 'wp_posts.ID',
  'join'     => '',
  'orderby'  => 'wp_posts.post_date DESC',
  'distinct' => '',
  'fields'   => 'wp_posts.*',
  'limits'   => '',
);
?>

Last updated