0

I know it may sound very strange as a question, but I would like to know if it is possible to insert the result of a foreach in WP_query

How:

I would like to avoid doing the maintenance manually that every custom post type created must add the name of the custom post type to the post_type query, to be clear:

'post_type' => array('CUSTOM_POST_TYPE_ONE,CUSTOM_POST_TYPE_TWO'),

but instead get me the whole list of custom post types:

<?php
$args = array(
    'public'   => true,
    '_builtin' => false
);

$output = 'names'; // 'names' or 'objects' (default: 'names')
$operator = 'and'; // 'and' or 'or' (default: 'and')

$post_types = get_post_types( $args, $output, $operator );
echo $post_types;
if ( $post_types ) { // If there are any custom public post types.

    echo '<ul>';

    foreach ( $post_types  as $post_type ) {
        echo '<li>' . $post_type . '</li>';
    }

    echo '<ul>';

}
?>

and insert it into:

'post_type' =>

so that every time I create a custom post type I get a dynamic query

  • it's not really clear where you want the post types to be included. generally you would use a query filter for this and check the context within the filter. – majick Oct 1 '19 at 6:09
  • @majick Yes probably the explanation is not clear, I would like to know if there is a tool that allows that every time a new custom backend type is added from a backend it is automatically included in the query in post_type => without adding it manually – dcer Oct 1 '19 at 10:17

Your Answer

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

Browse other questions tagged or ask your own question.