Support Forums

The forums are closed and will be removed when we launch our new site.

Looking for support? You can access the support system via your account.

Forums Forums Search Search Results for 'function sf_edit_query_args'

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

    Pau
    Participant

    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

    Andrea Fischer
    Participant

    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


    Pou Lala
    Participant

    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


    Pou Lala
    Participant

    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

    Ariane Weisel
    Participant

    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.

    #275335

    Ariane Weisel
    Participant
    This reply has been marked as private.
    #274881

    Ariane Weisel
    Participant

    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 );  

    Samuel Schneider
    Participant
    This reply has been marked as private.
    #272540

    Trevor
    Moderator
    This reply has been marked as private.
    #272324

    Richard Hartley
    Participant
    This reply has been marked as private.
Viewing 10 results - 1 through 10 (of 148 total)