Forums Forums Search Search Results for 'function sf_edit_query_args'

Viewing 10 results - 61 through 70 (of 123 total)
  • Author
    Search Results
  • #180865

    Anonymous
    Inactive

    Hey,

    So I attempted to add additional arguments and tested it with my original search form/results page and received this error – Warning: trim() expects parameter 1 to be string, array given in /home/andreymh/public_html/v2/wp-includes/class-wp-meta-query.php on line 577

    This is the argument code I put in functions.php –

    function filter_function_name( $query_args, $sfid ) {

    //if search form ID = 225, the do something with this query
    if($sfid==12)
    {
    //modify $query_args here before returning it
    $query_args = array(
    ‘meta_key’ => ‘property_type’,
    ‘meta_compare’ => ‘LIKE’,
    ‘meta_value’ => array(
    ‘modular_with_land’,
    ‘manufactured_with_land’,
    ‘mobile_with_land’
    )
    );
    }

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

    In this case 12 is the ID of the main search page form, not the curated page I want this to actually be applied to (which I’m not sure how that will work since that page only has a results shortcode).

    As for the infinite scroll, yes. I created a search form for the main search page and then duplicated it to create the curated pages. On those forms, I deleted all the fields from the form UI per your suggestion, changed what URL the results get displayed on, and changed the post meta filters.

    #180733

    Anonymous
    Inactive

    Trevor, I appreciate the help so far. This is the filter I tried in our functions.php file:

    
    function filter_function_name( $query_args, $sfid ) {
    	
    	//if search form ID = 35922, the do something with this query
    	if($sfid==35922)
    	{
    		//modify $query_args here before returning it
    		$query_args['post_mime_type'] = 'application/pdf';
    	}
    	
    	return $query_args;
    }
    add_filter( 'sf_edit_query_args', 'filter_function_name', 20, 2 );
    

    I got the ID by looking at the query string in the URL on the edit page, as well as verifying it through data-sf-form-id="35922" on the form itself using Chrome inspector.

    For our search page, we’re including posts, pages, and media (trying to only include PDFs from media). After I added this to functions.php, it actually stopped ALL search items from loading.

    Any ideas?

    #180694

    Anonymous
    Inactive

    So this is the code block I’d be using?

    
    function filter_function_name( $query_args, $sfid ) {
    	
    	//if search form ID = 225, the do something with this query
    	if($sfid==225)
    	{
    		//modify $query_args here before returning it
    		$query_args['somearg'] = 'newvalue';
    	}
    	
    	return $query_args;
    }
    add_filter( 'sf_edit_query_args', 'filter_function_name', 20, 2 );
    
    

    Can you give me any information on how I’d apply your suggestion to this filter? Sorry, I’m not much of a coder.


    Ross
    Keymaster

    Hi Pablo

    Apologies for the delay coming back.

    Our filter, sf_edit_query_args is meant especially for this reason, so your changes are reflected across our fields.

    I just did a quick test, with a different meta query (using price, on my shop) and it worked exactly as expected, with all the fields updating their count numbers:

    add_filter( 'sf_edit_query_args', 'kp_exclude_sold_older_than_two_months', 100, 2 );
    function kp_exclude_sold_older_than_two_months( $query ) {
    	
    	$query['meta_query'] = array( 
    		array(
    			'key' => '_price', 
    			'value' => 11, 
    			'compare' => '>=', 
    			'type' => 'NUMERIC' 
    		)
    	);
    	return $query;
    }

    The only thing is, some fields, such as post type, don’t have a dynamic count, so would remain unaffected regardless.

    Could this be the issue you are seeing?


    Anonymous
    Inactive

    We are in the need to customize a bit more the criteria for fetching using dynamic information during the searches.
    We are looking for altering the query posts arguments to exclude some posts from the search.

    But basically the process of selection of those posts are dynamic and we can’t built it using the UI.
    Example:

    Exclude post which custom meta field ‘ListDate’ is older than two months.

    The following code kind of works but leaves all complex filtering counts unaltered in the dropdown filters we have.
    Is there a way to make the dropdown filters created to be aware of this update?

    
    add_filter( 'sf_edit_query_args', 'kp_exclude_sold_older_than_two_months', 100, 2 );
    function kp_exclude_sold_older_than_two_months( $query ) {
    
    	$time_ago = date('Y-m-d', strtotime('-2 months'));
    
    	$query['meta_query'] = array( 
    	    array(
    		        'key' => 'ListDate', 
    		        'value' => $time_ago, 
    		        'compare' => '>=', 
    		        'type' => 'DATE' 
    	        )
            );
    
    	return $query;
    }
    

    Thanks in advance.

    Pablo.

    #178610

    Anonymous
    Inactive

    Hi,

    thank you Ross for your answer. The project was kind of on hold for a while, so I’m only replying now.
    I’ve played a bit with sf_edit_query_args and got it working.
    So my setting was as follows:

    I have a home.php template set up, containing:

     <?php echo do_shortcode('[searchandfilter id="' . BLOG_FILTER . '"]'); ?>
            <?php echo do_shortcode('[searchandfilter id="' . BLOG_FILTER . '" show="results"]'); ?>

    I also have a page named Blog that is set up as the Posts Page on /wp-admin/options-reading.php

    In backend I have a filter whose Display settings are:

    Display results method: Using a Shortcode
    Results URL: http://website.com/blog
    Load results using Ajax? Checked
    Make searches bookmarkable? Checked

    I then have a filter (usually put in functions.php):

    /**
     * Modify Search & Filter PRO plugin's query so that it returns posts of authors that are not anonymous.
     */
    function domain_sf_filter_query_args($query_args, $sfid)
    {
        if ($sfid == 95 && isset($_GET['authors'])) {
            $query_args['meta_key'] = 'anonimni_avtor';
            $query_args['meta_value'] = 1;
            $query_args['meta_compare'] = '!=';
        }
        return $query_args;
    }
    add_filter('sf_edit_query_args', 'domain_sf_filter_query_args', 10, 2);

    So, when visiting an unfiltered page it returns all posts, but when filtering through authors, it returns only posts from that author that are not marked as anonymous author.

    Thank you all for your help 🙂

    Have a great start of next week!

    Domen

    #171898

    Ross
    Keymaster

    Hi Domen

    It looks like the problem is your usage of sf_edit_query_args

    The reason for the filter, is so that it works effectively for all our queries and ajax.

    One of the issues I see with your code is actually with this line:

    if (is_author()) {

    A typical ajax request will be performed at wp-admin/admin-ajax.php, the function is_author will always return false, this is applicable when using our filter sf_edit_query_args and your display method is set to shortcode

    I added an extra line in our example to show how to set query arg properties using our function – https://www.designsandcode.com/documentation/search-filter-pro/action-filter-reference/#Edit_Query_Arguments

    If you wanted is_author to work correctly, you would need to use the display method archive where our ajax requests are loaded from the same page.

    Hope that makes sense

    Thanks

    #169305

    Anonymous
    Inactive

    I have tried, it’s not working. Maybe there is a mistake? I although don’t see any possibility in admin-panel to add relation ‘or’ to meta_query.

    function add_query_args($query_args, $sfid) {
            if ($sfid == 218) {
                date_default_timezone_set('Europe/Kiev');
                $now = date('Y-m-d H:i:s');
                $query_args = array(
                    'meta_query' => array(
                        'relation' => 'OR',
                        array(
                            'key'       => 'event-date-time-start',
                            'compare'   => '>=',
                            'value'     => $now,
                            'type'      => 'DATETIME',
                        ),
                        array(
                            'key'       => 'event-date-time-finish',
                            'compare'   => '>=',
                            'value'     => $now,
                            'type'      => 'DATETIME',
                        ),
                    ),
                );
            }
            
            return $query_args;
        }
    
        add_filter('sf_edit_query_args', 'add_query_args', 20, 2);
    #168826

    Ross
    Keymaster

    Hi Liam

    I had a look (and was surprised that you could add an orderby with random seed to the query object so easily)! You learn something new every day!

    Anyway, I tested the code you were using and it mostly seemed correct – but I had some trouble getting it working.

    I realised, in this scenario, using our filter sf_edit_query_args was the wrong filter to use, because it is called several times (once for the main query, and again, while generating our filters) – which probably meant your seed being continually reset, or reset 3 times in 1 page load.

    — As a side note, the whole purpose of this filter we use is so that any modification you do to the query, is also reflected in the filter counts…

    However, in this case, ordering the results will have no effect on the filter counts, so its not necessary and we can go ahead and use pre_get_posts.

    Using your code, and some I’ve found online, I think I’ve managed to successfully do this:

    
    session_start();
    add_filter( 'pre_get_posts', 'sf_sort_random_seed', 200, 2 );
    function sf_sort_random_seed( $query ) {
    
    	if( !is_admin() && $query->is_main_query() ) {
    			
    		//$query->set( 'posts_per_page', 1 ); // testing
    		
    		//grab the ID from the query_vars
    		$sfid = 0;
    		if(isset($query->query_vars['search_filter_id'])) {
    			$sfid = $query->query_vars['search_filter_id'];
    		}
    
    		if($sfid==27339||$sfid==27361) {
    
    			// Reset seed on load of initial archive page
    			$sf_paged = 0;
    			if(isset($_GET['sf_paged'])){
    				$sf_paged = $_GET['sf_paged'];
    			}
    
    			if( $sf_paged == 0 || $sf_paged == 1 ) {
    				if( isset( $_SESSION['seed'] ) ) {
    					unset( $_SESSION['seed'] );					
    				}
    			}
    
    			// Get seed from session variable if it exists
    			$seed = false;
    			if( isset( $_SESSION['seed'] ) ) {
    				$seed = $_SESSION['seed'];
    			}
    
    			// Set new seed if none exists
    			if ( ! $seed ) {
    				$seed = rand();
    				$_SESSION['seed'] = $seed;
    			}
    
    			// Inject the order into the query
    			$query->set( 'orderby', 'RAND(' . $seed . ')' );
    		
    		}
    	}
    	
    	return $query;
    }

    This seems to be working for me anyway! Just watchout when using sessions, I’ve heard many cases of them not working in WP, and needing to use transients instead.

    Let me know if it works!

    Thanks

    #168110

    Anonymous
    Inactive

    Hi Trevor,

    I have been doing exactly that and I think I have gotten pretty close to getting this solved but I have hit another snag. I think the only problem I have now is that I cant get the session variable to stick whilst using the filter you supplied. The filter might be running before the session data is loaded or the session data may not work inside of the filter. I might need to find another method of storing the rand seed variable into user cookies or something similar.

    I have modified the sf form and taken it off rand. I am now using the function to apply the random order, which means I can control the incoming arguments whereas I couldnt before. I am collecting the query args from the filter then assessing the paged data and at that point will create or reuse the random seed session variable. The only problem I have is the filter is not detecting the stored session data, I assume this is because the filter is running before the session data is loaded? that would explain why its being reset every time.

    I feel like is pretty close to working and would really appreciate if you could ask your developer to take a quick look at the function. This is what I have so far:

    session_start();
    add_filter( 'sf_edit_query_args', 'filter_function_name', 20, 2 );
    function filter_function_name( $query_args, $sfid ) {
    
    	if($sfid==27339||$sfid==27361) {
    
    	  	// Detect first page and reset seed
    		if( ! $query_args['paged'] || $query_args['paged'] == 0 || $query_args['paged'] == 1 ) {
    			?><p>first page<p><?php 
    			if( isset( $_SESSION['seed'] ) ) {
    				unset( $_SESSION['seed'] );
    				?><p>seed unset<p><?php 	
    			}
    		} else {
    			?><p>not first page<p><?php 
    		}
    		?><p>session seed value: <?php print_r($_SESSION['seed']);?></p><?php 
    		
    		// Get seed from session variable if it exists and store it
    		$seed = false;
    		if( isset( $_SESSION['seed'] ) ) {
    			$seed = $_SESSION['seed'];
    			?><p>seed session reused : <?php echo $seed ;?><p><?php 
    		}
    	
        	// Set new seed if none exists
        	if ( ! $seed ) {
          		$seed = rand();
          		$_SESSION['seed'] = $seed;
          		?><p>seed session created: <?php echo $seed;?><p><?php 
        	}
    
        	// Inject the order into the query
        	$query_args['orderby'] = 'RAND(' . $seed . ')';
    
    	}
    
    	return $query_args;
    }
    
Viewing 10 results - 61 through 70 (of 123 total)