Skip Navigation

[Resolved] Author fields in Query Filter for Posts

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

Last updated by ioannisM-2 7 years ago.

Assisted by: Minesh.

Author
Posts
#598729

This ticket is for Luo and it is the second part of this ticket: https://toolset.com/forums/topic/users-views-pagination-issue/. I just opened a new thread as this is a new question.

Whatever I tried the posts view is always faster and because I am planning a big website, I decided to create an extra post type to hold the user profiles and use a posts view that can be rendered in less time and with much less queries.

My question is, how could I add a filter in the post type's parametric search so the visitors can choose to see posts of the authors that have the user field wpcf-provider="Type-A" or wpcf-language="English" ? Is there a way to achieve that? I found this thread:
https://toolset.com/forums/topic/parametric-search-by-author/
but here the posts can be filtered only by 2 fields of the author: his email or id. Is there a way to filter by custom user fields too?

Thank you!

#598801

Dear ioannisM-2,

There isn't such a built-in feature within Views, see wordpress document:
https://codex.wordpress.org/Class_Reference/WP_Query#Author_Parameters
No option to filter by custom user field in a post view,

I suggest setup the custom fields into the CPT you mentioned above, for example:
Custom post type "authors-cpt"
-- with custom post field "wpcf-provider" + custom post field "wpcf-language"

Each wordpress user can have only one post "authors-cpt" post, they can manage their own field values.
Then you will be able to setup a view to query "authors-cpt" posts, filter by the custom field "wpcf-provider" + "wpcf-language", see our document:
https://toolset.com/documentation/user-guides/filtering-views-by-custom-fields/

#598839

The user fields are updated in a daily basis from another site that the users are registered too. That makes it difficult to use the post fields of the "author-cpt" of this site to make the search. I need to use in the parametric search the author fields

If I change the query filter to "Post Search Filter" instead of the "Post Author" could it be possible to make it work and be able to filter the posts by another author field except email and id? In a previous thread you helped me use the "post search filter" in the query and manually add the [wpv-filter-search-box] shortcode in the parameric search to filter the posts by transients assigned to the author of the post.

Here is the code

add_filter( 'wpv_filter_query', 'author_a_filter_func', 1, 3 );
  
function author_a_filter_func( $query_args, $view_settings, $view_id ) {
  
    if ( $view_id == 511 && isset($_GET['users_a'][0]) ) {
		$logged_in_users = get_transient('users_a');
		
		$logged_in_users_ids = array();
		foreach($logged_in_users as $k => $v){
			$logged_in_users_ids[] = $k;
		}
	  
	  if($_GET['users_a'][0] == 1){
		$query_args['author__in'] = $logged_in_users_ids;
	  }
	  if($_GET['users_a'][0] == 0){
		$query_args['author__not_in'] = $logged_in_users_ids;
	  }
    }
    return $query_args;
}

The link to the page with this example view is:
hidden link

Could it be possible to use a similar approach here and filter the posts by an author field?

Thank you!

#599089

I have also found from another thread this code that helps to show the posts only from authors that have specific roles

add_filter( 'wpv_filter_query', 'prefix_show_only_members', 10, 3 );
    
function prefix_show_only_members( $query, $view, $view_id ) {
    if ( $view_id == 20879 ) {
        $roles = array('member', 'test_role');
        $users = array();
        foreach($roles as $role){
            $args = array(
                'role' => $role,
                'fields' => 'id'
            );
            $tmp = get_users($args);
            $users = array_merge($users, $tmp);
        }
        if(empty($users)){
            $users = array(0);
        }
        $query['author__in'] = $users;
    }
    return $query;
}

Could it be possible to modify this code and use specific user fields instead of roles?

Thank you again!

#599128

Q1) Could it be possible to use a similar approach here and filter the posts by an author field?
As I mentioned above, there isn't such a built-in feature within Views, and there are large number of wordpress users in your website, I don't think it is a good idea to filter the posts by an author field, I suggest you consider that:
When you update the user fields in daily basis from another site, update the post fields too. For example, when you update the user's field, trigger a PHP function, in this PHP function, locate the post ID of the "authors-cpt" specific post, then update the post field:
https://codex.wordpress.org/Function_Reference/update_post_meta

Q2) Could it be possible to modify this code and use specific user fields instead of roles?
Same as above, there isn't such a built-in feature within Views, see the answer of Q1).

#599143

Dear Luo,

Could you point me to the right direction how I could do that? Is it possible to trigger a change to this site although the user's meta is updated from another site? In the other site the creation of a child post via the cred_save_data API is updating an author's user field. Is it possible the same time a post field from this site been updated too? How could I make this "connection" between the 2 sites?

#599163

It depends the tools that you "update the user fields in daily basis from another site", if you are using other plugin to do it, please ask help from the plugin author, check if there is any action hook you can use, just like the action hook of CRED plugin.

#600064

Dear Luo,

please don't close this thread yet... I need some time to do my tests

Thank you!

#600076

OK, please update this thread if you still need assistance.

#600289

I am close but I have stuck at the last part ... Let's summarize the previous and add the new steps I have done too:

1) I need to use a query filter in the parametric search of posts that is a post's author field
2) You advised me that 1) is too hard to achieve or very expensive if we consider the database queries and the amount of users and posts so I should find another way to do what I need
3) The author's user field "my skills" is updated in website B with the value of the post field "skills site b" every time a post is updated . I use a cred save data hook to update the user field:

$skills = get_post_meta( $post_id, 'wpcf-skills-site-b', true );
update_user_meta($user_id, 'wpcf-my-skills', $skills);

4) The solution you recommended, is when a post is updated in site B and the user field is updated also the same time (users are common for both sites) then I should trigger a function to update the cpt-author's post field in site A. The field of cpt-author in site A is "skills site a" and the id of the post is $authorcptid
5) I have upgraded my account, so now I have added toolset plugins in both sites. The function I have added in site A to update the cpt-author fields when a user field changes is a cred save data hook with the code:

$skill = get_user_meta( $user_id, 'wpcf-my-skills', true ); 
 update_post_meta($authorcptid, 'wpcf-skills-site-a', $skill);

but I cannot figure out how I can trigger an action in site A as the code that updates the user field and the cred form for the submition of the original post live in site B. I used the same code in site B and worked fine but site A does not listen the changes when a user field is updated via site B and my code is not triggered. I tried to do what you are suggesting here https://toolset.com/forums/topic/author-fields-in-query-filter-for-posts/#post-599128 but I cannot make it work... Could you please help me a little bit with that last part?

Thank you!

#600387

Q1) There isn't such a built-in feature within Views plugin, same as the WordPress class WP_Query:
https://codex.wordpress.org/Class_Reference/WP_Query#Author_Parameters
no such a feature to use a query filter in the parametric search of posts that is a post's author field

Q2) For the new question:
but I cannot figure out how I can trigger an action in site A as the code that updates the user field and the cred form for the submition of the original post live in site B ...
I assume we are talking about this case:
1) user submit a CRED form in site A
2) you are going to setup custom codes with action cred_save_data to update post field value in site B
If it is, it is not possible within CRED form, all Toolset plugins only work in the same site, so it requires custom codes, which is out the range of Toolset support, you can check it with our Toolset contractors:
https://toolset.com/contractors/

#600601

Dear Luo,

You say that the following case is possible but without the use of cred hook save data:

1) user submits a CRED form in site A
2) I can setup custom codes to update post field value in site B

I also have this set up:
When 1) happens a user's field is updated with the value of the post field.

This user is registered in site B too. Both sites share the same userbase. So I could just show the user's field in a view in site B but I need the visitors to be able to search posts by this field. That's why I need to update site's B post field

You recommend as a solution 2). I would like to ask you in this case is it possible to achieve what I need without the use of rest api and could I just create a simple function that would do the job? Is that what you mean with 2)?

Could you please point me to the right direction or give me a couple of links with some examples or tutorials that I could start from?

Thank you a lot!

#600721

Please elaborate more details for the questions:
How do you setup the "Both sites share the same userbase"?

And my recommendation is in the answer:
https://toolset.com/forums/topic/author-fields-in-query-filter-for-posts/#post-599128
When you update the user fields in daily basis from another site, update the post fields too.
This is for same site: when you update the user fields in site A, update the post field of site A too.

But all of them will involve custom codes, according to our support policy, we don't provide the custom codes support:
https://toolset.com/toolset-support-policy/

Currently you can check it with our Toolset contractors:
https://toolset.com/contractors/

#600840

Yes Luo, of course I know that you cannot provide custom code solutions according to toolset's support policy.

Here are the details you asked: Both sites share the same database and the same user tables. User field is updated via a cred save data hook from site B when a post field is updated in site B. The problem is that this function exists in site B and I do not know how to pass the changes to a post to site A.

Your recommendation is "when you update the user fields in site A, update the post field of site A too. But all of them will involve custom codes" I am not asking for help with the custom codes, I just wonder if it would be possible to point me to the right direction in order to achieve what you recommend. Where should I start from learning? Do I need Rest Api or sth else in order to achieve that?

#601116

Minesh
Supporter

Languages: English (English )

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

Luo is on vacation. This is Minesh here and I'll take care of this ticket and try to help you further. Hope this is OK.

If you are talking about site hosted with different domains and want that site B field should update when site A field is updated or changed. Yes - this is cross site communication and you need to use REST API or run CRON but this is purely depends upon your needs. I suggest you should take expert advise on this from experts of concern field.