5

我创建了一个名为“技术”的自定义分类法,但无法像使用类别或标签那样查询多个术语。

这些查询确实有效:

query_posts('tag=goldfish,airplanes');

query_posts('technologies=php');

但是,以下任何一项都不能正常工作:

query_posts('technologies=php,sql');

query_posts('technologies=php&technologies=sql');

我的目标:显示所有使用“php”技术的帖子和所有使用“sql”技术的帖子

有任何想法吗?这甚至可能吗?谢谢!

4

9 回答 9

10

显然 query_posts 在这种特定情况下无济于事。(希望它会在未来的 Wordpress 版本中添加!)解决方案是使用自定义选择查询,如下所示:

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 中找到: http: //codex.wordpress.org/Displaying_Posts_Using_a_Custom_Select_Query

于 2009-07-21T21:40:16.493 回答
6

这是一个有点延迟的回复,但它目前在谷歌上是第一次出现“多个词的 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 的第一个贡献,我希望它符合标准。

于 2011-12-17T19:05:54.280 回答
4

你可以使用这个插件:

http://scribu.net/wordpress/query-multiple-taxonomies/

于 2010-07-09T12:35:26.537 回答
1

这行得通吗?query_posts('标签=面包+烘焙+食谱')

来自: http ://codex.wordpress.org/Template_Tags/query_posts

于 2009-07-20T23:03:21.340 回答
1

好的,所以这是我的破解。这有点hacky,但它有效。最大的缺点是需要重新添加任何其他查询变量,因为当调用多个术语时,失败会删除所有查询变量。

此外,我没有针对跨多个分类法的查询进行测试。这仅适用于特定的分类。使用风险自负。

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");
于 2010-01-13T22:28:20.023 回答
0
 //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);
 }
于 2013-10-22T08:44:27.070 回答
0

有点愚蠢的是,在 WP 中实现自定义分类后,没有内置函数可以随意使用它们,而且文档几乎不存在。我一直在寻找解决方案,这个查询解决了它(并让我很开心)。谢谢。

尽管如此,可悲的是,我太笨了(OOP 盲目),无法将它变成一个函数,所以我不会重复一遍。我得到:**Fatal error**: Call to a member function get_results() on a non-object

我想我不知道如何从函数中调用$wpdb 。

于 2009-10-11T23:27:50.997 回答
0

它应该是这样的:

        global $wp_query;
        query_posts(
                array_merge(
                    array('taxonomy' => 'technologies', 'term' => array('sql', 'php')),
                    $wp_query->query
                )
            );

至少适用于自定义 post_types。

于 2010-06-22T18:29:53.903 回答
0

嘿,我也遇到过同样的问题。如果您没有很多多个值,那么您可以通过以下方式进行操作,而不是编写原始 SQL 查询:

$loop = new WP_Query(array('technologies' => 'php','technologies' => 'sql')); 

然后您可以循环访问此处创建的 wp_query 对象。虽然这是一个非常古老的帖子,但我相信你已经解决了这个问题。:)

于 2011-06-03T19:33:32.923 回答