Forums Forums Search Search Results for 'function sf_edit_query_args'

Viewing 10 results - 1 through 10 (of 123 total)
  • Author
    Search Results
  • #276863

    Anonymous
    Inactive

    Howdy Trevor!

    Instead of a range date picker, we’re gonna go with only one date picker.

    The thing is we need to query from that date in advance, so we did with this code.

    function filter_query_args( $query_args, $sfid ) {
    
    	// Trainings Form ID.
    	if( 1788 === $sfid && ! empty( $_GET['_sfm_start_date'] ) ) {
    		$year  = substr( $_GET['_sfm_start_date'], 4, 4 );
    		$month = substr( $_GET['_sfm_start_date'], 2, 2 );
    		$day   = substr( $_GET['_sfm_start_date'], 0, 2 );
    		$fdate = $year . $month . $day;
    
    		$query_args['posts_per_page'] = -1;
    		$query_args['orderby']      = 'meta_value_num';
    		$query_args['meta_key']     = 'start_date';
    		$query_args['meta_value']   = $fdate;
    		$query_args['meta_compare'] = '>=';
    		$query_args['meta_query']   = [[
    			'key'     => 'start_date',
    			'value'   => $fdate,
    			'compare' => '>',
    		]];
    
    		/*$q = new WP_Query( $query_args );
    
    		print '<pre><br><br>';
    			//print $fdate . '<br>';
    			print count( $q->posts ) . '<br>';
    			//print_r( $query_args );
    			//print_r( new WP_Query( $query_args ) );
    
    			foreach( $q->posts as $p )
    				print get_post_meta( $p->ID, 'start_date', true )
    					. ' : ' . $p->post_title . '<br>';
    		print '</pre>';*/
    	}
    
    	return $query_args;
    
    }
    add_filter( 'sf_edit_query_args', 'filter_query_args', 99, 2 );

    We’ve modified the query correctly, or that says the debug (as you can see commented in the code). The data is there, but somehow the results are not showing.

    Any hints? Thanks in advance.

    #276695

    Anonymous
    Inactive

    Why not use the Edit Query Arguments Filter?
    https://searchandfilter.com/documentation/action-filter-reference/
    /////////////////
    function filter_function_name( $query_args, $sfid ) {
    if($sfid==12345)
    {
    //modify $query_args here before returning it
    //$query_args[‘somearg’] = ‘newvalue’;
    $query_args[‘offset’] = ‘1’;
    }
    return $query_args;
    }
    add_filter( ‘sf_edit_query_args’, ‘filter_function_name’, 20, 2 );

    #276548

    In reply to: Exclude first post


    Anonymous
    Inactive

    Sorry for the bad writing:

    function sf_filter_query_args( $query_args, $sfid ) {
      
      //if search form ID = 225, the do something with this query
      if($sfid==31940)
      {
      	//modify $query_args here before returning it
            //only show results within categories with these IDs
      	$query_args['offset'] = '1';
      }
      return $query_args;
    }
    add_filter( 'sf_edit_query_args', 'sf_filter_query_args', 10, 2 );
    
    #276546

    In reply to: Exclude first post


    Anonymous
    Inactive

    Hi, I’ve a solution for that. You have to use the ‘offset’ parameter.

    I added this to function.php:
    <?php
    function sf_filter_query_args( $query_args, $sfid ) {

    //if search form ID = 225, the do something with this query
    if($sfid==31940)
    {
    //modify $query_args here before returning it
    //only show results within categories with these IDs
    $query_args['offset'] = '1';
    }
    return $query_args;
    }
    add_filter( 'sf_edit_query_args', 'sf_filter_query_args', 10, 2 );
    ?>

    #275584

    Anonymous
    Inactive

    Sure. So I want the results here to display the same as the taxonomy archive page.

    Here is the pre_get_post that results in the taxonomy archive page.

    // Order posts first by event and date, then by other kinds of content  
    function order_events_by_event_date($query=false) {
        
        // only used on main query on front end of site
        if (is_admin() || !$query  ||
            !$query->is_main_query()) {
            return;
        }
        
    
        // modfiy a custom post type to show and order by meta value
        if (is_tax() || is_archive() ) {
            $query->set('orderby', array( 'meta_value' => 'DESC' ) );
            $query->set('order', 'ASC');
            $query->set('posts_per_page', -1);
    
            $meta_query = array(
                'relation' => 'OR',
                    array(
                        'key'     => 'expired',
                        'compare' => 'NOT EXISTS',
                    ),
                    array(
                        'relation' => 'OR',
                        array(
                            'key'   => 'expired',
                            'value' => '0',
                        ),
                        array(
                            'key'     => 'expired',
                            'value'   => '1',
                            'compare' => '!=',
                        ),
                    ),
                );
        
                
            $query->set('meta_query', $meta_query);
            
        }
    } // end function blunt_pre_get_posts
    add_action('pre_get_posts', 'order_events_by_event_date');
    

    The nested array is necessary to include the “experiences” post type (first OR) and the array beneath it includes items that are not expired or field expired is empty.

    I changed it using the code you provided above, removing the is_tax and first qualifiers about queries, and got the results posted above.

    Only thing that seems to semi-sort the results is the sfid filter, so what is displaying results now is

    function exclude_expired_results ( $query_args, $sfid ) {
        
        //if search form ID = 3988, the do something with this query
        if($sfid==3988)
        {
            //modify $query_args here before returning it
           
            $query_args['meta_query'] = array(
                'orderby' => array( 'meta_value' => 'DESC' ),
                'order' => 'ASC',
                'meta_query' => array(
                     'relation' => 'OR',
                    array(
                        'key'     => 'expired',
                        'compare' => 'NOT EXISTS',
                    ),
                    array(
                        'relation' => 'OR',
                        array(
                            'key'   => 'expired',
                            'value' => '0',
                        ),
                        array(
                            'key'     => 'expired',
                            'value'   => '1',
                            'compare' => '!=',
                        ),
                    ),
                ),
                    ); // end meta_query addtion to arg
            }
            
        return $query_args;
    }
    add_filter( 'sf_edit_query_args', 'exclude_expired_results', 20, 2 );  

    sfid is 3988. Thank you for any help.

    #274881

    Anonymous
    Inactive

    Hi there,
    I’m trying to edit the query args to exclude posts with acf field that denotes the “event” post type is “expired” (true/false field). I’ve followed the example in the docs here …. but no dice. Not sure about the ‘somearg’ bit in your example, it is unexplained in the docs, could that be it? Many thanks in advance for having a look.

    
    function exclude_expired_results ( $query_args, $sfid ) {
        
        //if search form ID = 3988, the do something with this query
        if($sfid==3988)
        {
            //modify $query_args here before returning it
            $query_args['somearg'] = array(
                'meta_query' => array(
                'relation' => 'OR',
                    array (
                    'key' => 'expired',
                    'compare' => 'NOT EXISTS'
                    ),
                  array (
                    'key' => 'expired',
                    'value' => '1',
                    'compare' => '!='
                  )
                ) // end meta_query
              ); // end $args
            }
            
        return $query_args;
    }
    add_filter( 'sf_edit_query_args', 'exclude_expired_results', 20, 2 );  
    #271205

    Anonymous
    Inactive

    Hi guys,

    Not the most elegant solution, I suspect, but I have this code correctly pulling in the right values from the slug and parsing them. In the end I opted to filter by the category of the portfolio items as that tends to be more reliable than some of the other meta.

    I’m not sure why, but even though the form is set up to return 42 results, the page only ever displays 10.

    If I add nopaging=>true, to the query I get all results.

    Of course ideally I’d like to load them via ajax as the user scrolls. Do I need to add anything extra to this query, or pass any other variables to achieve that?

    Many thanks!

    The code is here:

    function filter_function_name( $query_args, $sfid ) {
    
    global $post;
    $slug = $post->post_name;
    $sluglist = explode("-", $slug);
    $orientation = $sluglist[0];
    unset($sluglist[0],$sluglist[1],$sluglist[2]);
    $location = implode("-", $sluglist);
    
    if($sfid=="28441"){
    
    $query_args = array(
    'post_type' => 'portfolio',
    'portfolio_category' => "$orientation + $location"
    
    );
    }
    
    return $query_args;
    }
    add_filter( 'sf_edit_query_args', 'filter_function_name', 20, 2 );
    #270938

    Ross
    Keymaster

    Hi Richard

    I’m having a look through this ticket, and if I understand correctly, the first task we need to solve is how to alter the query / restrict things conditionally.

    I think we were on the right path, with using the filter sf_edit_query_args but the implementation doesn’t look right from what I can see.

    So, to explain, our filter, sf_edit_query_args is just a fancy filter for editing query args that get passed into the new WP_Query( $query_args ); we do for our queries (there is some other stuff going on, but that’s the core of it).

    What that means is, the structure of $query_args must match those of a WP_Query – fortunately, something well documented here:
    https://developer.wordpress.org/reference/classes/wp_query/

    What we need to understand first is, what type of data is orientation?

    Is it custom field / post meta? If so, we can follow the instuctions here:
    https://developer.wordpress.org/reference/classes/wp_query/#custom-field-post-meta-parameters

    Assuming your meta key is orientation then we can do something like:

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

    Let me know how you get on with that!

    Best

    #269971

    Anonymous
    Inactive

    Thanks maybe that the thing, but I am not very expert and I don’t know how to use it.

    If I modified the function you suggest and I var_dump the results, I find exactly the list of post I should have, but still the filter doesn’t work

    function filter_function_name( $query_args, $sfid ) {
    	
    	//if search form ID = 225, the do something with this query
    	if($sfid==225)
    	{
    	$posts = get_posts($query_args);
    	$today_year = date('yy');
    	$two_years_ago = $today_year - 2;
    	$current = array();
    
    		foreach( $posts as $post ) {
    			$meta = get_post_meta($post->ID);
    
    				$acf_year = $meta['anno'][0];
    				if($acf_year < $two_years_ago) {
    				$current[] = $post->ID;
    
    		}
    	}
    	$query_args = $current;
    	}
    	
    	return $query_args;
    }
    add_filter( 'sf_edit_query_args', 'filter_function_name', 20, 2 );

    Could you help through it?

    #269535

    In reply to: Elastic Search/Press


    Trevor
    Participant

    1. You code the SFID (which is the ID number of the form), like this (assuming the form ID is 1234, but you will need to change that to match the ID of your form):

    function filter_function_name( $query_args, $sfid ) {
    	if($sfid==1234) {
    		$query_args['ep_integrate'] = true;
    	}
    	return $query_args;
    }
    add_filter( 'sf_edit_query_args', 'filter_function_name', 20, 2 );

    Our function (sf_edit_query_args) passes the sfid itself.

    2. This method works irrespective of the Display Results Method you use in the form.
    3. That is your choice, depending on how you want your site to work, but again this method works irrespective of the Ajax setting in the form.
    4. No need to rebuild the cache.
    5. Not really. There may be a monitor function on your server to show if it is being used.

Viewing 10 results - 1 through 10 (of 123 total)