Custom post type displays when user is authenticated but disappears for anonymous visitors

I’m working on a WordPress site and facing a weird issue. I have a custom template that shows a specific post type, and everything works fine when I’m logged into the admin area. But when I log out and view the same page as a regular visitor, no posts show up at all.

The posts are definitely published and set to public visibility. They appear correctly in other parts of the site. Here’s my template code:

<?php /*
Template Name: Courses Display
*/ ?>

<?php get_header(); ?>

<div class="content-wrapper">
    <div class="container">
        <h1><?php the_title(); ?></h1>
        
        <div class="course-grid">
            <?php
            $course_query = new WP_Query(array(
                'post_type' => 'course_listings',
                'post_status' => 'publish',
                'meta_key' => 'course_priority',
                'orderby' => 'meta_value_num',
                'order' => 'DESC',
                'posts_per_page' => -1
            ));
            
            if ($course_query->have_posts()) {
                while ($course_query->have_posts()) {
                    $course_query->the_post();
                    $featured_image = get_post_meta(get_the_ID(), 'course_thumbnail', true);
            ?>
                    <div class="course-item">
                        <?php if ($featured_image) { ?>
                            <img src="<?php echo $featured_image; ?>" alt="Course Image" />
                        <?php } ?>
                        
                        <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
                        <?php the_excerpt(); ?>
                        <a href="<?php the_permalink(); ?>" class="read-more">Learn More</a>
                    </div>
            <?php
                }
            } else {
                echo '<p>No courses available.</p>';
            }
            wp_reset_postdata();
            ?>
        </div>
    </div>
</div>

<?php get_footer(); ?>

I’ve tried rewriting the query multiple times and even tested with different post types, but the same thing happens. The posts only show when I’m logged in as admin. Any ideas what could cause this behavior?

check your custom post type registration - the ‘capability_type’ is probably set to something that needs login. if you’re using ‘edit_posts’ or similar capabilities, anonymous users can’t see them. set capability_type to ‘post’ in your register_post_type function.

It seems like you’re encountering a common issue with custom post types in WordPress. Make sure that your custom post type registration includes both ‘public’ set to true and ‘publicly_queryable’ also set to true. These parameters control visibility for logged-out users. You might also want to consider any plugins that could be affecting permissions or visibility based on user roles. Deactivating them one by one can help identify if a plugin is causing the issue. Lastly, review your theme’s functions.php file for any custom queries or filters that might inadvertently alter visibility for anonymous visitors.

This topic was automatically closed 4 days after the last reply. New replies are no longer allowed.