Skip Navigation

[Resolved] Option to change number of posts per page when using archive

This thread is resolved. Here is a description of the problem and solution.

Problem:
Option to change number of posts per page when using archive

Solution:
The 'wpv_filter_query' hook will work for views not for archives. If you want to filter archive query arguments on fly either you will have to use pre_get_posts hook or the 'wpv_action_apply_archive_query_settings' hook.'

You can find proposed solution in this case with the following reply:
=> https://toolset.com/forums/topic/option-to-change-number-of-posts-per-page/#post-2262469

Relevant Documentation:

This support ticket is created 3 years, 7 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.

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 2 replies, has 2 voices.

Last updated by roxanneS 3 years, 7 months ago.

Assisted by: Minesh.

Author
Posts
#2262215

Tell us what you are trying to do?
I used the following solution to allow the user to select the number of posts per page to display: https://toolset.com/forums/topic/option-to-change-the-number-of-results-per-page/

It says to replace "1234" with the actual ID of the view. This works fine on views that I created, but not on the Posts archive page.
In Toolset > WordPress Archives, the ID is 111, but when I hover over the link I see an ID of 112. I tried both IDs in the function but still it does not work.

I then tried modifying the function to this but it still doesn't work (with or without the is_archive() :
// Allow the Archive Custom Search to choose how many posts to display
add_filter( 'wpv_filter_query', 'filter_items_per_page_custom_fn', 1000 , 3 );
function filter_items_per_page_custom_fn( $query_args ) {

if ( !is_admin() && is_archive() )
{
if(empty($_GET['item-per-page'])) {
$query_args['posts_per_page'] = 6;
} else {
$query_args['posts_per_page'] = $_GET['item-per-page'];
}

}

return $query_args;
}

Is there any documentation that you are following?
https://toolset.com/forums/topic/option-to-change-the-number-of-results-per-page/
Is there a similar example that we can see?

What is the link to your site?
hidden link

#2262469

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

The 'wpv_filter_query' hook will work for views not for archives. If you want to filter archive query arguments on fly either you will have to use pre_get_posts hook or the 'wpv_action_apply_archive_query_settings' hook.

Can you please try to use the following code and check if that help you to resolve your issue:

function modify_query($query, $archive_settings, $archive_id){
    if($archive_id==111) {   //change this to the view ID of your archive page

if(empty($_GET['item-per-page'])) {
 $query->set('posts_per_page', 6);
} else {
$query->set('posts_per_page',  $_GET['item-per-page']); 
}
    
    }
}
add_action('wpv_action_apply_archive_query_settings','modify_query',31,3);
#2262699

Thank you. It works perfectly.