Skip Navigation

[Resolved] Filter view based on custom user meta

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/Karachi (GMT+05:00)

This topic contains 4 replies, has 3 voices.

Last updated by Waqar 10 months, 2 weeks ago.

Assisted by: Waqar.

Author
Posts
#2687721
toolset.jpg

Need to filter view that list custom post type. I found this tread:

https://toolset.com/forums/topic/conditional-display-posts-based-on-custom-user-meta/

but can not make it.

Custom meta filed is created with toolset, meta_key 'user_liked_posts' (multiple lines)

I made shortcode:

add_shortcode('my-strains-id', function() {
$user_id = get_current_user_id();
$liked_posts = get_user_meta($user_id, 'user_liked_posts', true);

if (!empty($liked_posts)) {
return is_array($liked_posts) ? implode(',', $liked_posts) : $liked_posts;
}

return '';
});

This shortcode return custom post ids that current user liked in form: 3378,3416,3335

Now I have to filter custom post type 'weed strains' with this values (to list strains that user liked)

Problem is that I dont know how to do that.

All my views are made with blocks (dont know how to use legacy)

In practice, this field should be used:

https://toolset.com/wp-content/uploads/tmp/toolset_6.jpg

To filter posts with ID's from shortcode [my-strains-id] or with values from custom user meta filed 'User liked posts' with slug 'user_liked_posts'

Thanks in advance

#2687794

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Screenshot 2024-03-13 at 21.22.14.png

Hi there

For this to work you need to insert the View via shortcode (the wpv-view shortcode), which you can add attributes to, that you then use in the filter shown in your screenshot.

This is straightforward with legacy Views; it can be done with block-based Views, there is just an extra layer, because you need to create the View with blocks somewhere other than where you want to use it (because you need to insert it there via shortcode). So if you made the View in the page where you want it you should delete the View.

Start again, adding the View to a Content Template which doesn't get assigned to anything. Effectively this template is just somewhere to park the View as you create it, because a block-based View has to live somewhere that uses the block editor, and you probably don't want a redundant page on your site that exists just to hold the View.

Having made the View, add a Query Filter as per your screenshot, and choose that the values come from a shortcode attribute. We can call the attribute anything; the default is "ids", but so you can follow what we're doing more clearly, let's call the attribute "likes".

To help make sure the wpv-view shortcode is formatted correctly, let's go to Toolset > Settings and make sure the Toolset shortcodes menu will be visible in the admin toolbar (screenshot).

While at Toolset > Settings go to the Front-end Content tab. Add the name of your custom shortcode (my-strains-id) to the Third-party shortcode arguments settings.

Now go to the page where you want this View to appear.

Instead of inserting the View with a View block, insert a Shortcode block. Use the Toolset shortcodes button in the admin toolbar to generate the shortcode you will need to insert the View you created in the prior step (you'll find the available Views towards the bottom).

Insert that shortcode in the editor for the Shortcode block, and then edit it to add an attribute "likes" where the value comes from your custom shortcode, so it will look something like this:

[wpv-view name="My New View" likes="[my-strains-id]"]

This will pass the list of IDs to the View for use by the Query Filter, and should work the way you intend.

#2687849

Thank you Nigel for a very detailed guide.

I did everything as you described but the posts are not filtered.

#2688037

Hi,

Glad that Nigel's message helped.

To troubleshoot, why the filtering by IDs is not working, I'll need to see how this view and the shortcode are being used in the admin area.

Can you please share temporary admin login details, along with the link where this view can be seen?

Note: Your next reply will be private and making a complete backup copy is recommended before sharing the access details.

regards,
Waqar

#2688394

Thank you for sharing the access details.

I noticed that on the page, the following shortcode was being used to load the content template 'My Strains Like':


[wpv-post-body view_template="my-strains-like" likes="[my-strains-id]"]

But passing the shortcode attribute likes="[my-strains-id]" has no effect here, because that is expected for the view's shortcode ( ref: https://toolset.com/documentation/programmer-reference/views/views-shortcodes/#wpv-view ), and not the content template's shortcode ( ref: https://toolset.com/documentation/programmer-reference/views/views-shortcodes/#wpv-post-body ).

And because the content template 'My Strains Like' is loading the view 'My Strains Like' through the view block and not the view's shortcode, it is not possible to pass the shortcode attribute likes="[my-strains-id]" to that view.

To make this filtering work, I used a different approach of filter 'wpv_filter_query' and removed the post ID filter from the view's settings:
( ref: https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query )


add_filter( 'wpv_filter_query', 'filter_posts_IDs_6598', 1000 , 3 );
function filter_posts_IDs_6598( $query_args, $view_settings ) {

    if ( !is_admin() && ( isset($view_settings['view_id']) && $view_settings['view_id'] == 6598) ) {

        $query_args['post__in'] = explode(",",do_shortcode('[my-strains-id]'));   
    }
    return $query_args;
}

The above code snippet is included through the Toolset's custom code feature ( ref: https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/ ) on the website.

This code filters the view with the ID '6598' ( i.e. 'My Strains Like') and limits the results only to items coming from the shortcode '[my-strains-id]'.

I hope this helps and please let me know if you need further assistance.

#2688405

Tnx a lot.

I appreciate your help Waqar.