{"id":1020,"date":"2023-06-02T21:45:17","date_gmt":"2023-06-02T13:45:17","guid":{"rendered":"https:\/\/zhuangyejianzhan.com\/?p=1020"},"modified":"2023-06-02T21:45:19","modified_gmt":"2023-06-02T13:45:19","slug":"wordpress-%e6%90%9c%e7%b4%a2%e9%a1%b5%e6%94%af%e6%8c%81%e6%90%9c%e7%b4%a2%e8%87%aa%e5%ae%9a%e4%b9%89%e5%ad%97%e6%ae%b5%e7%9a%84%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"https:\/\/zhuangyejianzhan.com\/es\/1020\/wordpress-%e6%90%9c%e7%b4%a2%e9%a1%b5%e6%94%af%e6%8c%81%e6%90%9c%e7%b4%a2%e8%87%aa%e5%ae%9a%e4%b9%89%e5%ad%97%e6%ae%b5%e7%9a%84%e6%96%b9%e6%b3%95\/","title":{"rendered":"WordPress \u641c\u7d22\u9875\u652f\u6301\u641c\u7d22\u81ea\u5b9a\u4e49\u5b57\u6bb5\u7684\u65b9\u6cd5"},"content":{"rendered":"<h2 class=\"wp-block-heading\" id=\"in1\">LEFT JOIN \u65b9\u6cd5<\/h2>\n\n\n\n<p>\u6b64\u65b9\u6cd5\u5206\u4e3a\u4e09\u6b65<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"in2\">1\u3001Left Join<\/h3>\n\n\n\n<p>\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0cWordPress \u641c\u7d22\u529f\u80fd\u8bbe\u7f6e\u4e3a\u4ec5\u641c\u7d22<code>posts<\/code>\u8868\uff0c\u4e3a\u4e86\u5728\u6211\u4eec\u7684\u641c\u7d22\u4e2d\u5305\u542b\u81ea\u5b9a\u4e49\u5b57\u6bb5\u6570\u636e\uff0c\u6211\u4eec\u9996\u5148\u9700\u8981\u5bf9\u6570\u636e\u5e93\u4e2d\u7684<code>postmeta<\/code>\u8868\u6267\u884c\u5de6\u8fde\u63a5\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"in3\">2\u3001\u4fee\u6539\u67e5\u8be2\u6761\u4ef6<\/h3>\n\n\n\n<p>\u4fee\u6539 WordPress \u7684\u641c\u7d22\u67e5\u8be2\u5305\u542b\u81ea\u5b9a\u4e49\u5b57\u6bb5\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"in4\">3\u3001\u9632\u6b62\u91cd\u590d<\/h3>\n\n\n\n<p>\u6b64\u65b9\u6cd5\u67e5\u8be2\u51fa\u7684\u6570\u636e\u4f1a\u4ea7\u751f\u91cd\u590d\u9879\uff0c\u6211\u4eec\u9700\u8981\u5728&nbsp;<code>SQL<\/code>&nbsp;\u67e5\u8be2\u4e2d\u6dfb\u52a0<code>&nbsp;DISTINCT<\/code>&nbsp;\u5173\u952e\u5b57\uff0c\u4ee5\u9632\u6b62\u8fd4\u56de\u91cd\u590d\u9879\u3002<\/p>\n\n\n\n<p>\u5b8c\u6574\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/**\r\n * WordPress \u641c\u7d22\u9875\u652f\u6301\u641c\u7d22\u81ea\u5b9a\u4e49\u5b57\u6bb5\u7684\u65b9\u6cd5\r\n * \u539f\u6587\u5730\u5740\uff1ahttps:\/\/www.iowen.cn\/wordpress-search-page-supports-custom-fields\/\r\n *\r\n * \u4fee\u6539\u641c\u7d22\u67e5\u8be2\u7684sql\u4ee3\u7801\uff0c\u5c06postmeta\u8868\u5de6\u94fe\u63a5\u8fdb\u53bb\u3002\r\n *\/\r\nfunction io_search_join( $join, $query ) {\r\n    global $wpdb;\r\n    if ( is_search() &amp;&amp; $query->is_main_query() &amp;&amp; !empty($query->query&#91;'s']) ) {\r\n        $join .=' LEFT JOIN '. $wpdb->postmeta . ' AS post_metas ON ' . $wpdb->posts . '.ID = post_metas.post_id ';\r\n    }\r\n    return $join;\r\n}\r\nadd_filter('posts_join', 'io_search_join',10,2 );\r\n\r\n\/**\r\n * \u5728wordpress\u67e5\u8be2\u4ee3\u7801\u4e2d\u52a0\u5165\u81ea\u5b9a\u4e49\u5b57\u6bb5\u503c\u7684\u67e5\u8be2\u3002\r\n *\/\r\nfunction io_search_where( $where, $query ) {\r\n    global $pagenow, $wpdb;\r\n    if ( is_search() &amp;&amp; $query->is_main_query() &amp;&amp; !empty($query->query&#91;'s']) ) {\r\n        $meta_key = \"'_sites_link','_spare_sites_link','_seo_desc','_sescribe','_down_list'\"; \/\/\u9700\u641c\u7d22\u7684\u81ea\u5b9a\u4e49\u5b57\u6bb5\u952e\u540d\r\n        $where = preg_replace(\"\/\\(\\s*\".$wpdb->posts.\".post_title\\s+LIKE\\s*(\\'&#91;^\\']+\\')\\s*\\)\/\",\"({$wpdb->posts}.post_title LIKE $1) OR ((post_metas.meta_value LIKE $1) AND (post_metas.meta_key IN ({$meta_key})))\", $where ); \r\n    }\r\n    return $where;\r\n}\r\nadd_filter('posts_where', 'io_search_where',10,2);\r\n\r\n\/**\r\n * \u5728 SQL \u67e5\u8be2\u4e2d\u6dfb\u52a0 DISTINCT \u5173\u952e\u5b57\uff0c\u4ee5\u9632\u6b62\u8fd4\u56de\u91cd\u590d\u9879\r\n *\/\r\nfunction io_search_distinct( $where, $query) {\r\n    global $wpdb;\r\n    if ( is_search() &amp;&amp; $query->is_main_query() &amp;&amp; !empty($query->query&#91;'s']) )  {\r\n        return 'DISTINCT';\r\n    }\r\n    return $where;\r\n}\r\nadd_filter( \"posts_distinct\", \"io_search_distinct\",10,2 );<\/code><\/pre>","protected":false},"excerpt":{"rendered":"<p>LEFT JOIN \u65b9\u6cd5 \u6b64\u65b9\u6cd5\u5206\u4e3a\u4e09\u6b65 1\u3001Left Join \u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0cWordPress \u641c\u7d22\u529f\u80fd\u8bbe\u7f6e [&hellip;]<\/p>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-1020","post","type-post","status-publish","format-standard","hentry","category-blog"],"_links":{"self":[{"href":"https:\/\/zhuangyejianzhan.com\/es\/wp-json\/wp\/v2\/posts\/1020","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zhuangyejianzhan.com\/es\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zhuangyejianzhan.com\/es\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zhuangyejianzhan.com\/es\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zhuangyejianzhan.com\/es\/wp-json\/wp\/v2\/comments?post=1020"}],"version-history":[{"count":1,"href":"https:\/\/zhuangyejianzhan.com\/es\/wp-json\/wp\/v2\/posts\/1020\/revisions"}],"predecessor-version":[{"id":1021,"href":"https:\/\/zhuangyejianzhan.com\/es\/wp-json\/wp\/v2\/posts\/1020\/revisions\/1021"}],"wp:attachment":[{"href":"https:\/\/zhuangyejianzhan.com\/es\/wp-json\/wp\/v2\/media?parent=1020"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zhuangyejianzhan.com\/es\/wp-json\/wp\/v2\/categories?post=1020"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zhuangyejianzhan.com\/es\/wp-json\/wp\/v2\/tags?post=1020"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}