{"id":5235,"date":"2024-10-16T16:27:33","date_gmt":"2024-10-16T09:27:33","guid":{"rendered":"https:\/\/dev.artru.net\/?p=5235"},"modified":"2024-10-16T21:09:53","modified_gmt":"2024-10-16T14:09:53","slug":"code-tim-kiem-theo-tieu-de-cua-bai-viet-trong-wordpress","status":"publish","type":"post","link":"https:\/\/artru.net\/en\/code-tim-kiem-theo-tieu-de-cua-bai-viet-trong-wordpress\/","title":{"rendered":"Code to search by post title in WordPress"},"content":{"rendered":"<p>By default WordPress search, it will compare the search query with the title. <code data-no-translation=\"\" data-no-auto-translation=\"\">post_title<\/code>, excerpt <code data-no-translation=\"\" data-no-auto-translation=\"\">post_excerpt<\/code> and content <code data-no-translation=\"\" data-no-auto-translation=\"\">post_content<\/code>.<\/p>\n\n\n\n<p>In some cases, you only need search results by article title, so you have to write additional custom code to filter the query.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Here is a code snippet that filters the query by title only. You can add it to your file <code data-no-translation=\"\" data-no-auto-translation=\"\">function.php<\/code> or plugin <a href=\"https:\/\/wordpress.org\/plugins\/code-snippets\/\" target=\"_blank\" rel=\"noreferrer noopener\">Code Snippets<\/a>.<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\" data-no-translation=\"\" data-no-auto-translation=\"\"><code data-no-translation=\"\" data-no-auto-translation=\"\">function ARTRU_Search_By_Title($columns, $search, $query) {\n\treturn &#91;'post_title'];\n}\nadd_filter('post_search_columns', 'ARTRU_Search_By_Title', 10, 3);<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>If you need more detailed filters, for example Vietnamese with accents, use the code below.<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\" data-no-translation=\"\" data-no-auto-translation=\"\"><code data-no-translation=\"\" data-no-auto-translation=\"\">function ARTRU_Search_By_Title_With_Special_Characters($search, $wp_query) {\n    global $wpdb;\n    $search_term = $wp_query->query_vars&#91;'s'];\n    if ($search_term) {\n        $search = $wpdb->prepare(\n            \" AND LOWER({$wpdb->posts}.post_title) LIKE BINARY LOWER(%s) \",\n            '%' . $wpdb->esc_like($search_term) . '%'\n        );\n    }\n    return $search;\n}\nadd_filter('posts_search', 'ARTRU_Search_By_Title_With_Special_Characters', 10, 2);<\/code><\/pre>","protected":false},"excerpt":{"rendered":"<p>Theo t\u00ecm ki\u1ebfm m\u1eb7c \u0111\u1ecbnh c\u1ee7a WordPress, n\u00f3 s\u1ebd so s\u00e1nh truy v\u1ea5n t\u00ecm ki\u1ebfm v\u1edbi ti\u00eau \u0111\u1ec1 post_title, \u0111o\u1ea1n tr\u00edch post_excerpt v\u00e0 n\u1ed9i dung post_content. Trong m\u1ed9t v\u00e0i tr\u01b0\u1eddng [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-5235","post","type-post","status-publish","format-standard","hentry","category-website"],"_links":{"self":[{"href":"https:\/\/artru.net\/en\/wp-json\/wp\/v2\/posts\/5235","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/artru.net\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/artru.net\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/artru.net\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/artru.net\/en\/wp-json\/wp\/v2\/comments?post=5235"}],"version-history":[{"count":0,"href":"https:\/\/artru.net\/en\/wp-json\/wp\/v2\/posts\/5235\/revisions"}],"wp:attachment":[{"href":"https:\/\/artru.net\/en\/wp-json\/wp\/v2\/media?parent=5235"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/artru.net\/en\/wp-json\/wp\/v2\/categories?post=5235"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/artru.net\/en\/wp-json\/wp\/v2\/tags?post=5235"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}