[Special Summer Sale] 40% OFF All Magento 2 Themes

Cart

Get All posts in wordpress

  • This topic is empty.
Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #9971
    user9426983
    Participant

    Below is the code I am using to get all posts in WordPress, but somehow I am not able to get more than 9 post. I have total of 30 posts. Any suggestions what I am doing wrong.

    $post_ids = new WP_Query(array('post_type' => 'sample','fields' => 'ids'));
    
       if ($post_ids->have_posts()):
         foreach( $post_ids->posts as $id ):
           $post_titles[] = apply_filters('the_title', get_the_title($id));
         endforeach;
      endif;
    
    #9972
    rhysclay
    Participant

    You need to modify the posts per page argument of the WP_Query object like so:

    $post_ids = new WP_Query(array(
       'post_type' => 'sample',
       'fields' => 'ids',
       'posts_per_page' => -1
    ));
    

    By setting posts_per_page to -1 your loop will return all posts. If this argument is not supplied the number of posts will default to the posts per page setting in Reading -> Settings.

Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.