我正在尝试为wordpress编写自定义选择帖子查询。我正在使用woocommerce。在woocommerce中,默认的帖子类型名称为" product"和#34; product_cat"中的分类名称; 我已经添加了另一个分类名称" spec"用于产品规格。
所以在我的产品类别中假设" samsung","笔记本电脑"是产品类别的术语名称。并且在" spec"我有一些规范,例如它的核心i5处理器"," 8gb ddr3 ram"我的帖子标题是"超级笔记本"
我想搜索其中的任何一个字段,就像在我的搜索字段中输入笔记本一样,它会显示所有笔记本。在搜索输入类型文本区域下面,我还有一个选择框,其中包含所有" product_cat"有三星,惠普,苹果,笔记本电脑等。
产品名称/帖子标题
product_cat
规范
8gb ddr3 ram
全球$ wpdb;
$query = "SELECT DISTINCT wp_posts.*
FROM wp_posts, wp_term_relationships, wp_term_taxonomy, wp_terms
WHERE (wp_terms.name LIKE '%ddr3 ram%'
OR wp_posts.post_title LIKE '%ddr3 ram%')
AND wp_posts.post_status = 'publish'
AND wp_posts.post_type = 'product'
AND wp_posts.ID = wp_term_relationships.object_id
AND wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id
AND wp_term_taxonomy.term_id = wp_terms.term_id GROUP BY wp_posts.ID ORDER BY wp_posts.post_title ASC";
$object = $wpdb->get_results($query);
_log($object);
我尝试使用此代码显示结果。
但如果我试试这个,它就不会显示结果
global $wpdb;
$query = "SELECT DISTINCT wp_posts.*
FROM wp_posts, wp_term_relationships, wp_term_taxonomy, wp_terms
WHERE (wp_terms.name LIKE '%ddr3 ram%'
AND wp_terms.name LIKE '%samsung%'
OR wp_posts.post_title LIKE '%ddr3 ram%')
AND wp_posts.post_status = 'publish'
AND wp_posts.post_type = 'product'
AND wp_posts.ID = wp_term_relationships.object_id
AND wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id
AND wp_term_taxonomy.term_id = wp_terms.term_id GROUP BY wp_posts.ID ORDER BY wp_posts.post_title ASC";
$object = $wpdb->get_results($query);
_log($object);
我不知道如何解决这个问题。所以任何人都想帮助我,请给我这个答案。 感谢。
答案 0 :(得分:0)
你好Faysal bhai你应该试试这个
global $wpdb;
$query = "SELECT DISTINCT wp_posts.*
FROM wp_posts, wp_term_relationships, wp_term_taxonomy, wp_terms
WHERE (wp_terms.name LIKE '%ddr3 ram%'
OR wp_terms.name LIKE '%samsung%'
OR wp_posts.post_title LIKE '%ddr3 ram%')
AND wp_posts.post_status = 'publish'
AND wp_posts.post_type = 'product'
AND wp_posts.ID = wp_term_relationships.object_id
AND wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id
AND wp_term_taxonomy.term_id = wp_terms.term_id GROUP BY wp_posts.ID ORDER BY wp_posts.post_title ASC";
$object = $wpdb->get_results($query);
_log($object);
我希望你能找到解决方案