I am using conditional statements inside the loop in Views to do some fine selection of what items to display.
I am using a pagination with manual reload.
I would like to have the same number of items per page in the resulting list.
Due to the conditional statement, the number of items per page changes from page to page. Is there a way to correct the internal counter used for the pagination?
Hello. Thank you for contacting the Toolset support.
What conditional statement you are using? Can you please share bit more details and problem URL and admin access details and let me review your settings and then I will be able to guide you in the right direction.
*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin) 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 have set the next reply to private which means only you and I have access to it.
I set pagination for 10 items per page. If the conditional statement stops from displaying three items of that page, then the rendered page has only 7 items. I would like to see 10 items per page in all cases, until there are no items anymore.
Is there a way to control the counter used in the pagination?
The function does its job currectly, I have thoroughly tested it, and here is a simplified version to illustrate the logics:
function my_user_has_access_to_post(){
$post_id = get_the_ID();
if ( ... various tests using custom post and user fields... ) return 'true';
else return 'false';
}
Of course, I have added the function in the Settings under "Functions inside conditional evaluations".
The function filters correctly the items I don't want to display in the list.
My problem is with the pagination. My question is about controlling the counter used in the pagination in Views.
I understand what you mean but I need to know the types access group name.
Is it possible for you to share admin access details and problem URL where you added your view so that I can able to know the types access group name and then try to filter with access group.
*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin) 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 have set the next reply to private which means only you and I have access to it.
I don't see our exchange on this page anymore, not sure why.
In your last reply that I received by email, you asked for the type access group name. I am not using access groups here. I am using a custom function in a View loop and I am asking if Toolset provides a way to access the pagination counter used in Views to correct it consistently with the displayed output.
Thanks for any help anyway.
In your last reply that I received by email but which I don't see here, you asked for the type access group name. I am not using access groups here. I am using a custom function in a View loop and I am asking if Toolset provides a way to access the pagination counter used in Views to correct it consistently with the displayed output.
Thanks for any help anyway.
I need to know how you are removing and what code exactly you are using to hide the post using the conditional function you use my_user_has_access_to_post.
If you can share admin access details it will help me to review whats exactly going on and what could be the possible solution.
*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin) 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 have set the next reply to private which means only you and I have access to it.
You ask: "I need to know how you are removing and what code exactly you are using to hide the post using the conditional function you use my_user_has_access_to_post."
In a nutshell, the function my_user_has_access_to_post() return 'false' when the post should not be displayed. The conditional statement in the loop "[wpv-conditional if="my_user_has_access_to_post() = 'true'"][wpv-post-body view_template="Search Results - loop item"][/wpv-conditional]" prevents this post from being displayed in Views.
This information is already documented in my comments above.
add_filter( 'wpv_filter_query_post_process', 'func_exclude_unwanted_posts', 10, 3 );
function func_exclude_unwanted_posts( $query, $view_settings, $view_id ) {
if ($view_id==99999 and !empty( $query->posts ) ) {
$final_result = array();
foreach($query->posts as $k=>$v):
$return_value = my_user_has_access_to_post($v->ID);
if($return_value){
$final_result[]=$v;
}
endforeach;
$query->posts = $final_result;
$query->found_posts = count($final_result); // modify the count of found posts
$query->post_count = count($final_result); // modify the count of displayed posts
}
return $query;
}
Where:
- Replace 99999 with your original view ID
You can adjust the code of your function as:
function my_user_has_access_to_post($passed_post_id=''){
if($passed_post_id!=''){
$post_id = $passed_post_id;
}else{
$post_id = get_the_ID();
}
if ( ... various tests using custom post and user fields... ) return 'true';
else return 'false';
}
By doing this way - you can also remove the conditional shortcode you added to your view. So, you can replace the following code:
Thanks. It does not seem to work. When I print out using error_log the $view_id from inside the function func_exclude_unwanted_posts, I don't get the ID of the View I am running.
In my case, I want to apply this to a View that is in a Layout which is associated to Search results. I guess that makes it a WP Archive. Maybe that's why this codes does not work?
Would you be able to open a sandbox to test this in a clean environment?
I can offer you test site but it will be clean installation.
I need a test site where you setup your actual reuqirement in order to test this. Can you please share problem URL and admin access details of the test site where I can see the issue.
I have set the next reply to private which means only you and I have access to it.
The topic ‘[Closed] Pagination with conditional statements in Views’ is closed to new replies.