Skip Navigation

[Resolved] Use wpv_filter_query to filter by Post ID match to an Array

This support ticket is created 2 years, 6 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

This topic contains 2 replies, has 2 voices.

Last updated by benjaminJ-3 2 years, 6 months ago.

Assisted by: Shane.

Author
Posts
#2318689

Tell us what you are trying to do?
how do I filter to only show posts id's in_array of $user_providers_array

Is there any documentation that you are following?
You deleted all the docs I would normally have followed to resolve this.

This is what I have so far:

//Return only service providers from the current user:
add_filter( 'wpv_filter_query', 'prefix_show_only_current_user', 101, 3 );
function prefix_show_only_current_user( $query_args, $view_settings, $view_id ) {

    if ($view_id == 551) {
      global $current_user;
      var_dump($query_args);
      $pType = (array) $query_args['post_type'];

      $providers = (types_render_usermeta( 'user-s-service-provider-id', array( 'separator' => ',', 'user_current' => 'true') ));
      $user_providers_array = explode (',',$user_prog_id);

      if ( !is_admin() && in_array( 'service-provider', $pType ) ) {
          //how do I filter to only show posts id's in_array of  $user_providers_array
      }
    }
    return $query_args;
}
#2318803

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Benjamin,

Thank you for getting in touch.

You will actually use the "post__in" attribute to do this.

Here is an example below.

//Return only service providers from the current user:
add_filter( 'wpv_filter_query', 'prefix_show_only_current_user', 101, 3 );
function prefix_show_only_current_user( $query_args, $view_settings, $view_id ) {
 
    if ($view_id == 551) {
      global $current_user;
      var_dump($query_args);
      $pType = (array) $query_args['post_type'];
 
      $providers = (types_render_usermeta( 'user-s-service-provider-id', array( 'separator' => ',', 'user_current' => 'true') ));
      $user_providers_array = explode (',',$user_prog_id);
 
      if ( !is_admin() && in_array( 'service-provider', $pType ) ) {
          //how do I filter to only show posts id's in_array of  $user_providers_array
         $query_args['post__in'] =   $user_providers_array;
      }
    }
    return $query_args;
}

Thanks,
Shane

#2318845

My issue is resolved now. Thank you!

For anyone who finds this thread in the future, I had an error in my $user_providers_array variable and I also found you don't need to call global $current_user; (I'm guessing the types_render_usermeta has that baked in).

Working code:

add_filter( 'wpv_filter_query', 'prefix_show_only_current_user', 101, 3 );
function prefix_show_only_current_user( $query_args, $view_settings, $view_id ) {

    if ($view_id == 551) {
      global $current_user;

      $pType = (array) $query_args['post_type'];

      $providers = (types_render_usermeta( 'user-s-service-provider-id', array( 'separator' => ',', 'user_current' => 'true') ));
      $user_providers_array = explode (',',$providers);

      if ( !is_admin() && in_array( 'service-provider', $pType ) ) {
          $query_args['post__in'] = $user_providers_array;
      }
    }
    return $query_args;
}
This ticket is now closed. If you're a Toolset client and need related help, please open a new support ticket.