0

我想在 the_loop 在 word press 开始之前过滤我的查询结果。我只希望 term_id 或 term_taxonomy_id 等于 1 的结果。

query_posts('post_type=my_post&term_id=1&posts_per_page=6');

这将返回所有行,我是 word press 的新手,所以我不确定我们是否可以在 query_posts 中使用 term_id。还有其他方法吗?

4

2 回答 2

0

实际上,帖子类型不是分类法,帖子类型基本上是post, page, Attachment,Revision和创建我们使用的自定义帖子类型Navigation Menu,所以你没有创建自定义帖子类型(我从你的评论中得到)相反,您已经使用register_taxonomy()函数创建了一个自定义分类法,在这种情况下,第一个参数是分类法名称,第二个参数是分类法的对象类型的名称,并且可以内置对象类型帖子类型或任何可能已经注册的自定义帖子类型。因此,根据您在下面给出的示例Custom Post Typesregister_post_type()register_taxonomy()

register_taxonomy( 
    'mumG', 
    'mumT', 
    array( 
        'label' => __( 'Label' ),
        'show_ui' => false,
    ) 
);

在上面的代码中,您创建了一个自定义分类,它的名称是mumG,第二个参数mumT用作分类的对象类型,WordPress除非您不注册/创建,否则似乎没有这样的对象(post_type)一种使用register_post_type()函数的自定义帖子类型。因此,如果您不想为此分类创建自定义帖子类型,那么您应该使用内置帖子类型post代替mumTas 帖子类型,并且可以使用以下query来检索

$args = array( 
        'post_type' => 'post', // if post_type is post
        'showposts' => -1,
        'tax_query' => array(
        array(
            'taxonomy' => 'mumG',
            'terms' => 1,
            'field' => 'term_id',
        )
    ),
    'orderby' => 'title',
    'order' => 'ASC' 
);
query_posts( $args );

否则,如果要使用自定义帖子类型,则必须先创建自定义帖子类型,然后使用自定义帖子的名称代替post(第二个参数),例如

register_taxonomy( 
    'mumG', 
    'yourCustomPostName', // if you have a post_type of 'yourCustomPostName'
    ......
);

'post_type' => 'yourCustomPostName'在查询中使用。请阅读这些文档以更好地理解Codex 上的register_taxonomy()register_post_type()query_posts() 。

于 2013-05-19T21:26:02.063 回答
0

将分类类别名称 (sliders_category) 替换为您的分类类别名称。

$args = array(
                'tax_query' => array(
                                    array(
                                            'taxonomy' => 'sliders_category',
                                            'field' => 'id',
                                            'terms' => '1'
                                          )

                            ),
                'post_type'=>'my_post',
                'order'=>'ASC',
                'posts_per_page'=>6

                );

query_posts($args);
于 2013-05-20T04:54:58.427 回答