User_FTW
User_FTW

Reputation: 524

WordPress custom post type array not working as expcted

I'm using this code...

function include_post_types() {
$post_types = get_post_types( array (
'show_ui'       => true
), 
'objects' );
$return = '';
foreach ( $post_types as $post_type ) {
    $my_sitemap_options = get_option( 'my_sitemap_settings' ); 
    $post_type_name = $post_type->name;
    if ($my_sitemap_options['post_types'] && in_array($post_type_name, $my_sitemap_options['post_types'])) { 
        $the_excluded = $post_type_name;
        $return .= "'" . $the_excluded . "', ";
    } 
}
return $return;

}

... to return a list of custom post types that I have selected from an options page. This works fine, and if I do this...

echo included_post_types();

...I see this...

'clothes', 'shoes', 'jackets', 

...which is what I excpected.

But the problem is when I try to use included_post_types() in a loop to only show posts with those post types, like this:

$sitemap_post_args = array(
        'post_type'         => array(included_post_types()),
        'posts_per_page'    => -1,
        'orderby'           =>'post_type',
        'order'             =>'asc'
    );

    $loop = new WP_Query($sitemap_post_args);
    global $post_type;
    global $post;

    echo '<ul>';
    $last_post_type = '';
    while($loop->have_posts()): $loop->the_post(); 
        $current_post_type = $post->post_type;
        $current_post_type_object = get_post_type_object( $current_post_type );
        if($current_post_type != $last_post_type) echo "<br /><li><strong>" . $current_post_type_object->labels->name . "</strong></li>";?>
            <li><a href="<?php echo get_the_permalink(); ?>"><?php echo get_the_title(); ?></a></li>
        <?php echo "\n";
        $last_post_type = $current_post_type;
    endwhile;
    wp_reset_query();
    echo '</ul>';

It simply doesn't display anything on my page, but it doesn't throw an error either.

I'm almost certain the problem is this line:

'post_type' => array(included_post_types()),

I even tried it like this...

'post_type' => included_post_types(),

...but it did not work.

If I try this...

'post_type' => array('clothes', 'shoes', 'jackets', ),

...it works, but I need to be able to use the function name.

Any suggestions would be helpful.

Upvotes: 0

Views: 228

Answers (1)

aniket patel
aniket patel

Reputation: 41

please replace below code with yours. It should be help.

function include_post_types() {
$post_types = get_post_types( array (
'show_ui'       => true
), 
'objects' );
$return = array();
foreach ( $post_types as $post_type ) {
    $my_sitemap_options = get_option( 'my_sitemap_settings' ); 
    $post_type_name = $post_type->name;
    if ($my_sitemap_options['post_types'] && in_array($post_type_name, $my_sitemap_options['post_types'])) { 
        $the_excluded = $post_type_name;
        $return[] = $the_excluded;
    } 
}
return $return;
}

and also replace below code that show posts

$post_type_list = include_post_types();
$sitemap_post_args = array(
    'post_type'         => $post_type_list,
    'posts_per_page'    => -1,
    'orderby'           =>'post_type',
    'order'             =>'asc'
);

Upvotes: 1

Related Questions