Search Results for 'orderby sf_edit_query_args'

Forums › Forums › Search › Search Results for 'orderby sf_edit_query_args'

Viewing 10 results – 21 through 30 (of 34 total)
  • Author
    Search Results
  • #138398

    Ross
    Keymaster

    Hi there

    Just taking a look at this.

    The reason we implemented the edit_query_args filter is exactly for this reason, the arguments get passed through to our count functions too, so these should be updated, and its why this filter should be used over the WP pre_get_posts…

    So, this is odd.

    2 things I would suggest:

    1) We are only trying to edit the $query_args (more specifically the meta query), not replace all of them (by using = on the variable you replace anything in the array already), perhaps this is causing an issue. I would change your code above to:

    function filter_upcoming_events( $query_args, $sfid ) {
    	if($sfid==29642)
    	{
    		$query_args["meta_query"] = array(
    					"key" => "event_date_time",
    					"value" => time() - (60 * 60 * 2),
    					"compare" => ">="
    				),
    			"meta_key" => "event_date_time",
    			"order" => "ASC",
    			"orderby" => "meta_value",
    		);	
    	}
    	return $query_args;
    }
    add_filter( 'sf_edit_query_args', 'filter_upcoming_events', 20, 2 );

    I would also make sure, that any Post Meta settings, defined in the settings form, are removed / disabled, as well as the order results option in posts tab in your search form (this is all just to be safe and ensure no conflicts).

    2) You could have a pre_get_posts somewhere else in your setup, causing this unusual behaviour, but if using the post meta settings in the form works fine (with the counts) then this may not be the issue. Lets see about option 1 first.

    Let me know how you get on.

    Thanks

    #136903

    Anonymous
    Inactive

    Thanks for that, I’ve got it working now but the count on the filters are wrong. The list of filters is now including all posts rather than the ones that match the new query?

    This wasn’t a problem when I used the Post Meta conditions through the plugin UI

    This is the function I have up to now…

    function filter_upcoming_events( $query_args, $sfid ) {
    	if($sfid==29642)
    	{
    		$query_args = array(
    			"meta_query" => array(
    					"key" => "event_date_time",
    					"value" => time() - (60 * 60 * 2),
    					"compare" => ">="
    				),
    			"meta_key" => "event_date_time",
    			"order" => "ASC",
    			"orderby" => "meta_value",
    		);	
    	}
    	return $query_args;
    }
    add_filter( 'sf_edit_query_args', 'filter_upcoming_events', 20, 2 );

    Page is at http://www.our-pub.co.uk/wp

    As you can see, the England games under the Football filters have already been played so the England filter should be hidden as it’s empty. ticking the England box and submitting the search returns no results.

    Thanks in advance.

    #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?

    #99219

    Anonymous
    Inactive

    Hi Trevor,
    some of my WooCommerce products have a meta key “_tribe_event_starttime” and with your plugin I have setup an AJAX filter ordering these products by that meta key.
    Everything was fine for weeks but today I realized that the order has fallen back to the WC default (creation) “date”. You can see the result list by clicking on one of the big images on the top.

    https://obsthof-am-steinberg.de/produkt-kategorie/event-feste-kinder-musik/

    As you can see the AJAX requests has the GET parameter orderby=date and I tried to alter this order by hooking into your filter which I found here on your website:

    // alter query for search & filter results
    add_filter( ‘sf_edit_query_args’, ‘sf_orderby_eventdate’, 10, 2 );
    function sf_orderby_eventdate ($query_args, $sfid) {
    if ($sfid == 6047) {
    $query_args[“meta_key”] = ‘_tribe_event_starttime’;
    $query_args[“order_by”] = ‘meta_value’;
    $query_args[“order”] = ‘ASC’;
    }
    return $query_args;
    }

    Without success. I have installed WP Super Cache last week. Could this be the reason? Is there anything I missed or doing wrong?

    Thank you for your support!
    Oliver

    Now I am


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

    Anonymous
    Inactive

    Hello,

    I am having some issue with the filtering. I am trying to add an argument that will list my posts in order of date in descending order. Here is what I’ve come up with:

    function filter_function_name( $query_args, $sfid ) {
    	
    	//if search form ID = 225, the do something with this query
    	if($sfid==334)
    	{
    		$query_args = array(
    	'meta_key' => 'webinar_date',
    	'orderby'=> 'meta_value',
    	'order'	=> 'DESC'
            );
    	}
    	
    	return $query_args;
    }
    add_filter( 'sf_edit_query_args', 'filter_function_name', 20, 2 );
    

    But it comes up with no result with the above. Could you please help me with this.

    Thanks in advance

    #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()?

Viewing 10 results – 21 through 30 (of 34 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.