Skip Navigation

[Resolved] Pre-filter post view when the view is being filtered by parent taxonomy view

This support ticket is created 4 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.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9: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/Hong_Kong (GMT+08:00)

This topic contains 8 replies, has 2 voices.

Last updated by Luo Yang 4 years, 9 months ago.

Assisted by: Luo Yang.

Author
Posts
#1492825

Hello, I am having some trouble with a second layer of filtering. I can't figure it out.

First, I have a custom post type (staff portal pages) with 2 custom taxonomies (staff portal categories and staff portal access types).

I have custom user meta that assigns a user 1 or many staff types (all staff, admin, team leads, dvm, and support staff).

I will have the same 5 staff types mirrored in the staff portal access types taxonomy.

When a user of a specific staff type logs in, their dashboard will have the view that feeds posts that are matched to their access type.

So, if a user with all staff, and team leads logs in, the posts that have those access types selected in the custom taxonomy will be visible.

My code works if I am only feeding the posts via a single view, but when I try to first filter by the other taxonomy, my code breaks.

add_filter( 'wpv_filter_query', 'func_prefilter_food_drink_taxonomy', 99, 3 );
function func_prefilter_food_drink_taxonomy( $query_args, $view_settings, $view_id ) {

  $current_user = wp_get_current_user();
  $current_user_login = $current_user->user_login;
  $current_user_id = $current_user->ID;
  $current_user_meta = get_user_meta($current_user_id, 'wpcf-portal-staff-type', true);
  $staff_type_array = array($current_user_meta['wpcf-fields-checkboxes-option-1bc875802f7cd80318e2856c04d94805-1']['0'],$current_user_meta['wpcf-fields-checkboxes-option-758fcce84fcd4ce43714086c8197e782-1']['0'],$current_user_meta['wpcf-fields-checkboxes-option-665cd1c9a2322706e21ef5eeb53fc9b9-1']['0'],$current_user_meta['wpcf-fields-checkboxes-option-343b76173c6607890afa154a6f35b4b7-1']['0'],$current_user_meta['wpcf-fields-checkboxes-option-b6e731fe3a431f47c6cb723d4bfe24b3-1']['0']);
  
  $display_view_ids = array(3217);

  if (in_array($view_id,$display_view_ids) and empty($query_args['tax_query']) ){

    $query_args['tax_query'] = array(array(
      'taxonomy'=> 'staff-portal-access-type',
      'field' => 'slug',
      'terms' => $staff_type_array,
      'operator' => 'IN',
      'include_children' => 1
    ));

  }
  return $query_args;
}

You will see that I created an array of staff types that are being pulled by the current user. This feeds the tax_query only the terms that the current user has assiciated to them.

This works perfectly.

However, I want the posts to be broken up by the other category, so each posts of that category is under a nice title.

I tried filtering the posts by the taxonomy filter, and then putting the view id of the taxonomy filter into my code, but that doesn't work.

I hope I explained myself well.

Please let me know what I am doing wrong.

Thanks!

#1493321

Hello,

I assume we are talking about this case:
You are going to display the post view's result as below:
- portal category A
-- Post 1
-- Post 2
- portal category B
-- Post 3
-- Post 4
...

If it is, you can try with nested view, for example:
1) Parent taxonomy view
- Query terms of taxonomy "staff portal categories"
- In view's loop, display term's title/link + below child post view
2) Child post view
- Query your custom posts
- Filter by:
Select posts with taxonomy:
staff portal categories set by the parent Taxonomy View
+ the custom filter of your PHP codes
- In view's loop, display the post information

More help:
Views Inside Views – Display Nested Multidimensional Lists
https://toolset.com/documentation/user-guides/views/using-a-child-view-in-a-taxonomy-view-layout/

#1494261

Luo,

Thank you for your reply. This is exactly what I did, but the posts are not filtering with my PHP when I nest the views. If I simply remove the "set by taxonomy view" from the child view and just display the post loop on a page, my PHP filter works. As soon as I nest the post view with the "set by taxonomy view" filter, and display the taxonomy filter in the front-end, my PHP filter stops working.

I tried putting both the child and parent view IDs in this line of code:

$display_view_ids = array(ID HERE);

I don't know why my PHP filter breaks when I nest.

#1494527
mvs-views.JPG

Luo,

I managed to get it working more than it was before. However, it still isn't 100% correct.

Let me tell you what I did.

First, there was a conditional in the original code:

 and empty($query_args['tax_query'])

Because the post view was nested inside a taxonomy view, this statement was always returning that there was a tax query. I remove this and the PHP filter is working somewhat.

Here is my full function as it is right now:


add_filter( 'wpv_filter_query', 'func_prefilter_staff_type_taxonomy', 100, 3 );
function func_prefilter_staff_type_taxonomy( $query_args, $view_settings, $view_id ) {

  $current_user = wp_get_current_user();
  $current_user_login = $current_user->user_login;
  $current_user_id = $current_user->ID;
  $current_user_meta = get_user_meta($current_user_id, 'wpcf-portal-staff-type', true);
  $staff_type_array = array($current_user_meta['wpcf-fields-checkboxes-option-1bc875802f7cd80318e2856c04d94805-1']['0'],$current_user_meta['wpcf-fields-checkboxes-option-758fcce84fcd4ce43714086c8197e782-1']['0'],$current_user_meta['wpcf-fields-checkboxes-option-665cd1c9a2322706e21ef5eeb53fc9b9-1']['0'],$current_user_meta['wpcf-fields-checkboxes-option-343b76173c6607890afa154a6f35b4b7-1']['0'],$current_user_meta['wpcf-fields-checkboxes-option-b6e731fe3a431f47c6cb723d4bfe24b3-1']['0']);
  
  $display_view_ids = array(3217);

  if (in_array($view_id,$display_view_ids) ){

    $query_args['tax_query'] = array(array(
      'taxonomy'=> 'staff-portal-access-type',
      'field' => 'slug',
      'terms' => $staff_type_array,
      'operator' => 'IN',
    ));

  } 
  return $query_args;
}

The display view is 3217, which is my child post view.

The issue now is that the parent taxonomy view is not filtering the posts into the correct category.

I feel like there is a conflict between the PHP filter (which is supposed to filter the staff types taxonomy), and the parent filter (which is supposed to filter with the portal page category).

Please advise.

#1495613

The taxonomy view won't be able to filter posts, it can be filtered by terms.

For the parent taxonomy view, you can try the filter hook wpv_filter_taxonomy_query to apply your custom PHP codes, see our document:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_taxonomy_query

#1495641

Luo,

I'm sorry, but to be clear, are you suggesting that I also have to filter the taxonomy view with another PHP filter? By your original message, it seemed that you were saying I would be able to create the parent taxonomy view, filter my post view by the parent taxonomy view, and then just apply my additional PHP filter to the post view.

I have proven that the PHP filter functions properly when only applied to the post view as a stand-alone view.

Once I nest the post view inside of the taxonomy view, my PHP filter no longer works properly.

To clarify, it still works, but the results are duplicated.

Example:

If I have 2 posts and 2 categories, the normal result would be:

Category 1
-- Post 1
Category 2
-- Post 2

My PHP filter takes a second taxonomy that matches my user meta exactly to the categories of the second taxonomy. This way, I can apply a "user access level" to the posts, and only show posts that the current user is allowed to see.

Example:

CURRENT USER = Access level "team lead"
Post 1 has a category from the parent category of "Insurance"
Post 2 has a category from the parent category of "Employment Docs"
Post 1 has a category from the second taxonomy of "Team Lead"
Post 2 has a category from the second taxonomy of "Support Staff"

The current user should see:

Insurance
-- Post about insurance
Employment Docs
-- No Result

Instead we see:

Insurance
-- Post about insurance
Employment Docs
-- Same post about insurance

For some reason, even though the employment doc post is removed by my PHP filter successfully, the Insurance post that the current user has access to is duplicated and put into the Employment Docs category, as well.

I don't know if I can be much clearer that this. It seems that there is a conflict where the parent taxonomy view loops my PHP filter multiple times, and because of this, the post that my filter says should be returned gets returned by as many times as the taxonomy loop runs

Can you explain this or duplicate this issue on your end?

Please advise.

#1495681

My above suggestion is for your screenshot:
https://toolset.com/wp-content/uploads/2020/02/1494527-mvs_views.jpg
Display the results without term "Employment Docs" or it's child post view, for example:
a)
Insurance
-- Post about insurance

And it needs only to apply custom PHP code filter to parent taxonomy view, it does not need custom PHP code filter to child post view.

If you want to display the result as below:
b)
Insurance
-- Post about insurance
Employment Docs
-- No Result

Then you will need to modify your PHP codes, to compare the existed taxonomy "staff-portal-access-type" filter with user field "portal-staff-type" value, check if existed taxonomy "staff-portal-access-type" filter is not in user field "portal-staff-type" value, then output empty result.

If you need more assistance for it, since it is a custom PHP codes problem, please provide a test site with the same problem, also point out:
1) Confirm what do you want to achieve: a) or b)
2) Problem page URL and view URLs
3) Where I can edit your PHP codes
I need a live website to test and debug the PHP codes.

#1501927

Luo,

I had a meeting with my client last week, so I had to come up with a different solution. I no longer need to debug this issue. It is, in fact, not solved ... however, I don't wish to monopolize your time unecessarily.

If I need this in the future, I will certainly open a new support ticket.

Thank you for your assistance!

#1502539

OK, feel free to create new thread if there is other qustion.