Skip Navigation

[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.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 10 replies, has 2 voices.

Last updated by Veres Horia Serban 6 years, 9 months ago.

Assisted by: Minesh.

Author
Posts
#548633

Hi, I need help with changing the amount of posts shown in an archive using toolset:

1. has to be done in PHP
2. has to apply to archive rather than views

How, please?
Thanks!

#548647

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Well - we have detailed Doc about how you can implement pagination for your WordPress Archive Pages using Toolset:
=> https://toolset.com/documentation/user-guides/custom-pagination-for-wordpress-archives/

I hope above Doc will help you to resolve your issue.

#548653

Hi Minesh,

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.

thanks!

#548656

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Ok - but I don't know why you want PHP solution as you said you want to do using Toolset.

Nevermind. As I do not know your where and how you want to implement things.

You need to use WordPress pre_get_posts hook to override the posts_per_page.

For example:

add_action('pre_get_posts', 'change_posts_per_page_archive');
  function change_posts_per_page_archive($query) {
 if(!is_admin() && $query->is_archive()){
        $query->set('posts_per_page', 15);
    }
    return $query;
}

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.

#548663

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

#548667

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

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.

#548725

Hi Minesh,

For more than one hour now the site layout is broken. Can you please let me know what your findings are? I'd like to resume working on it.

Thanks!

#548737

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Ok - the first thing is that you added "pre_get_posts" to your functions.php file as filter BUT you should add "pre_get_posts" as action .

See WP Doc:
=> https://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts

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.

#548747

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.

Please undo everything you did.

Thank you very much for your help.

#548752

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Doh - what a typo mistake I did for filter and action.

I undo all changes I made to functions.php

Please note that - you should change all "pre_get_posts" to add_action.

#550020

Brilliant, thank you!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.