3

I'd like to know if I can remove the limit on the posts_per_page for a specific post type.

In the archive.php page I'm displaying different post type, and for the specific "publications" post type I want to display all the posts. How can I achieve this without impacting the traditional "post" type?

3 Answers 3

5

You can hook into the pre_get_posts action, to access the $query object.

You should use your action hook inside your functions.php. An example of what your function could look like:

add_action( 'pre_get_posts', 'publications_archive_query' );
function publications_archive_query( $query ) {
  if ( !is_admin() && $query->is_main_query()) {
    if ( $query->get('post_type') === 'publications' ) {
      $query->set( 'posts_per_page', 5 );
    }
}

Narrow down what is the query you are modifying by using conditional checks.

$query->get('post_type') to get current's Query post_type to check against.

is_main_query to make sure you are only applying your query modification to the main query.

Read further and find more examples: https://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts

4
  • 1
    I’m afraid it won’t work. You use get_post_type to check post type. But this function checks post type of global $post variable. And during pre_get_post that variable is not set yet ;) Oct 1, 2018 at 6:26
  • 1
    @KrzysiekDróżdż - yes you are right. Thanks for the note. I am editing the answer and also fixed a syntax error in my first if ().
    – FFrewin
    Oct 1, 2018 at 6:52
  • 1
    Still not correct. 1. You still write about get_post_type. 2. post_type doesn't have to be set, so your can cause notices and warnings. Oct 1, 2018 at 7:19
  • @KrzysiekDróżdż: yes - I only updated my code ... forgot to correct the rest of my answer. Thanks! Fixed now (hope I didn't miss anything else this time)
    – FFrewin
    Oct 1, 2018 at 7:25
0

Accepted answer isn't working, but this one does:

function bloom_custom_posts_per_page( $query ) {
  if( !is_admin() && $query->query_vars['post_type'] == 'post_type') {
     $query->query_vars['posts_per_page'] = -1;
     return $query;
  }
}
add_filter( 'pre_get_posts', 'bloom_custom_posts_per_page' );
-3

If you are creating the query yourself inside those .php pages, then it's better to pass -1 in argument to obtain unlimited posts:

$args =  array( ...... 'posts_per_page'=>-1, .....) 
$your_query = new WP_Query($args);

If you dont have access to the query-creating .php file, then you might neeed to use less-recommended pre_get_post version (which affects all pages/queries with that custom-type), like @FFrewin suggested.

8
  • Well, modifying global wp_query using pre_get_posts is not less-recommended. You always should use global wp_query and modify it with filters, if it's only possible and not create your own queries. Oct 1, 2018 at 7:23
  • @KrzysiekDróżdż I neither way agree with you and it's really "less-recommended" to modify the global query, and what's more no indication which pages you are affecting (and that affects any existing page with cpt publications). Instead, it's better to make a custom query (instead of altering whole site) within the targeted .php. However, you have right to downvote my question (with incorrect reason), and ok, it's your right.
    – T.Todua
    Oct 1, 2018 at 7:28
  • 2
    Creating a new WP_Query in this context leads to querying the database twice. As you have a second query over the main one.
    – FFrewin
    Oct 1, 2018 at 7:29
  • @FFrewin to say, actually i dont see any problem with that. You are not re-creating new queries on all pages, instead doing on sepcific pages, and even i dont think you have million of hits in hour on that page, to worry about DB overload (which is really good habbit to worry about overload and always try to have optimized code). But, in this case i dont see real problem. ok, i just suggested my answer, you decide.
    – T.Todua
    Oct 1, 2018 at 7:32
  • 1
    @T.Todua of course I don’t see any difference. It makes no sense at all to create custom query just to change number of posts showing on archive page - it’s exactly perfect case for using pre_get_posts action. If you’ll use your custom query, then you’ll break the pagination for example - WP won’t return 404 for page/2 and so on, because it would thing that there are more posts, and so on... Sorry, I really and strongly recommend not to use custom query in this case - it makes no sense and it can easily cause a lot of problems. Oct 1, 2018 at 10:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.