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 11 Answer
Reset to default 0You 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
评论列表(0条)