I made this website a while ago, with some custom post types and everything was working OK, now I have to add a new custom post type and made a loop with this new custom post type and the ‘post’ post type.
something like this:
$args = array (
'post_type' => array( 'post', 'newsletter' ),
'posts_per_page' => -1,
'order' => 'DESC'
);
For some reason this is not working…
I have the same array for other post type and is working fine.
$args = array (
'post_type' => array( 'post', 'events' ),
'posts_per_page' => -1,
'order' => 'DESC'
);
Now here is the weird part:
If I have ‘post’-‘events’ (events is an old custom post type) it works, shows both post custom post types,
If I have ‘post’-‘newsletter’ (newsletter is the new custom post type) it only shows post,
If I have ‘events’-‘newsletter’ it only shows events,
If I create a new custom post type ‘newsletter2’,
and If I have ‘newsletter’-‘newsletter2’ it works, shows both custom post types, but if I have ‘post’-‘newsletter2’ it only shows ‘post’
So… it looks like the old custom post types are not working with the new custom post types for some reason… any ideas???
Thanks!!!
Here is the ‘newsletter’ custom post type (by the way all of my custom post types are exactly the same, except that instead of newsletter they have their own name ‘events’, ‘people’, ‘newsletter2’)
function custom_post_newsletter() {
$labels = array(
'name' => __( 'Newsletter' ),
'singular_name' => __( 'New Newsletter' )
);
$args = array(
'labels' => $labels,
'supports' => array( 'title', 'editor', 'thumbnail', ),
'taxonomies' => array( '', 'post_tag' ),
'hierarchical' => false,
'menu_icon' => 'dashicons-format-aside',
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'menu_position' => 7,
'can_export' => true,
'has_archive' => false,
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'page',
'rewrite' => array( 'slug' => _x('newsletter', 'URL Slug', 'theTheme')),
);
register_post_type( 'newsletter', $args );
}
add_action( 'init', 'custom_post_newsletter', 0 );
here is the loop:
$args = array (
'post_type' => array( 'newsletter', 'post' ),
'posts_per_page' => -1,
'order' => 'DESC',
'post_status' => 'any',
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) : while ( $loop->have_posts() ) : $loop->the_post();?>
<li><? the_title(); ?></li> //here only shows post post_type posts.
<?php
// end loop
endwhile;
endif;
wp_reset_query(); ?>
It is really extemely hard to correctly answer your question, even with a bounty.
What really is quite interesting about this whole scenario is that your code stays the same across the board, so your issue is not related to the code used to register your post type.
Lets look at possible issues and defaults
WP_Query
( if you are usingget_posts
, this stays the same,get_posts
usesWP_Query
)has set preset values to its parameters, and some are completely excluded by default if not explicitely set. Lets look at the two which can influence the result:post_type
-> Defaultpost
post_status
-> Defaultspublish
for logged out users,publish
andprivate
for logged in usersAs you have set
post_type
to include your post types, the only other option I can think of, looking at the above, is that thenewsletter
post type posts has either not being published or you have a custom post status assigned to them via a plugin or custom code.You need to explore this possibility. Try adding
'post_status' => 'any'
to your custom query and see if you get posts back from thenewsletter
post type. I’m not entirely sure if this will work if you have a custom post status assigned to the posts.Apart from this, I really don’t have anything to offer regarding this issue.
Perhaps you have a theme and/or plugin that already creates a ‘newsletter’ CPT?
You can use “get post types” to see all the post types you currently have: