Forums Forums Search Search Results for 'orderby sf_edit_query_args'

Viewing 9 results - 21 through 29 (of 29 total)
  • Author
    Search Results

  • Trevor
    Participant

    Right now, by default the plugin only sorts by the count excluding the children. I wonder if it is possible to modify the query using our Edit Query Arguments filter. It should be, if it can be done in general in WordPress.

    You might want to do a search in this forum for sf_edit_query_args orderby to see if there any code snippets. For example, I found this thread. Not the same question, but it does show another user has used this filter.

    #93355

    In reply to: Meta sort order


    Anonymous
    Inactive

    Might have made some progress. When I change the priority I now get no results instead of results in the wrong order. Here’s a code sample:

    function filter_sort_by_start_date( $query_args, $sfid ) {
    
    if( $sfid==2298 ) {
        $query_args = array(
          'meta_key' => 'class_date',
          'orderby' => 'meta_value_num',
          'order' => 'DESC'
        );
    }
    
    	return $query_args;
    }
    add_filter( 'sf_edit_query_args', 'filter_sort_by_start_date', 2000, 2 );

    I’m getting the “no result” result even if I comment out the other ‘pre_get_posts’ calls.

    #90518

    In reply to: Post Meta Date Range


    Ross
    Keymaster

    Hi Max

    I had a look, it seems to me there is something wrong with your code sample.

    The filter receives a $query_args object, which contains the wp_query args that S&F defines, and at the end of the function it needs to return this $query_args object.

    Inside the function you are free to modify this as you want, which it looks like you got close to do doing but not quite there..

    You add your meta query to a new variable called $args, which never gets used.

    I would do it like the following (untested – please also copy & paste into editor to see my comments, this is a bit clunky):

    <?php
    function filter_function_name( $query_args, $sfid ) {
    
    	if( $sfid == 940 ) {
    
    		// I would use some known values for testing (I just made some dates up here)
    		$tender_start = "20170101";
    		$tender_end = "20170101";
    		
    		/*global $searchandfilter;
    		$sf_current_query = $searchandfilter->get(940)->current_query();
    		$array_data = $sf_current_query->get_array();
    		$tender_start = $array_data['_sfm_tender_start']['active_terms'][0]['value'];
    		$tender_end = $array_data['_sfm_tender_end']['active_terms'][0]['value'];*/
    		
    		//if we want to play nice and not completely replace the existing meta query args 
    		//there might be, then we need to check if some meta queries already exist and treat it differently
    		$meta_query = array(
    			'relation' => 'AND',
    			array(
    				'key'	  => 'tender_start',
    				'compare' => '>=',
    				'value'	  => $tender_start
    			),
    			array(
    				'key'	  => 'tender_end',
    				'compare' => '<=',
    				'value'   => $tender_end
    			)
    		);
    		
    		if((isset($query_args['meta_query']))&&(is_array($query_args['meta_query'])))
    		{//this means there are some meta queries already here, so push this one onto the end
    			array_push($query_args['meta_query'], $meta_query);
    		}
    		else
    		{//else there aren't any meta queries
    			//$query_args['meta_query'] should always be an array of meta queries according to wp docs
    			$query_args['meta_query'] = array($meta_query);
    		}
    		
    		// not sure if you need these, you should set the order via the S&F admin (in the <code>posts</code> tab)
    		$query_args['meta_key'] = 'tender_start';
    		$query_args['orderby'] = 'meta_value_num';
    		$query_args['order'] = 'ASC';
    		
    	}
    	
    	return $query_args;
    }
    add_filter( 'sf_edit_query_args', 'filter_function_name', 20, 2 );

    I’ve written that in my editor so it should be correct syntax, but I haven’t actually tested it, its just showing you how to properly use the $query_args to make the changes you want.

    Hope that helps

    #89576

    In reply to: Post Meta Date Range


    Anonymous
    Inactive

    Thank for your answer. Do you have a code sample? I tried some but no result.

    function filter_function_name( $query_args, $sfid ) {
    
    	if( $sfid == 940 ) {
    
    		global $searchandfilter;
    		$sf_current_query = $searchandfilter->get(940)->current_query();
    
    		$array_data = $sf_current_query->get_array();
    		$tender_start = $array_data['_sfm_tender_start']['active_terms'][0]['value'];
    		$tender_end = $array_data['_sfm_tender_end']['active_terms'][0]['value'];
    
    		$args = array(
    			'meta_key'       => 'tender_start',
    			'orderby'        => 'meta_value_num',
    			'order'          => 'ASC',
    			'meta_query'     => array(
    				'relation' => 'AND',
    				array(
    					'key'	  => 'tender_start',
    					'compare' => '>=',
    					'value'	  => $tender_start
    				),
    				array(
    					'key'	  => 'tender_end',
    					'compare' => '<=',
    					'value'   => $tender_end
    				)
    			)
    		);
    	}
    	
    	return $query_args;
    }
    add_filter( 'sf_edit_query_args', 'filter_function_name', 20, 2 );
    #79470

    Anonymous
    Inactive

    I’ve started to work on the code by beginning with showing only the featured therapists.
    I’ve got the code

    add_filter( 'sf_edit_query_args', 'filter_featured_first', 20, 2 );
    function filter_featured_first( $query_args, $sfid ) {
    if($sfid==491)
    	{ $query_args = array('meta_query' => array(
            'relation' => 'AND', array('key' => 'featured', 'value' => 1 )       
            ), 'posts_per_page'   => 4
            );}	return $query_args;}

    However, I can’t get the pagination to work. The same posts are being shown on every page.
    How do I fix the pagination? And how do I get it to work with the above function edit_posts_orderby()?

    #79403

    Anonymous
    Inactive

    Thanks so much for your help.
    What do you mean by a ‘sort field’? Do you mean in the Search Form UI?
    I’m using ‘post meta’ and ‘search’ fields. You can see the filter on http://gethelpisrael.mgtestsite.com/therapist/?_sfm_medical_professional_type=Therapist So would I be ok then?
    Also, how would I add the code for the randomisation with the pagination?
    Could I leave the filter: add_filter(‘posts_orderby’, ‘edit_posts_orderby’);
    and add add_filter( ‘sf_edit_query_args’, ‘filter_function_name’, 20, 2 ); with my custom code and they would both work?
    Or do I need to combine them into one function?

    #70909

    Anonymous
    Inactive

    function sf_filter_query_args( $query_args, $sfid ) {

    if($sfid==1225){

    global $searchandfilter;
    $sf_current_query = $searchandfilter->get(1225)->current_query();

    $sf_current_query->get_field_string(“_sfm_event_archives”);
    $_sfm_event_archives_array = $sf_current_query->get_array();
    $archive_is_check = $_sfm_event_archives_array[_sfm_event_archives][active_terms][0][value];

    //print_r($archive_is_check);

    if($archive_is_check != ‘1’){
    $args_custom = array(
    “meta_key” => “imic_event_start_dt”,
    “orderby” => “meta_value_num”,
    “order” => “ASC”,

    “meta_query” => Array (
    “event_archives” => Array (
    “key” => “event_archives”,
    “value” => “0”
    )
    )
    );

    $query_args = array_merge($query_args, $args_custom);
    }

    // si aucun filtre d’appliqué
    //if($sf_current_query->is_filtered()!=1){}

    }

    return $query_args;

    }
    add_filter( ‘sf_edit_query_args’, ‘sf_filter_query_args’, 100, 2 );

    #70903

    Anonymous
    Inactive

    Hi,
    thanks for your help!!!
    It wasn’t the wpml plugin,
    but the custom code (that I was sure it wasn’t in cause)
    I had to add the bold section here to make it work

    
    function sf_filter_query_args( $query_args, $sfid ) {
    
      if($sfid==1225){
    	
    	global $searchandfilter;
    	$sf_current_query = $searchandfilter->get(1225)->current_query();
    	
    	
    	$sf_current_query->get_field_string("_sfm_event_archives");
    	$_sfm_event_archives_array = $sf_current_query->get_array();
    	$archive_is_check = $_sfm_event_archives_array[_sfm_event_archives][active_terms][0][value];
    	
    	//print_r($archive_is_check);
    	
    	if($archive_is_check != '1'){
    		$args_custom = array(
    			<strong>"meta_key" => "imic_event_start_dt",
    			"orderby" => "meta_value_num",
    			"order" => "ASC",</strong>
    			"meta_query" => Array ( 
    		  		"event_archives" => Array ( 
    					"key" => "event_archives",
    					"value" => "0"
    				)
    			)
        	);
    		
    		$query_args = array_merge($query_args, $args_custom);
    	}
    	
    	// si aucun filtre d'appliqué
    	//if($sf_current_query->is_filtered()!=1){}
    
      }
      
      return $query_args;
    
    }
    add_filter( 'sf_edit_query_args', 'sf_filter_query_args', 100, 2 );
    

    Thanks again for your help guys!
    Really nice plugin!

    #39398

    Ross
    Keymaster

    Hey Paul

    You don’t need to do this via query args – you can do all this via the posts tab.

    If it must be dynamic and via the query args, then its the same as when using the standard new WP_Query($args)

    https://www.designsandcode.com/documentation/search-filter-pro/action-filter-reference/#Edit_Query_Arguments

    So, to modify orderby you need this part of the WP docs:

    https://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters

    Updating the S&F filter example with the example from the WP site on order:

    $args = array(
    	'orderby' => 'title',
    	'order'   => 'DESC'
    );
    $query = new WP_Query( $args );

    Will update the filter like:

    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['orderby'] = 'title';
    		$query_args['order'] = 'DESC';
    		
    	}
    	
    	return $query_args;
    }
    add_filter( 'sf_edit_query_args', 'filter_function_name', 10, 2 );

    Thanks

Viewing 9 results - 21 through 29 (of 29 total)