Home › Forums › Support › Increasing maximum number of posts shown per page in archive with pagination
- This topic has 17 replies, 4 voices, and was last updated 4 years, 1 month ago by
Elvin.
-
AuthorPosts
-
February 3, 2022 at 3:40 pm #2104484
Graeme
I’m using a plugin called Toolset to create custom post types and archives to display the posts in various taxonomies.
The trouble being I want to display as many posts on a map on the archive page as possible, but it will only show posts on the map which are loaded on the page; and I have hundreds of these posts to show.
It makes the page load speed exceedingly slow, too slow to be feasible.
Right now I’m shown that I can have pagination up to 39 posts per archive page.
Is there any way to increase this to a custom number, for example 100 or 200 per page?
I found the following two support information pages, but for someone like myself with no developing experience, I haven’t been able to extract how I should functionally achieve this..
https://docs.generatepress.com/article/generate_pagination_mid_size/
https://docs.generatepress.com/article/using-filters/If this is as simple as copy pasting some code into the relevant theme file, could someone please help direct me?
Thank you!
February 3, 2022 at 7:47 pm #2104589Elvin
StaffCustomer SupportHi there,
It makes the page load speed exceedingly slow, too slow to be feasible.
The most glaring issue w/ this is the sheer number of images to be loaded by the page for every post on the list.
If you wish to load 100/200 pages, you may want to consider removing the number of things to be loaded in 1 post item so the page doesn’t get way too heavy.
Alternatively, consider using Infinite scroll. This way, the archive page doesn’t necessary have to load all 200 posts at once on page load.
https://docs.generatepress.com/article/blog-content-layout/#:~:text=of%20the%20post.-,Use%20infinite%20scroll,-%E2%80%93%20Activate%20or%20disableWith infinite scroll, the page loads the only the first 10 and then loads the next 10 when the user is close to scrolling to the bottom. W/ this, the page won’t be too heavy.
February 4, 2022 at 4:45 pm #2105780Graeme
Hi Elvin,
the images was the first thing to occur to me, so I removed these from the posts.
Now it only calls minimal information for each post, title, phone number, address, website URL.
The problem with infinite scroll is that the display Map that I insert using the ToolSet plugin, only shows the locations loaded in the archive page; therefore, it will show very few locations if only a few load at first.
Of course I can turn on pagination, and set to the maximum 39 posts to load per page, and its VERY fast, but then it only displays 39 locations on the map.
The problem is that, this is a listings directory, and I want to display all of the locations on the map when a user arrives, so they can see they have many options.
I may need to look in to hiring a private developer to get what I want.
But what I’m asking you, is if I can load more than 39 posts per page when using pagination.
Can I hardcode into the theme somewhere, to load for example 100 posts per page when using pagination?
Thanks
February 5, 2022 at 6:33 pm #2106730Tom
Lead DeveloperLead DeveloperHi there,
It’s possible using a filter, but you’ll need to add in the necessary conditions.
For example:
add_action( 'pre_get_posts', function( $query ) { if ( ! is_admin() && $query->is_main_query() ) { if ( 'your_post_type' === $query->get('post_type') ) { $query->set( 'posts_per_page', 200 ); } } } );February 7, 2022 at 1:52 pm #2108789Graeme
Thanks Tom, this looks promising.
Despite having taken a programming class or two in the past, I don’t have any knowledge of web development.
This snippet of code seems like it may achieve the result I’m looking for, but I’ve no idea where I should insert this code and maybe if I need to change any of the parameters?
Also this is pertaining to an archive page, so I’ve noticed that maybe there is a difference between “posts_per_page” and “posts_per_archive_page”?
Could you clarify and maybe point me towards where and how I may be able to add this code?
Thank you!
February 7, 2022 at 4:23 pm #2108872Ying
StaffCustomer SupportHi Graeme,
You can add PHP using any of the 3 methods introduced in the below article:
https://docs.generatepress.com/article/adding-php/February 9, 2022 at 1:59 pm #2111340Graeme
Thanks.
I used the Code Snippets plugin to add in the PHP.
This didn’t seem to have any effect though; I’m not sure what I’m doing wrong.
This is for an archive page for a specific taxonomy of a custom post type I created using the Toolset plugin.
Toolset support provided me this PHP code to choose a custom number of posts to show on the archive page,
add_action(‘pre_get_posts’, function($query){
if ( ! is_admin() && $query->is_main_query() && is_post_type_archive( ‘test-cpt-1’ ) ) { // replace ‘test-cpt-1’ with your custom post type slug
$query->set( ‘posts_per_page’, 123 ); // here replace 123 with the number of posts you want to show
}
},99);Using the code above and the URL slug of the custom post type itself where “test-cpt-1” is didn’t work, in my case this URL slug was “location”.
So next I tried the URL slug of the archive page for the specific taxonomy that I’m trying to get this working for and it also didn’t work.
Should I be putting in more than just the URL slug in that place, like the whole website domain etc.?
Or is there something else that I’m really missing?
Thanks!
February 9, 2022 at 3:31 pm #2111438Ying
StaffCustomer SupportTry this:
add_action( 'pre_get_posts', function($query){ if ( ! is_admin() && $query->is_main_query() && is_post_type_archive( 'test-cpt-1' ) || is_tax() ) { // replace ‘test-cpt-1’ with your custom post type slug $query->set( 'posts_per_page', 2 ); // here replace 123 with the number of posts you want to show } },99);Should I be putting in more than just the URL slug in that place, like the whole website domain etc.?
No, you only need to re-place
test-cpt-1with the CPT slug.February 9, 2022 at 7:37 pm #2111660Graeme
Hey thank you!
I was fiddling around with this and actually found a way that makes it work…
add_action(‘pre_get_posts’, function($query){
if ( ! is_admin() && $query->is_main_query() && is_archive( ‘test-cpt-1’ ) ) { // replace ‘test-cpt-1’ with your custom post type slug
$query->set( ‘posts_per_page’, 149 ); // here replace 123 with the number you want
}
},99);The only change I made to the previous code was to put in is_archive instead of is_post_type_archive.
It seems specifying the archive in particular worked.
I’m going to have a total of 3 different archive pages on my website, I think I can probably just create a code snippet like this one for each of the different archives?
I think this works, haven’t seen any issues; from your perspective would there be anything wrong with doing it this way?
Thank you again so much!
GFebruary 9, 2022 at 7:59 pm #2111675Elvin
StaffCustomer SupportHi G,
I’m going to have a total of 3 different archive pages on my website, I think I can probably just create a code snippet like this one for each of the different archives?
I think this works, haven’t seen any issues; from your perspective would there be anything wrong with doing it this way?
There’s no need to do multiple copies of this for multiple archive pages. You only need to change the condition.
While is_archive works, I think it’s not the most appropriate because
is_archive()actually doesn’t take any arguments so the ‘test-cpt-1’ value you passed on it is actually ignored.is_archive()just checks if the page is an archive page.Can you specify what kind of archive page these 3 archive pages are? So we can think of the appropriate condition for it.
Here are the most common ones used to check –
is_category() – https://developer.wordpress.org/reference/functions/is_category/
is_tax() – https://developer.wordpress.org/reference/functions/is_tax/
is_post_type_archive() – https://developer.wordpress.org/reference/functions/is_post_type_archive/But sometimes, the appropriate approach would be to actually get the specific taxonomy and/or term value of the archive and have it checked against an array of specified values.
February 9, 2022 at 9:13 pm #2111740Graeme
Brilliant.
It’s no wonder is_archive was making my other archives, not the ones specified, also show that number of posts haha.
Thank you for the suggested conditions!
is_tax() turned out to be the right choice, using the taxonomy and term specified I got this to work on just the one archive page.
is_tax( $taxonomy = ‘taxonomy1’, $term = ‘term1’ )
I have one more pressing question, and with your expertise I wonder if you may be able to point me the right direction, as everything I’ve tried so far has not worked.
This archive page is built with Toolset, and it has a filter/search at the top of the page which I designed.
I want to make the drop down selection for State default to a single state, this will intuitively get the user to pick their own state, and will keep page load times down because it isn’t trying to load hundreds of locations (custom post type) at once on the page and into the map.
You can see the webpage in question in the private information tab below.
I think I should be able to use most of that same code snippet to set both the number of posts per page, and then I’m hoping, the selected State to default load for the page.
I found that I could get it to set to New York for example with this code,
$query->set( ‘state’, “new-york” )
But the problem was then New York was the only option in the drop down selection for State, and of course I need the rest of the taxonomy/term archive there to filter and search for the user.
Thanks again wow!!
GFebruary 9, 2022 at 9:42 pm #2111762Elvin
StaffCustomer SupportFor this one, I suggest contacting the plugin developer of the search filter as the filter’s functionality itself is beyond the theme’s control and scope, unfortunately.
I’m not exactly sure but I’d like to believe they have a filter for this to set the default value. This has probably been asked before and perhaps they already have a ready-made solution for it. 😀
February 9, 2022 at 10:52 pm #2111823Graeme
I haven’t got far requesting support on this.
Of course it seems like others would have asked before me, but it hasn’t been the case so far lol
Thanks
February 9, 2022 at 11:12 pm #2111870Elvin
StaffCustomer SupportIt’s pretty tricky to do because there are 2 ends to this. The front end and the actual request.
If this search filter was made to be conducive for this kind of function, a filter hook to assign the default value should be built into its code. (if it’s from a shortcode, perhaps it has a shortcode attribute for setting a default)
February 9, 2022 at 11:23 pm #2111886Graeme
When I initially requested support on this from Toolset they responded with this,
If for some reason, you’d still prefer to filter by default state (when the page initially loads), you’ll need to use the WordPress hook “pre_get_posts”, to customize the archive’s query:
https://developer.wordpress.org/reference/hooks/pre_get_posts/Which seems to be like the solution I worked out, only problem that it only shows the one state and eliminates all of the other states from the drop down menu.
I would think that there should be some kind of filter hook for the search function… I’ve now asked as much. Unfortunately it takes much longer to get a reply from Toolset than you! 😛
Hopefully they have some kind of solution..
Thanks
G -
AuthorPosts
- You must be logged in to reply to this topic.