Skip Navigation

[Resolved] Custom query filter with multiple conditions 2

This support ticket is created 3 years, 2 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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 4 replies, has 2 voices.

Last updated by umbertoZ 3 years, 2 months ago.

Assisted by: Christian Cox.

Author
Posts
#1929535

Hi, Minesh helped me with some custom code on this topic:

https://toolset.com/forums/topic/custom-query-filter-with-multiple-conditions/page/2/

This is the custom code:

function func_remove_unwanted_posts( $query_args ,$view_settings, $view_id ) {
    global $post;
    global $current_user; 
    global $WP_Views;
   
    if ( in_array($view_id, array( 9023, 19926, 18389) ) ) {
       
      $wpvprofile = do_shortcode("[wpv-attribute name='wpvprofile']");
       
      $custom_query = new WP_Query( 
                        array(
                            'post_type' => 'follower',
                            'posts_per_page' => -1,
                            'author__in'=>array($current_user->ID),
                            'fields'=>"ids",
                            'meta_query' => array(
                                    'relation'=>"AND",
                                        array(
                                        'key'     => 'wpcf-followed-id',
                                        'value'   => array($wpvprofile, 9764),
                                        'compare' => 'NOT IN'),
                                        array(
                                            'key'     => 'wpcf-followed-id',
                                            'compare' => 'EXISTS'),
                                        )
                ));
       
      $found_posts = $custom_query->posts;
       
         
         
      $query_args['post__in'] = $found_posts;
      }
     
    return $query_args;
}
add_filter( 'wpv_filter_query', 'func_remove_unwanted_posts', 10, 3);

Now I found a problem with the custom code.

In the case of 'follower' CPT, the code is getting only posts where the author is the current_user. I need to exclude the other author's posts only when 'followed-id' filed value is DIFFERENT form 'wpvprofile' attribute.

I try to better explain the scenario.
Each user has a 'profile' CPT post asigned. Then I have the 'follower' CPT with the custom post field 'followed-id'.

Everytime a user starts following another user, I create a 'follower' post where the user who FOLLOW is the author and the FOLLOWED profile ID is stored in the 'followed-id' field.

On my stream I display activity from all the followed users, from the current user and from followers when the start following. I could display 3 different kind of messages:

1- CURRENT-USER begun following USER-44

2- USER-44 begun following USER-99

3- USER-55 begun following CURRENT-USER

What I don't want to show is the number 2. It is an activity form a user that the current user is following (so it is correctly quried by the View), but I want to remove it from the query, because it is not related to the current user.

So, the problem with the custom code is that it is excluding all the posts where the current user is not the author, but I need exclude them only when 'followed-id' is DIFFERENT from 'wpvprofile'.

I hope you can help me.

thanks

#1929909

Okay this sounds like a more complex problem to solve. Let me make sure I understand so I can recommend a good solution. What you want is:
- Get all Follower posts by the current author
- AND
- Get all Follower posts by other authors where the follower-id custom field is equal to the current User's Profile ID
That combination of clauses is not really possible in a single WP_Query, so you need to combine the results of multiple queries.

It seems like you could accomplish something like this with this process:
- Create a temporary empty array
- Get all the Follower posts by all authors
- Loop over all the Follower posts and check both the post author and the followed-id custom field value
- If the post author ID is the same as the current User ID, or if the followed-id custom field value is the same as the current User's profile ID, add that post ID to the temporary array.
- After the loop, use the temporary array to set the 'post__in' query argument in the main query

Does that sound right? Or have I misunderstood the setup you have already created?

#1930131

Hi Christian, it sounds good, you have just to consider this:

The view is loading 4 different CPTs:
follower
stream-comment
experience
report

The view query filter is by author id where I pass a string of IDs by 'wpvfollow' attribute. 'wpvfollow' includes the current user ID and all the users Ids that are following or are followed by the current user.

I'm also passing this IDs by other attributes if is could be usefull (I'm not using them in the query filter):

wpvfollowed = the IDs of the users that the current user is following
wpvfollowers = the IDs of the users that are following the current user
wpvprofile = the current user profile ID (that is not the user author ID, but the post Profile ID)

This is the full current custom code:

function func_remove_unwanted_posts( $query_args ,$view_settings, $view_id ) {
  global $post;
  global $current_user; 
  global $WP_Views;

  if ( in_array($view_id, array( 9023, 19926, 18389) ) ) {


    $wpvprofile = do_shortcode("[wpv-attribute name='wpvprofile']");

    /// getting follower posts
    $cq_follower = new WP_Query( 
      array(
        'post_type' => array('follower'),
        'posts_per_page' => -1,
        'author__in'=>array($current_user->ID),
        'fields'=>"ids",
        'meta_query' => array(
          'relation'=>"AND",
          array(
            'key'     => 'wpcf-followed-id',
            'value'   => array($wpvprofile, 9764),
            'compare' => 'NOT IN'),
          array(
            'key'     => 'wpcf-followed-id',
            'compare' => 'EXISTS'),
        )
      ));


    $found_follower = $cq_follower->posts;


    /// getting 'stream-comment','experience' posts
    $cq_get_all = new WP_Query( 
      array(
        'post_type' => array('stream-comment','experience'),
        'posts_per_page' => -1,
        'fields'=>"ids",

      ));

    $found_all = $cq_get_all->posts;

    /// getting report posts
    $site_host_user = get_user_by('login', 'sitehost');
    $cq_report = new WP_Query( 
      array(
        'post_type' => array('report'),
        'posts_per_page' => -1,
        'author__not_in'=>array($current_user->ID,$site_host_user->ID),
        'fields'=>"ids"

      ));
    $found_report = $cq_report->posts;

    $query_args['post__in'] = array_merge($found_follower,$found_all,$found_report);

  }

  return $query_args;
}
add_filter( 'wpv_filter_query', 'func_remove_unwanted_posts', 10, 3);

Cheers

#1930201

Okay, so it seems like you need to change this part of the code to modify the $found_follower array:

/// getting follower posts
    $cq_follower = new WP_Query( 
      ...
    $found_follower = $cq_follower->posts;

Instead, you need to get all Follower posts where the author ID is one of the following, regardless of the custom field value:
- equal to the current User ID
- or in the wpvfollowed array
- or in the wpvfollowers array
So you would need to use array_merge or something similar to merge all the desired author IDs into a single array you can then pass into the 'author__in" parameter.

$merged_author_ids = array( 1, 2, 3, 4 );

Initialize the empty found_follower array:

$found_follower = array();

Query the followers posts:

    $cq_follower = new WP_Query( 
      array(
        'post_type' => array('follower'),
        'posts_per_page' => -1,
        'author__in'=>$merged_author_ids,
        'fields'=>"ids"        
      ));

Then loop over the results of that cq_follower query. Compare the custom field value and author ID to determine whether or not you want to include each result in the found_followers array. Something like:

foreach( $cq_follower->posts as $this_follower) {
  $follower_author = get_post_field( 'post_author', $this_follower );
  $follower_custom_field = get_post_meta( $this_follower, 'wpcf-fieldslug', true);
  if ( /* your custom code should test the author and custom field value */ ) {
    $found_follower[]= $this_follower; // include this_follower in the found_follower array
  }
}

Right? Or am I misunderstanding?

#1930817

My issue is resolved now. Thank you!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.