Skip Navigation

[Resolved] search user by custom fields in views

This support ticket is created 3 years, 11 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: Africa/Casablanca (GMT+01:00)

This topic contains 7 replies, has 3 voices.

Last updated by Jamal 3 years, 11 months ago.

Assisted by: Jamal.

Author
Posts
#1934331

search user by custom fields in views

Hi

Is there an option to search user by custom fields in views ?

Do you have any documentation for search user by custom fields in views ?

Can you provide a custom function for search user by custom fields in views ?

website: hidden link

Thanks

#1934653

Nigel
Supporter

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

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

Hi there

Custom search for users isn't possible, even with the legacy Views (the newer Blocks interface currently only supports Posts).

WordPress isn't very good at displaying and querying users in the same rich way that it is with posts, with templates for posts, archives etc. and so our recommendation is to create profile posts that are linked to users, and create templates for those profiles, perform searches etc.

That is described here: https://toolset.com/documentation/legacy-features/views-plugin/how-to-create-custom-searches-and-relationships-for-users/

We do plan in one of the upcoming dev cycles to improve this by automatically syncing data between users and profile posts to more tightly integrate them.

#1935489

Hello

We already tried that user connected to post but that option will not work for subscription functionality.

Do you have other option?
Is there a way we can custom this?
Can you provide a custom function for this?

Thanks

#1936055

Hello,

Representing users with a custom post type is very powerful and can allow you many features. I am sure there is a way to integrate it with your subscription functionality. If you can provide more details about the case we'll give you accurate advice.

Keep in mind that legacy views are tight to the internal WordPress queries:
- Post views are tight to WP_Query.
- Taxonomy views are tight to WP_Term_Query.
- User views are tight to WP_User_Query.

You can hook into the wpv_filter_user_query hook and modify the underlying WP_User_Query arguments:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_user_query

add_filter( 'wpv_filter_user_query', 'prefix_search_custom_field' );
 
function prefix_search_custom_field( $query_args ) {
    
    $search = "keyword";

    $query_args['meta_query'] = array(
          array(
                    'key'     => 'wpcf-custom-field',
                    'value'   => $search,
                    'compare' => 'LIKE'
                )
    );
    return $query_args;
}

You can also hook into WordPress pre_get_users hook like this thread:
- https://wordpress.org/support/topic/adding-custom-user-meta-to-search-field-on-users-page-in-wordpress/

I hope this helps. Let me know if you have any questions.

#1939745

OUr issues using custom post type for subscription functionlity is

1. custom post type is not related to woocommerce orders.

- user should only create account/publish the post once the is order complete.
- delete user/post if they cancel or unsubscribe order.
- basicaly the woocommerce orders should control the user/post.

2. Profile behavior

- when user tried to buy or switch another subscription they endup having 2 post (should only be one for displaying profile)

Im not sure how can we use this wpv_filter_user_query in a views search form

Can you provide more detail about this wpv_filter_user_query

Thanks

#1943699

My apologies for the late reply, but I do not work on Wednesdays and Thursdays as you can see on my profile page https://toolset.com/forums/users/jamal-b/

Regarding what you would like to implement between orders and users or posts, I suggest that you check the following articles, the second one is related to subscriptions, but I believe it to hold valuable information:
- https://toolset.com/course-lesson/charge-users-for-submitting-content/
- https://toolset.com/course-lesson/charge-users-for-registering-premium-accounts/

Regarding the Profile behavior, I need more information to better understand it.

For the custom code that I shared, it can be added to the theme's functions.php file, or to Toolset->Settings->Custom Code. But, you need to build the search form yourself. As Nigel explained, views do not offer a built-in search for users. And that's why we suggest using a separate CPT. Read more about the hook here https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_user_query

Please note that, even though we sometimes provide it, custom code is out of the scope of this support forum. If you are not comfortable with programming, consider hiring a developer. You'll find a list of our partners here https://toolset.com/contractors/

#1950063

Using this option

- https://toolset.com/course-lesson/charge-users-for-submitting-content/

Is there a way to create user if the payment/order was completed?
Is there a way to draft or pending the post created by this user if they want to cancel the completed subscription/order?

Thanks

#1951151

Is there a way to create user if the payment/order was completed?
Yes, that needs to use our integration plugin to WooCommerce, "Toolset Forms Commerce", create a product that will hold the price and will be bought, and connect it to a User Form.
https://toolset.com/download/toolset-cred-commerce/

Is there a way to draft or pending the post created by this user if they want to cancel the completed subscription/order?
Yes, I think so, but it may need some custom code. Check these replies in similar tickets:
- https://toolset.com/forums/topic/cred-commerce-with-woocommerce-subscriptions/#post-163510
- https://toolset.com/forums/topic/member-profiles-being-made-active-inactive-based-on-subscription-status/#post-1639063

Basically, it will need to hook into the WooCommerce subscription processes. You may want to read more about it from a development perspective. https://docs.woocommerce.com/document/subscriptions/develop/

While working with WooCommerce Subscription, please be aware of this known issue https://toolset.com/errata/posts-published-with-forms-disappear-after-completing-woocommerce-order/

However, for support rules, we are able to handle only one issue at a time. This helps us to bring you a better service and also helps other users to find all the information here exposed. For that reason, I have to kindly ask you to open a new thread if you need further assistance.