{"id":1133,"date":"2024-06-05T20:32:07","date_gmt":"2024-06-05T12:32:07","guid":{"rendered":"https:\/\/cn.raksmart.com\/blog\/host\/4114.html"},"modified":"2024-06-05T20:32:07","modified_gmt":"2024-06-05T12:32:07","slug":"%e8%ae%a9wordpress%e5%90%8e%e5%8f%b0%e6%90%9c%e7%b4%a2%e6%94%af%e6%8c%81%e8%87%aa%e5%ae%9a%e4%b9%89%e5%9f%9fpost-meta","status":"publish","type":"post","link":"https:\/\/cn.raksmart.com\/blog\/website\/wordpress\/1133.html","title":{"rendered":"\u8ba9WordPress\u540e\u53f0\u641c\u7d22\u652f\u6301\u81ea\u5b9a\u4e49\u57dfpost meta"},"content":{"rendered":"<div class=\"entry-content e-content\" itemprop=\"description articleBody\">\n<p>\u3000\u3000<strong>WordPress\u7f51\u7ad9<\/strong>\u7ba1\u7406\u5458\u540e\u53f0\u7684\u641c\u7d22\u529f\u80fd\u672c\u8eab\u53ea\u80fd\u5bf9\u6587\u7ae0\u6807\u9898\u548c\u5185\u5bb9\u8fdb\u884c\u67e5\u8be2\u641c\u7d22\uff0c\u4f46\u5f53\u6211\u4eec\u6709\u6bd4\u8f83\u91cd\u8981\u7684\u81ea\u5b9a\u4e49\u57dfpost meta\u65f6\uff0c\u5c31\u9700\u8981\u652f\u6301\u641c\u7d22\u5305\u542b\u81ea\u5b9a\u4e49\u57df\u7684\u6587\u7ae0\u3002\u4e3a\u4e86\u5b9e\u73b0\u540e\u53f0\u652f\u6301\u5bf9post meta\u81ea\u5b9a\u4e49\u57df\u7684\u641c\u7d22\u6211\u4eec\u9700\u8981\u7528\u5230<strong><a href=\"https:\/\/cn.raksmart.com\/\" target=\"_blank\" rel=\"noreferrer noopener\">WordPress\u7f51\u7ad9<\/a><\/strong>\u7684\u4e09\u4e2aadd_filter\u51fd\u6570posts_join\u3001posts_where\u548cposts_groupby\u5373\u53ef\u5b9e\u73b0\u8be5\u529f\u80fd\u3002\u4ee5\u4e0b\u662f\u5177\u4f53\u4ee3\u7801\uff1a<\/p>\n<p>\u3000\u3000\/*<\/p>\n<p>\u3000\u3000\u6dfb\u52a0<strong>WordPress\u7f51\u7ad9<\/strong>\u540e\u53f0\u641c\u7d22\u652f\u6301post meta\u81ea\u5b9a\u4e49\u57df\u67e5\u8be2<\/p>\n<p>\u3000\u3000\u4ee3\u7801\u6765\u6e90: www.wpzhinan.com\/jiaocheng\/1794.html<\/p>\n<p>\u3000\u3000*\/<\/p>\n<p>\u3000\u3000add_filter( \u2018posts_join\u2019, \u2018posts_join_func\u2019 );<\/p>\n<p>\u3000\u3000add_filter( \u2018posts_where\u2019, \u2018posts_where_func\u2019 );<\/p>\n<p>\u3000\u3000add_filter( \u2018posts_groupby\u2019, \u2018posts_groupby_func\u2019 );<\/p>\n<p>\u3000\u3000\/**<\/p>\n<p>\u3000\u3000* \u4e3a<strong>WordPress\u7f51\u7ad9<\/strong>\u641c\u7d22\u52a0\u5165post meta\u6570\u636e<\/p>\n<p>\u3000\u3000*\/<\/p>\n<p>\u3000\u3000function posts_join_func( $join ) {<\/p>\n<p>\u3000\u3000global $wpdb;<\/p>\n<p>\u3000\u3000if ( ! is_applicable() )<\/p>\n<p>\u3000\u3000return $join;<\/p>\n<p>\u3000\u3000$join .= \u201cLEFT JOIN $wpdb-&gt;postmeta ON $wpdb-&gt;posts.ID = $wpdb-&gt;postmeta.post_id \u201c;<\/p>\n<p>\u3000\u3000return $join;<\/p>\n<p>\u3000\u3000}<\/p>\n<p>\u3000\u3000\/**<\/p>\n<p>\u3000\u3000* \u786e\u4fdd<strong>WordPress\u7f51\u7ad9<\/strong>\u83b7\u53d6\u6307\u5b9a\u7684\u641c\u7d22\u7ed3\u679c<\/p>\n<p>\u3000\u3000*\/<\/p>\n<p>\u3000\u3000function posts_where_func( $where ) {<\/p>\n<p>\u3000\u3000global $wpdb, $wp;<\/p>\n<p>\u3000\u3000if ( ! is_applicable() )<\/p>\n<p>\u3000\u3000return $where;<\/p>\n<p>\u3000\u3000$where = preg_replace(<\/p>\n<p>\u3000\u3000\u201d\/($wpdb-&gt;posts.post_title LIKE \u2018%{$wp-&gt;query_vars[\u2018s\u2019]}%\u2019)\/i\u201d,<\/p>\n<p>\u3000\u3000\u201d$0 OR $wpdb-&gt;postmeta.meta_value LIKE \u2018%{$wp-&gt;query_vars[\u2018s\u2019]}%\u2019 \u201c,<\/p>\n<p>\u3000\u3000$where<\/p>\n<p>\u3000\u3000);<\/p>\n<p>\u3000\u3000return $where;<\/p>\n<p>\u3000\u3000}<\/p>\n<p>\u3000\u3000\/**<\/p>\n<p>\u3000\u3000* \u6839\u636e<strong>WordPress\u7f51\u7ad9<\/strong>\u6587\u7ae0ID\u7ec4\u5408\u7ed3\u679c\uff0c\u5e76\u786e\u4fdd\u4e0d\u83b7\u53d6\u91cd\u590d\u7ed3\u679c<\/p>\n<p>\u3000\u3000*\/<\/p>\n<p>\u3000\u3000function posts_groupby_func( $groupby ) {<\/p>\n<p>\u3000\u3000global $wpdb;<\/p>\n<p>\u3000\u3000if ( ! is_applicable() )<\/p>\n<p>\u3000\u3000return $groupby;<\/p>\n<p>\u3000\u3000if ( empty( $groupby ) )<\/p>\n<p>\u3000\u3000$groupby = \u201c$wpdb-&gt;posts.ID\u201d;<\/p>\n<p>\u3000\u3000return $groupby;<\/p>\n<p>\u3000\u3000}<\/p>\n<p>\u3000\u3000\/**<\/p>\n<p>\u3000\u3000* \u786e\u4fdd<strong>WordPress\u7f51\u7ad9<\/strong>\u5728\u6b63\u786e\u7684\u9875\u9762<\/p>\n<p>\u3000\u3000*\/<\/p>\n<p>\u3000\u3000function is_applicable() {<\/p>\n<p>\u3000\u3000global $pagenow, $wp_query;<\/p>\n<p>\u3000\u3000\/\/ \u68c0\u67e5\u662f\u5426\u5728\u7ba1\u7406\u5458\u540e\u53f0<\/p>\n<p>\u3000\u3000if ( ! is_admin() )<\/p>\n<p>\u3000\u3000return false;<\/p>\n<p>\u3000\u3000\/\/ \u68c0\u67e5\u662f\u5426\u5728\u6587\u7ae0\u7f16\u8f91\u9875<\/p>\n<p>\u3000\u3000if ( \u2018edit.php\u2019 != $pagenow )<\/p>\n<p>\u3000\u3000return false;<\/p>\n<p>\u3000\u3000\/\/ \u68c0\u67e5\u662f\u5426\u641c\u7d22\u5173\u952e\u8bcd\u5df2\u8bbe\u7f6e<\/p>\n<p>\u3000\u3000if ( ! isset( $_GET[\u2018s\u2019] ) )<\/p>\n<p>\u3000\u3000return false;<\/p>\n<p>\u3000\u3000\/\/ \u68c0\u67e5\u662f\u5426\u5728\u641c\u7d22\u9875\u9762<\/p>\n<p>\u3000\u3000if ( ! $wp_query-&gt;is_search )<\/p>\n<p>\u3000\u3000return false;<\/p>\n<p>\u3000\u3000return true;<\/p>\n<p>\u3000\u3000}<\/p>\n<p>\u3000\u3000\u4ee3\u7801\u8bf4\u660e\uff1a\u5c06\u4ee5\u4e0a\u4ee3\u7801\u63d2\u5165\u4e3b\u9898\u7684function.php\u6587\u4ef6\u5373\u53ef\u5b9e\u73b0\u81ea\u5b9a\u4e49\u57dfpost meta\u7684\u641c\u7d22\u4e86\u3002\u4e4b\u540e\u4f60\u53ef\u4ee5\u5c1d\u8bd5\u4ee5\u81ea\u5b9a\u4e49\u57df\u5185\u5bb9\u6765\u641c\u7d22\u770b\u770b\u662f\u5426\u80fd\u6b63\u786e\u5730\u67e5\u8be2\u5230\u7ed3\u679c\u3002\u63a8\u8350\u9605\u8bfb\uff1a<a href=\"https:\/\/cn.raksmart.com\/blog\/website\/wordpress\/1045.html\" target=\"_blank\" rel=\"noreferrer noopener\">\u4f7fWORDPRESS\u7f51\u7ad9\u66f4\u6709\u5229\u4e8eSEO\u7684\u4e09\u4e2a\u9876\u7ea7\u63d2\u4ef6<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u3000\u3000WordPress\u7f51\u7ad9\u7ba1\u7406\u5458\u540e\u53f0\u7684\u641c\u7d22\u529f\u80fd\u672c\u8eab\u53ea\u80fd\u5bf9\u6587\u7ae0\u6807\u9898\u548c\u5185\u5bb9\u8fdb\u884c\u67e5\u8be2\u641c\u7d22\uff0c\u4f46\u5f53\u6211\u4eec\u6709\u6bd4\u8f83\u91cd\u2026 <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23],"tags":[639],"class_list":["post-1133","post","type-post","status-publish","format-standard","hentry","category-wordpress","tag-wordpress"],"_links":{"self":[{"href":"https:\/\/cn.raksmart.com\/blog\/wp-json\/wp\/v2\/posts\/1133","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cn.raksmart.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cn.raksmart.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cn.raksmart.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cn.raksmart.com\/blog\/wp-json\/wp\/v2\/comments?post=1133"}],"version-history":[{"count":0,"href":"https:\/\/cn.raksmart.com\/blog\/wp-json\/wp\/v2\/posts\/1133\/revisions"}],"wp:attachment":[{"href":"https:\/\/cn.raksmart.com\/blog\/wp-json\/wp\/v2\/media?parent=1133"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cn.raksmart.com\/blog\/wp-json\/wp\/v2\/categories?post=1133"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cn.raksmart.com\/blog\/wp-json\/wp\/v2\/tags?post=1133"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}