php - Filter by Post Type

I'm trying to figure out how to filter the posts returned by post type. I have a list of checkboxes of my different

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745350400a4623798.html

相关推荐

  • php - Filter by Post Type

    I'm trying to figure out how to filter the posts returned by post type. I have a list of checkboxes of my different

    7小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信