最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Filter by Post Type

programmeradmin9浏览0评论

I'm trying to figure out how to filter the posts returned by post type. I have a list of checkboxes of my different post types, and using that, I've gotten an array of the post types wanted;

$contentTypes = explode(',', $_POST['contents']);

Which gives me this:

Array ( [0] => post-type1 [1] => post-type2 [2] => post-type3 [3] => post-type4 [4] => post-type5 );

When I set the $args for post_type I want to check it against $contentTypes and only show the ones that are in my array.

I may not have explained this very well. But what would be the best way to do this? If I need to rework what I've done so far that's fine too.

I'm trying to figure out how to filter the posts returned by post type. I have a list of checkboxes of my different post types, and using that, I've gotten an array of the post types wanted;

$contentTypes = explode(',', $_POST['contents']);

Which gives me this:

Array ( [0] => post-type1 [1] => post-type2 [2] => post-type3 [3] => post-type4 [4] => post-type5 );

When I set the $args for post_type I want to check it against $contentTypes and only show the ones that are in my array.

I may not have explained this very well. But what would be the best way to do this? If I need to rework what I've done so far that's fine too.

Share Improve this question asked Jul 2, 2019 at 23:54 trose1189trose1189 1
Add a comment  | 

1 Answer 1

Reset to default 0

You can use WP_Query for that :

$contentTypes = explode(',', $_POST['contents']);
$qry = new WP_Query( array( 
'post_type' => $contentTypes
));

if( $qry->have_posts() ){
while( $qry->have_posts() ) {

$qry->the_post()
// Your code

}

}

Here is the [docs] ( https://codex.wordpress/Class_Reference/WP_Query ) for WP_Query

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论