获取特定类别的所有自定义帖子类型帖子和常规帖子的列表(Getting list of all custom post type posts and regular posts from a certain category)

我正在尝试从名为social的自定义帖子类型以及社交类别中的所有正常帖子中获取所有帖子的列表。 我目前正在使用以下内容:

$posts = get_posts( array( 'post_type' => array('social', 'post'), 'category_name' => 'social', 'post_status' => 'publish' ) );

这似乎只是返回社交类别中的帖子,而不是社交帖子类型。 无论如何要返回两个post_types?

I'm trying to get a list of all posts from a custom post type called social, and any normal posts within the category social. I currently am using the following:

$posts = get_posts( array( 'post_type' => array('social', 'post'), 'category_name' => 'social', 'post_status' => 'publish' ) );

This seems to only be returning posts within the social category, not the social post type. Anyway to return both post_types?

最满意答案

生成的查询期望所有条件都为真: post_type为social或post AND category_name为要publish social和post_status 。

一个简单的解决方案是使用WP_Query代替:

$query = new WP_Query(array( 'post_type' => array('social', 'post'), 'category_name' => 'social', 'post_status' => 'publish' )); $posts = $query->posts;

然后,您可以使用posts_where过滤器来修改查询的where部分:

add_filter( 'posts_where', 'my_posts_where', 10, 2 ); function my_posts_where( $where, $query ) { ...modify $where... return $where; }

我将跳过字符串操作部分,因为那里没有任何具体内容。 无论如何, $where会看起来像这样(用我的WordPress安装测试):

"AND ( wp_term_relationships.term_taxonomy_id IN (<number>)) AND wp_posts.post_type IN ('social', 'post') AND ((wp_posts.post_status = 'publish'))"

你需要将它转换成这样的东西:

"AND ( ((wp_term_relationships.term_taxonomy_id IN (<number>) AND wp_posts.post_type = 'post') OR (wp_posts.post_type = 'social')) AND (wp_posts.post_status = 'publish'))"

最后,您需要确保my_posts_where不会破坏其他查询。 我可能只是在$query对象中添加一些指示符来标识过滤器中的这个特定查询。

The generated query expects all the conditions to be true: post_typeto be either social or post AND category_name to be social AND post_status to be publish.

One simple solution is to use WP_Query instead:

$query = new WP_Query(array( 'post_type' => array('social', 'post'), 'category_name' => 'social', 'post_status' => 'publish' )); $posts = $query->posts;

Then, you may use a posts_where filter to modify the where part of the query:

add_filter( 'posts_where', 'my_posts_where', 10, 2 ); function my_posts_where( $where, $query ) { ...modify $where... return $where; }

I'll skip the string manipulation part, because there is nothing specific there. Anyhow, $where will look something like this (tested with my WordPress installation):

"AND ( wp_term_relationships.term_taxonomy_id IN (<number>)) AND wp_posts.post_type IN ('social', 'post') AND ((wp_posts.post_status = 'publish'))"

You need to convert it to something like this:

"AND ( ((wp_term_relationships.term_taxonomy_id IN (<number>) AND wp_posts.post_type = 'post') OR (wp_posts.post_type = 'social')) AND (wp_posts.post_status = 'publish'))"

Finally, you need to make sure that my_posts_where does not break the other queries. I might just add some indicator to the $query object to identify this particular query in the filter.

获取特定类别的所有自定义帖子类型帖子和常规帖子的列表(Getting list of all custom post type posts and regular posts from a certain category)

我正在尝试从名为social的自定义帖子类型以及社交类别中的所有正常帖子中获取所有帖子的列表。 我目前正在使用以下内容:

$posts = get_posts( array( 'post_type' => array('social', 'post'), 'category_name' => 'social', 'post_status' => 'publish' ) );

这似乎只是返回社交类别中的帖子,而不是社交帖子类型。 无论如何要返回两个post_types?

I'm trying to get a list of all posts from a custom post type called social, and any normal posts within the category social. I currently am using the following:

$posts = get_posts( array( 'post_type' => array('social', 'post'), 'category_name' => 'social', 'post_status' => 'publish' ) );

This seems to only be returning posts within the social category, not the social post type. Anyway to return both post_types?

最满意答案

生成的查询期望所有条件都为真: post_type为social或post AND category_name为要publish social和post_status 。

一个简单的解决方案是使用WP_Query代替:

$query = new WP_Query(array( 'post_type' => array('social', 'post'), 'category_name' => 'social', 'post_status' => 'publish' )); $posts = $query->posts;

然后,您可以使用posts_where过滤器来修改查询的where部分:

add_filter( 'posts_where', 'my_posts_where', 10, 2 ); function my_posts_where( $where, $query ) { ...modify $where... return $where; }

我将跳过字符串操作部分,因为那里没有任何具体内容。 无论如何, $where会看起来像这样(用我的WordPress安装测试):

"AND ( wp_term_relationships.term_taxonomy_id IN (<number>)) AND wp_posts.post_type IN ('social', 'post') AND ((wp_posts.post_status = 'publish'))"

你需要将它转换成这样的东西:

"AND ( ((wp_term_relationships.term_taxonomy_id IN (<number>) AND wp_posts.post_type = 'post') OR (wp_posts.post_type = 'social')) AND (wp_posts.post_status = 'publish'))"

最后,您需要确保my_posts_where不会破坏其他查询。 我可能只是在$query对象中添加一些指示符来标识过滤器中的这个特定查询。

The generated query expects all the conditions to be true: post_typeto be either social or post AND category_name to be social AND post_status to be publish.

One simple solution is to use WP_Query instead:

$query = new WP_Query(array( 'post_type' => array('social', 'post'), 'category_name' => 'social', 'post_status' => 'publish' )); $posts = $query->posts;

Then, you may use a posts_where filter to modify the where part of the query:

add_filter( 'posts_where', 'my_posts_where', 10, 2 ); function my_posts_where( $where, $query ) { ...modify $where... return $where; }

I'll skip the string manipulation part, because there is nothing specific there. Anyhow, $where will look something like this (tested with my WordPress installation):

"AND ( wp_term_relationships.term_taxonomy_id IN (<number>)) AND wp_posts.post_type IN ('social', 'post') AND ((wp_posts.post_status = 'publish'))"

You need to convert it to something like this:

"AND ( ((wp_term_relationships.term_taxonomy_id IN (<number>) AND wp_posts.post_type = 'post') OR (wp_posts.post_type = 'social')) AND (wp_posts.post_status = 'publish'))"

Finally, you need to make sure that my_posts_where does not break the other queries. I might just add some indicator to the $query object to identify this particular query in the filter.