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.


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() ) {

// Your code



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

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.