[Resolved] Set the amount of posts per page in php
This support ticket is created 6 years, 9 months ago. There's a good chance that you are reading advice that it now obsolete.
This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.
Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.
No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.
Don't want to be rude, but why do you just use a canned answer instead of really helping? Takes more of your time and of mine.
I specifically asked in both the subject and body of the support ticket that I need a PHP solution. As a courtesy for your time I did not provide details for you to read.
So please help and provide the hook/filter for this.
I hope above filter helps you to change posts per page for your archive. Feel free to get in touch with me with your queries if any. I will be happy to help you further.
Thank you. I apologise for not providing the entire context but our site has a very complex structure and it would have taken me too much time to write and you too much time to read in order to understand. The pre_get_posts filter was my initial try, before submitting the ticket.
There are a few problems:
On the page I am including other views. Setting the posts_per_page using the pre_get_posts filter changes how many results those views return. However it does not change how many results the archive returns, it's still the amount set in the toolset backend.
This is what I'm actually looking for
add_filter('whatever-filter','changePerPageItems');
function changePerPageItems($whatever_params){
if (CONDITION) //see below
$query->set('posts_per_page', 15);
}
CONDITION could be any of the following
- vew_id = whatever // the ID of the toolset archive view - this works for toolset views but not archive views
- is_main_query() // can't seem to be able to use this
- is_archive_query // not really aware if something like this exists
Well - the archive you are displaying is post type archive or taxonomy archive?
Could you please share problem URL where you want to change posts per page and temporary access details so that I can look into your setup.
*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.
I would additionally need your permission to de- and re-activate Plugins and the Theme, and to change configurations on the site. This is also a reason the backup is really important. If you agree to this, please use the form fields I have enabled below to provide temporary access details (wp-admin and FTP).
I have set the next reply to private which means only you and I have access to it.
Now, you can try one fo the following way - Using "pre_get_posts"
add_action('pre_get_posts', 'change_posts_per_page_archive_size',99, 3 );
function change_posts_per_page_archive_size($query) {
if ( is_post_type_archive( 'news' ) ) {
// Display 50 posts for a custom post type called 'movie'
$query->set( 'posts_per_page', 5 );
return;
}
}
I already added above code to your theme's functions.php file.
Thanks! Please note that you have provided the add_filter code initially, I just copied it 🙂
It is working, but now the layout is messed up.
What else did you do? Can you please undo? I have already commented out that piece of code in functions.php but the layout is still messed (it is full screen now for some reason). and for some reason it says "Latest Posts" in the beginning.