• Resolved MCM

    (@nathmie)


    I would to display all posts in an archive for a specific custom post type.

    I have tried the following but not working.

    function all_posts_custom_posts( $query ) {
    
    	$post_type = get_post_type();
    
    	if ( 'restaurants' == $post_type )
    	{
            $query->query_vars['posts_per_page'] = -1;
            return;
        }
    }
    add_action('pre_get_posts', 'all_posts_custom_posts', 1);
Viewing 4 replies - 1 through 4 (of 4 total)
  • hi

    try this instead:

    function all_posts_custom_posts( $query ) {
    
            $post_type =  $query->query_vars['post_type'];
    
            if ( 'news' == $post_type ){
                    $query->query_vars['posts_per_page'] = -1;
                    return;
            }
    }
    add_action('pre_get_posts', 'all_posts_custom_posts',1);
    Thread Starter MCM

    (@nathmie)

    Thanks, not working. I can say it at least shows all posts if I put in posts where you have news but then it does it for normal posts and custom post types.

    So, I guess the problem stems around the post_type checking.

    ahh, I forgot to change my own cpt ‘news’ to your cpt ‘restaurants’ 😉

    function all_posts_custom_posts( $query ) {
    
            $post_type =  $query->query_vars['post_type'];
    
            if ( 'restaurants' == $post_type ){
                    $query->query_vars['posts_per_page'] = -1;
                    return;
            }
    }
    add_action('pre_get_posts', 'all_posts_custom_posts',1);
    Thread Starter MCM

    (@nathmie)

    Thanks, I managed to get everything working and forgot to resolve this post apologies.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Display ALL posts for a specific custom post type’ is closed to new replies.