# Custom sql wordpress 1 (ok)

**Phần 1: Viết phần cơ bản**

```
function __adapted_search_function($search, $query) {
  if (is_admin() || !$query->is_main_query() || !$query->is_search) {
    return;
  }
  global $wpdb;
  $search_term = $query->get('s');
  $search      = " AND (";
  $search .= "({$wpdb->posts}.post_title LIKE '%$search_term%')";
  $search .= " OR ";
  $search .= "({$wpdb->posts}.post_excerpt LIKE '%$search_term%')";
  $search .= " OR ";
  $search .= "({$wpdb->postmeta}.meta_key = 'findergg' AND $wpdb->postmeta.meta_value LIKE '%$search_term%')";
  $search .= ") ";
  add_filter("posts_join", '__custom_join_tables');
  add_filter('posts_where', 'posts_where');
  add_filter('posts_fields', 'custom_posts_fields');
  return $search;
}
function posts_where($where) {
  global $wpdb;
  $where = "  ";
  return $where;
}
function __custom_join_tables($joins) {
  global $wpdb;
  $joins .= " JOIN $wpdb->postmeta AS wpma ON (wpma.post_ID = {$wpdb->posts}.ID) AND {$wpdb->posts}.post_status = 'publish'  AND wpma.meta_value LIKE '%Sunt expedita%' ";
  return $joins;
}
function custom_posts_fields($fields) {
  global $wpdb;
  $fields = " {$wpdb->posts}.post_title, {$wpdb->posts}.post_content ";
  return $fields;
}
add_action('posts_search', '__adapted_search_function', 1, 2);ima
```

![](https://1113999984-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M84NwP_L_qEsBHOa2xo%2Fuploads%2FJuGKXgy2gs5cXJCRCO1j%2FScreenshot_1.png?alt=media\&token=2f28ed63-2fe0-4c5c-abd3-17a75013b7da)

![](https://1113999984-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M84NwP_L_qEsBHOa2xo%2Fuploads%2Few4rwqcOou0VFh0rSD9m%2FScreenshot_2.png?alt=media\&token=fa7f7568-7f75-4b25-8b80-f34f2457ea4e)

![](broken-reference) ![](https://1113999984-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M84NwP_L_qEsBHOa2xo%2Fuploads%2Fv91VWkFkkuebZ2fn247b%2FScreenshot_3.png?alt=media\&token=8614307f-3f06-4c3a-ae4e-c2e1bd1f1b31)

Test:

{% embed url="<http://localhost/wordpress1?s=perspiciatis+laboriosam>" %}
Có 2 kết quả:
{% endembed %}

![](https://1113999984-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M84NwP_L_qEsBHOa2xo%2Fuploads%2F02kqduBoFyBVfGiOqyY0%2FScreenshot_4.png?alt=media\&token=912c4998-44b1-4ba2-8dfa-78522c73eadf)

Kết quả sql

```
SELECT wp_posts.post_title, wp_posts.post_content FROM wp_posts JOIN wp_postmeta AS wpma ON (wpma.post_ID = wp_posts.ID) 
AND wp_posts.post_status = 'publish' 
AND wpma.meta_value LIKE '%Sunt expedita%' WHERE 1=1 
ORDER BY (CASE WHEN wp_posts.post_title LIKE '{test8}perspiciatis laboriosam{test8}' THEN 1 WHEN wp_posts.post_title LIKE '{test8}perspiciatis{test8}' AND wp_posts.post_title LIKE '{test8}laboriosam{test8}' THEN 2 WHEN wp_posts.post_title LIKE '{test8}perspiciatis{test8}' OR wp_posts.post_title LIKE '{test8}laboriosam{test8}' THEN 3 WHEN wp_posts.post_excerpt LIKE '{test8}perspiciatis laboriosam{test8}' THEN 4 WHEN wp_posts.post_content LIKE '{test8}perspiciatis laboriosam{test8}' THEN 5 ELSE 6 END), wp_posts.post_date DESC LIMIT 0, 10
```

Chú ý:

![](https://1113999984-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M84NwP_L_qEsBHOa2xo%2Fuploads%2FquG0QdFSA0MkgMIBR1je%2FScreenshot_1.png?alt=media\&token=f52431a6-a3f1-4d43-9172-b7a0d030fa32)

Kết quả sql:

![](https://1113999984-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M84NwP_L_qEsBHOa2xo%2Fuploads%2Fly3fYvA0dM5ck7JJlwsg%2FScreenshot_2.png?alt=media\&token=033a24d1-7a44-4ee3-8372-8c3681a5aac5)

Chú ý: Nếu thêm điều kiện này thì thật hoàn hảo

```
$where = " AND {$wpdb->posts}.post_status = 'publish'  ";
```

![](https://1113999984-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M84NwP_L_qEsBHOa2xo%2Fuploads%2FRypndMbTzceWBPvywW6A%2FScreenshot_3.png?alt=media\&token=177f4c91-ff4f-4e0a-ba7c-c0b9746edfcc)

![](https://1113999984-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M84NwP_L_qEsBHOa2xo%2Fuploads%2FewHEpjgiNqe3FhxCqLnD%2FScreenshot_4.png?alt=media\&token=6a333d12-fe89-4d45-a22e-97e880d74a38)

Chú ý: Tôi lại thêm một điều kiện vào where để xem

![](broken-reference) ![](https://1113999984-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M84NwP_L_qEsBHOa2xo%2Fuploads%2Fcn0IzIxzsK0HtC5y8WUK%2FScreenshot_5.png?alt=media\&token=6a210ab7-e39e-4e10-849a-9dbb3e429d83)

![](https://1113999984-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M84NwP_L_qEsBHOa2xo%2Fuploads%2FGhOboQJSL6P36V3FAmC3%2FScreenshot_6.png?alt=media\&token=6f617baa-b820-4e71-acbd-3e242ab6fc23)

**Phần 2: Viết phần nâng cao tự động**
