Search code examples
phpwordpressloopscustom-post-type

Custom Post Type Loop Doesn't Load All Posts


I'm trying to load my custom posts using a php loop and it only loads 5 posts. I'm using CPT UI Wordpress plugin code to register a post:

function cptui_register_my_cpts_homepage_brands() {

    /**
     * Post Type: Homepage Brands.
     */

    $labels = array(
        "name" => __( "Homepage Brands", "astra" ),
        "singular_name" => __( "Homepage Brands", "astra" ),
    );

    $args = array(
        "label" => __( "Homepage Brands", "astra" ),
        "labels" => $labels,
        "description" => "",
        "public" => true,
        "publicly_queryable" => true,
        "show_ui" => true,
        "delete_with_user" => false,
        "show_in_rest" => true,
        "rest_base" => "",
        "rest_controller_class" => "WP_REST_Posts_Controller",
        "has_archive" => false,
        "show_in_menu" => true,
        "show_in_nav_menus" => true,
        "exclude_from_search" => false,
        "capability_type" => "post",
        "map_meta_cap" => true,
        "hierarchical" => false,
        "rewrite" => array( "slug" => "homepage_brands", "with_front" => true ),
        "query_var" => true,
        "supports" => array( "title", "editor", "thumbnail" ),
    );

    register_post_type( "homepage_brands", $args );
}

add_action( 'init', 'cptui_register_my_cpts_homepage_brands' );

and I use this code to create a loop and to load images from posts a loop:

<div class="your-class">
<?php
$args = array(
                'post_type' => 'homepage_brands',
                'posts_per_page' => -1,
                'orderby'   => 'menu_order',
                'order'     => 'ASC',
);

$brand = get_posts($args);
foreach ($brand as $post) {
                setup_postdata($post);
                $thumbnail = get_the_post_thumbnail_url($post->ID, 'full');
                if (!$thumbnail)
                                continue;
?>

<div><img src="<?php echo $thumbnail; ?>"></div>

                <?php
}

wp_reset_postdata();
?>
</div>

And yes, I checked Wordpress posts setting, and amount of posts is set to hundreds. Also, weirdly, I use the same exact way to loop another custom post and it works.

I think something is off with the way I register that post type, but I'm not sure. I just tried the same way/code on a different server and it all works perfectly.


Solution

  • The issue was caused by a faulty "Post Migrate" wordpress plugin. Removing it and creating a new post type fixed the issue.