WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Display ALL posts for a specific custom post type (5 posts)

  1. MCM
    Member
    Posted 1 year ago #

    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);
  2. birgire
    Member
    Posted 1 year ago #

    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);
  3. MCM
    Member
    Posted 1 year ago #

    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.

  4. birgire
    Member
    Posted 1 year ago #

    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);
  5. MCM
    Member
    Posted 1 year ago #

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

Topic Closed

This topic has been closed to new replies.

About this Topic