Search Results for 'function sf_edit_query_args'

Forums › Forums › Search › Search Results for 'function sf_edit_query_args'

Viewing 10 results – 91 through 100 (of 141 total)
  • Author
    Search Results

  • Anonymous
    Inactive

    This solved my problem:

    https://gist.github.com/rmorse/074db89682afa8501b15

    
    	function sf_filter_query_args( $query_args, $sfid ) {
    	  
    			$posts_to_exclude = array();
    			
    			$args = array(
    				'posts_per_page' => -1,
    				'post_type' => 'grants',
    				'tax_query' => array(
    				    array(
    				      'taxonomy' => 'program',
    				      'field' => 'slug',
    				      'terms' => array('youth-service-improvement-grants'),
    				    ),
    				),
    			);
    			
    			$myposts = get_posts( $args );
    			foreach ( $myposts as $post ) : setup_postdata( $post );
    				$tags = get_the_terms($post->ID, 'person');
    				
    				if(!empty($tags)) {
    					
    					foreach($tags as $tag) {
    						$tag_slug = $tag->slug;
    						$tag_terms = array($tag_slug);
    	
    						$args = array(
    							'posts_per_page' => -1,
    							'post_type' => 'grants',
    							'tax_query' => array(
    								'relation' => 'AND',
    							    array(
    							      'taxonomy' => 'person',
    							      'field' => 'slug',
    							      'terms' => $tag_terms
    							    ),
    		                        array(
    							        'taxonomy' => 'program',
    							        'field'    => 'slug',
    				      				'terms' => array('youth-service-improvement-grants'),
    				      				'operator' => 'NOT IN'
    		                        ),
    							)
    						);
    						
    						$my_person_posts = get_posts( $args );
    						
    						if(count($my_person_posts) < 1) {
    							
    							$myperson = get_page_by_path( $tag_slug, OBJECT, 'people' );
    							
    							if(!empty($myperson)) {
    								$posts_to_exclude[] = $myperson->ID;
    							}
    						}
    					}
    				}
    			endforeach; 
    			wp_reset_postdata();
    			
    	        $query_args['post__not_in'] = $posts_to_exclude;
    	        
    	  return $query_args;
    	}
    	add_filter( 'sf_edit_query_args', 'sf_filter_query_args', 10, 2 );
    
    #135093

    Anonymous
    Inactive

    Hi Trevor,

    thanks for pointing me to Edit Query Arguments Filter, must have missed that!

    Here is how I set things up:
    – a front end form for submitting private custom posts (cpt supports author)
    – search & filter restricted to
    – post type: my custom post type
    – post status: private

    I expected search & filter to take into account that limitation to private actually limits search results to current users posts, as a regular wordpress query would do. But maybe my setup is wrong.

    If s&f doesn’t work this way, consider this a feature request. Because otherwise it renders wordpress post status private useless.

    Next, I tried your approach of editing Arguments Filter, but it gives me no results. Probably wrong syntax?

    
    function filter_user_posts( $query_args, $sfid ) {
      $user_id = get_current_user_id();
    	
      //if search form ID = 109, do something with this query
      if($sfid==109)
      {
        //modify $query_args here before returning it
        $query_args = array(
          'author' => $user_id
        );
      }	
      return $query_args;
    }
    add_filter( 'sf_edit_query_args', 'filter_user_posts', 20, 2 );
    

    Speaking of security issues: I will further investigate the URL-snooping issue. But I also have a content restrictions on single post view in other places, so posts can’t be viewed by non-authors anyway.

    Cheers,

    Georg

    #134891

    Anonymous
    Inactive

    Hey, guys!

    We’re trying to display posts differently for logged in users and anonymous users (i.e. some posts are available only for logged in users). The problem is that when connecting to the ‘sf_edit_query_args_after_custom_filter’ hook the cache query is affected and the results are incomplete; the ‘sf_edit_query_args’ hook the cache works correctly but the listing displays wrong counter.
    My question is: is there any hook that can alter only the results displayed so that the cache query is not affected?
    Bellow is the code needed to run on the results found:

    function _query_filter($query_args, $sfid) {
        $post_type = get_query_var( 'post_type' );
        $affected_post_types = array(
            'module',
            'learning-object',
            'success-story'
        );
    
        if ( !is_post_type_archive( $affected_post_types ) || is_admin() ) {
            return $query_args;
        }
    
        // echo '<pre>'; print_r( get_query_var( 'post_type' ) ); echo '</pre>'; exit;
    
        $meta_query = array(
            'relation'      => 'AND'
        );
        $posts_per_page = '12';
        $post_status = array(
            'publish'
        );
    
        if ( is_user_logged_in() ) {
            
            if ( current_user_can('partner') || current_user_can('manage_options') || ( current_user_can( 'member' ) && preg_match( '/^(\/step\/)/i', $_SERVER['REQUEST_URI'] ) ) ) {
                $post_status[] = 'draft';
                $post_status[] = 'private';
                $post_status[] = 'internal';
            }
        }
    
        
        $query_args['orderby'] = 'date';
        $query_args['order'] = 'DESC';
    
        if ( $post_type != 'learning-object' ) {
            $posts_per_page = 6;
        }
    
        if ( !isset( $_GET['_sfm_language'] ) ) {
            $selected_language = '';
            if ( !defined( 'ICL_LANGUAGE_CODE' ) ) {
                define( 'ICL_LANGUAGE_CODE', 'en' );
            }
            
            switch ( ICL_LANGUAGE_CODE ) {
                case 'fi':
                    $selected_language = 'Finnish';
                    break;
                case 'de':
                    $selected_language = 'German';
                    break;
                default:
                    $selected_language = 'English';
                    break;
            }
    
            // $meta_query[] = array(
            //     'relation'		=> 'OR',
            //     array(
            //         'key'		=> 'language',
            //         'compare'	=> 'NOT EXISTS'
            //     ),
            //     array(
            //         'key'		=> 'language',
            //         'value'		=> $selected_language
            //     )
            // );
        }
    
        if ( !is_user_logged_in() ) {
    		$meta_query[] = array(
    			'relation'		=> 'OR',
    			array(
    				'key'		=> 'is_public',
    				'compare'	=> 'NOT EXISTS'
    			),
    			array(
    				'key'		=> 'is_public',
    				'value'		=> 'yes'
    			)
    		);
    	}
        
        $query_args['post_status'] = $post_status;
        $query_args['meta_query'] = $meta_query;
        $query_args['posts_per_page'] = $posts_per_page;
    
        // echo '<pre>'; print_r( $query_args ); echo '</pre>';
    
        return $query_args;
    }
    // add_filter( 'sf_edit_query_args', '_query_filter', 10, 2 );
    add_filter( 'sf_edit_query_args_after_custom_filter', '_query_filter', 10, 2 );

    Also, how can we update the counters to reflect only the displayed results?

    #134719

    In reply to: user specific results


    Anonymous
    Inactive

    Hello Trevor,

    I’ve tried

    function filter_function_name( $query_args, $sfid ) {
        
        $cat_id = array(1);
    	
    	//if search form ID = 225, the do something with this query
    	if($sfid==32)
        {
            $query_args = array(
    		 'category__in' => array($cat_id),
                );
    	}
    	
    	return $query_args;
    }
    add_filter( 'sf_edit_query_args', 'filter_function_name', 20, 2 );
    

    Is that right? It returns no results – seems not to extend the query ?

    Best
    P.

    #131892

    Trevor
    Participant

    You would need to be running a child theme, and inside the child theme folder on your server would be a file called functions.php

    Edit that and add the sample code on new lines in the file.

    There are examples of using the code in this forum, using this search:

    https://support.searchandfilter.com/forums/search/sf_edit_query_args/

    But I am unable to provide help with the coding. For that you would need to hire a coder.

    #131208

    Anonymous
    Inactive

    I still think it’s me who is making the strange question 😉
    I hope I will give this one a last try before I have to give up, since I cannot help my customer a step further.

    I will give you a step by step list of what I have done and want to have:

    1. I just have a front end search form in which the customer uses two fields to insert the ‘height’ and the ‘width’ of something

    2. I also wnat to use S&F to search/filter on a third field (which does not need to be filled in by the customer, since that field should be the calculation of height x width. It is kind of unlogical to let the user fill in the calculated value of something so simple. Right?

    3. So I don’t need to see all the fields on the screen, I just need to get access to the two fields I mentioned. I found the names of them: _sfm_breedte (width) and _sfm_hoogte (height)

    4. I thought I could use the ‘Edit Query Arguments’ routine, and use it for the ID of my S&F serach (in my case 3954). I thought I could get the field values in that routine and make the needed calculation of the third field value there.

    5. Yes and that field might be made invisible in the front end, since the user does not need to see it. So you in ACF Pro I use the three fields, but only two shown in the search form. Ik hope that makes the appraoch for my request more clear.

    Questions:

    1. Is the routine I want to use the right one?
    2. So, how do I access and get the values of the two defined fields? I don’t need to dispaly them, just use them to make a calculation

    Example of non-PHP code in written words:

    function filter_function_name( $query_args, $sfid ) {

    //if search form ID = xxxx, the do something with this query
    if($sfid==xxxx)
    {
    get me the value of ‘height’

    }

    return $query_args;
    }
    add_filter( ‘sf_edit_query_args’, ‘filter_function_name’, 20, 2 );

    #127591

    Anonymous
    Inactive

    Yes it is, the code is as following:

    function sf_filter_query_args($query_args, $sfid) {
        echo '<pre>',print_r($query_args,true),'</pre>';
    
        if ($sfid == 327) {
            add_filter('posts_where', 'acf_posts_where_likes');
    
            $query_args['meta_query'] = array(
                'relation' => 'AND',
                array(
                    'key' => 'likes_%_gebruiker',
                    'value' => '16',
                    'compare' => 'LIKE'
                )
            );
        }
    
        echo '<pre>',print_r($query_args,true),'</pre>';
    
        return $query_args;
    }
    
    add_filter('sf_edit_query_args', 'sf_filter_query_args', 10, 400);

    Anonymous
    Inactive

    Hello guys!

    I have a custom sf_filter_query_args like so:

    function sf_filter_query_args($query_args, $sfid) {
        if ($sfid == 327) {
            add_filter('posts_where', 'acf_posts_where');
    
            $query_args['meta_query'] = array(
                'relation' => 'AND',
                array(
                    'key' => 'likes_%_gebruiker',
                    'value' => '16',
                    'compare' => 'LIKE'
                )
            );
        }
    
        return $query_args;
    }
    
    add_filter('sf_edit_query_args', 'sf_filter_query_args', 10, 2);

    Which works perfectly when I come on the page. It filters succesfully on the meta_query. But when I select a filter it returns all result and not the results where likes_%_gebruiker is 16 + the filters.

    Am i doing something wrong? Can someone help me out?

    Thanks

    #127343

    Anonymous
    Inactive

    It was actually firing too early. Still I can’t seem to add the post to the results.
    Here is my code:

    function people_filter( $query_args, $sfid ) {
       if($sfid == 7287)
        {
        	if ($_GET["_sfm_people"] != "") {
        		array_push($query_args['post__in'], intval($_GET["_sfm_people"]));
        		echo '<pre>',print_r($query_args,true),'</pre>';
      		}
        }
        return $query_args;
    }
    add_filter( 'sf_edit_query_args', 'people_filter', 20, 2 );

    And here is what the print_r is echoing:

    Array
    (
        [paged] => 1
        [search_filter_id] => 7238
        [search_filter_override] => 
        [posts_per_page] => -1
        [post_status] => Array
            (
                [0] => publish
            )
    
        [meta_query] => Array
            (
            )
    
        [post_type] => Array
            (
                [0] => lesson
                [1] => people
                [2] => video
                [3] => image
                [4] => article
            )
    
        [is_search] => 1
        [post__in] => 
    )
    #127340

    Anonymous
    Inactive

    Hmm, I’m getting an empty array. My code:

    function people_filter( $query_args, $sfid ) {
        if($sfid == 7287)
        {
          		echo '<pre>',print_r($query_args,true),'</pre>';
        }
        return $query_args;
    }
    add_filter( 'sf_edit_query_args', 'people_filter', 10, 200 );

    It’s strange because the archive page is displaying results, maybe it’s firing too late?

Viewing 10 results – 91 through 100 (of 141 total)
Search & Filter Support
Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.

We also use cookies to store items in your cart as well as allowing your to login on the site.

You can adjust all of your cookie settings by navigating the tabs on the left hand side.

By continuing to use this site, you also agree to our Privacy Policy.