Php 在Wordpress 2.8中查询多个自定义分类术语?
我创建了一个名为“technologies”的自定义分类法,但无法像查询类别或标记那样查询多个术语 这些查询确实有效:Php 在Wordpress 2.8中查询多个自定义分类术语?,php,wordpress,taxonomy,Php,Wordpress,Taxonomy,我创建了一个名为“technologies”的自定义分类法,但无法像查询类别或标记那样查询多个术语 这些查询确实有效: query_posts('tag=goldfish,airplanes'); query_posts('technologies=php'); query_posts('technologies=php,sql'); query_posts('technologies=php&technologies=sql'); 但是,以下两项都不能正常工作: query_p
query_posts('tag=goldfish,airplanes');
query_posts('technologies=php');
query_posts('technologies=php,sql');
query_posts('technologies=php&technologies=sql');
但是,以下两项都不能正常工作:
query_posts('tag=goldfish,airplanes');
query_posts('technologies=php');
query_posts('technologies=php,sql');
query_posts('technologies=php&technologies=sql');
我的目标:显示所有使用“php”技术的帖子,以及所有使用“sql”技术的帖子
有什么想法吗?这可能吗?谢谢 这有用吗?查询帖子('标签=面包+烘焙+配方')
发件人:显然,在这种特殊情况下,查询帖子帮不上忙。(希望在未来版本的Wordpress中添加!)解决方案是使用自定义的select查询,如下所示:
SELECT *
FROM $wpdb->posts
LEFT JOIN $wpdb->term_relationships ON($wpdb->posts.ID = $wpdb->term_relationships.object_id)
LEFT JOIN $wpdb->term_taxonomy ON($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
LEFT JOIN $wpdb->terms ON($wpdb->term_taxonomy.term_id = $wpdb->terms.term_id)
WHERE $wpdb->posts.post_type = 'post'
AND $wpdb->posts.post_status = 'publish'
AND $wpdb->term_taxonomy.taxonomy = 'technologies'
AND $wpdb->terms.slug = 'php' OR $wpdb->terms.slug = 'css'
ORDER BY $wpdb->posts.post_date DESC
更多信息可在Wordpress Codex上找到:
在WP中实现自定义分类法之后,没有内置函数可以随意使用它们,而且文档几乎不存在,这在某种程度上是愚蠢的。我一直在寻找一个解决方案,这个查询解决了这个问题(让我很开心)。谢谢 不过,遗憾的是,我太笨(OOP盲)了,无法将它变成函数,所以我不会重复它。 我得到:
**致命错误**:对非对象调用成员函数get_results()
我想我不知道如何在函数中调用$wpdb。好的,我的问题就在这里。这有点老套,但很管用。最大的缺点是需要重新添加任何其他查询变量,因为当调用多个术语时,fail会删除所有查询变量 此外,我没有针对跨多个分类法的查询进行测试。这只适用于特定的分类法。使用风险自负
function multi_tax_terms($where) {
global $wp_query;
if ( strpos($wp_query->query_vars['term'], ',') !== false && strpos($where, "AND 0") !== false ) {
// it's failing because taxonomies can't handle multiple terms
//first, get the terms
$term_arr = explode(",", $wp_query->query_vars['term']);
foreach($term_arr as $term_item) {
$terms[] = get_terms($wp_query->query_vars['taxonomy'], array('slug' => $term_item));
}
//next, get the id of posts with that term in that tax
foreach ( $terms as $term ) {
$term_ids[] = $term[0]->term_id;
}
$post_ids = get_objects_in_term($term_ids, $wp_query->query_vars['taxonomy']);
if ( !is_wp_error($post_ids) && count($post_ids) ) {
// build the new query
$new_where = " AND wp_posts.ID IN (" . implode(', ', $post_ids) . ") ";
// re-add any other query vars via concatenation on the $new_where string below here
// now, sub out the bad where with the good
$where = str_replace("AND 0", $new_where, $where);
} else {
// give up
}
}
return $where;
}
add_filter("posts_where", "multi_tax_terms");
应该是这样的:
global $wp_query;
query_posts(
array_merge(
array('taxonomy' => 'technologies', 'term' => array('sql', 'php')),
$wp_query->query
)
);
这至少适用于自定义帖子类型。您可以使用此插件:
嘿,我也曾经遇到过同样的问题。如果没有很多多个值,则可以通过以下方式执行,而不是编写原始SQL查询:
$loop = new WP_Query(array('technologies' => 'php','technologies' => 'sql'));
然后,您可以在这里创建的wp_查询对象中循环。
虽然这是一个非常古老的帖子,我相信你已经解决了这个问题。:) 这是一个有点延迟的回复,但这是谷歌目前第一次发布“wordpress相关多术语帖子”,所以我想我会贡献我的发现 自从这个问题发布后,Wordpress已经被修改为允许这种类型的查询。这将为您提供与指定给对象的任何自定义分类术语相关的帖子列表:
$post_cats = wp_get_object_terms(get_the_ID(), 'video_category', array('fields' => 'ids'));
$args=array(
"tax_query" => array(
array(
"taxonomy" => "video_category",
"field" => "id",
"terms" => $post_cats
)
),
'post__not_in' => array(get_the_ID()),
'post_type' => 'video',
'posts_per_page' => 8,
'caller_get_posts' => 1
);
$related_by_cats = new WP_Query($args);
这是我对SO的第一次贡献,我希望它达到标准。//相当于获得\u帖子
//equivalent to get_posts
function brand_get_posts($args=array()){
global $wpdb;
$sql = "SELECT p.* ";
$sql.= " FROM $wpdb->posts p";
$sql.= " LEFT JOIN $wpdb->term_relationships term_r ON(p.ID = term_r.object_id)";
$sql.= " LEFT JOIN $wpdb->term_taxonomy term_t ON(term_r.term_taxonomy_id = term_t.term_taxonomy_id)";
$sql.= " LEFT JOIN $wpdb->terms terms ON(term_t.term_id = terms.term_id)";
$sql.= " WHERE 1=1 ";
if(!empty($args['post_type'])){
$sql.= " AND p.post_type = '".$args['post_type']."'";
}
$sql.= " AND p.post_status = 'publish'";
if(!empty($args['taxonomy'])){
$sql.= " AND term_t.taxonomy = '".$args['taxonomy']."'";
}
if(!empty($args['terms'])&&is_array($args['terms'])){
$sql.= " AND terms.slug IN ('".implode(",",$args['terms'])."')";
}
$sql.= " ORDER BY p.post_date DESC";
if(!empty($args['posts_per_page'])){
$sql.=" LIMIT ".$args['posts_per_page'];
}
if(!empty($args['offset'])){
$sql.=" OFFSET ".$args['offset'];
}
//echo '<h1>'.$sql.'</h1>';
return $wpdb->get_results($sql);
}
函数brand_get_posts($args=array()){
全球$wpdb;
$sql=“选择p.*”;
$sql.=“从$wpdb->posts p”;
$sql.=“左连接$wpdb->term_关系term_r ON(p.ID=term_r.object_ID)”;
$sql.=“左连接$wpdb->term\u taxonomy term\u ON(term\u r.term\u taxonomy\u id=term\u t.term\u taxonomy\u id)”;
$sql.=“左连接$wpdb->terms ON(term_t.term_id=terms.term_id)”;
$sql.=“其中1=1”;
如果(!empty($args['post_type'])){
$sql.=“和p.post_type=”。$args['post_type']。”;
}
$sql.=“和p.post_状态='publish'”;
如果(!empty($args['taxonomy'])){
$sql.=“和术语_t.taxonomy=”$args['taxonomy']。”;
}
if(!empty($args['terms'])和&is_数组($args['terms'])){
$sql.=“AND terms.slug IN(”)。内爆(“,”,$args['terms'])。“”);
}
$sql.=“按p.post\U日期描述订购”;
如果(!empty($args['posts\u per\u page'])){
$sql.=“限制”。$args['posts\u per\u page'];
}
如果(!empty($args['offset'])){
$sql.=“偏移量”。$args['OFFSET'];
}
//回显“.$sql.”;
返回$wpdb->get_results($sql);
}
不幸的是,query_posts('technologies=css+html');不返回任何帖子,即使有几个帖子与这两个术语都关联。我必须在函数中添加“global$wpdb;”。我真傻。在发布上述条目后立即意识到。像上面这样的手动查询正是我最终使用的。现在,如果你不想一个接一个地运行多个get_posts()查询(你不想),这是唯一可行的方法。这个查询看起来可能适用于我正在尝试做的事情,但是如果我想在一篇文章上搜索多个词呢?我试着将OR改为AND(在查询的第9行),但似乎不起作用。+1:这个插件对我来说立即有效,没有问题!