Forums Forums Search Search Results for 'function sf_edit_query_args'

Viewing 10 results - 81 through 90 (of 123 total)
  • Author
    Search Results
  • #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?

    #134719

    In reply to: user specific results


    Anonymous
    Inactive

    Hello Trevor,

    I’ve tried

    function filter_function_name( $query_args, $sfid ) {
        
        $cat_id = array(1);
    	
    	//if search form ID = 225, the do something with this query
    	if($sfid==32)
        {
            $query_args = array(
    		 'category__in' => array($cat_id),
                );
    	}
    	
    	return $query_args;
    }
    add_filter( 'sf_edit_query_args', 'filter_function_name', 20, 2 );
    

    Is that right? It returns no results – seems not to extend the query ?

    Best
    P.

    #131892

    Trevor
    Participant

    You would need to be running a child theme, and inside the child theme folder on your server would be a file called functions.php

    Edit that and add the sample code on new lines in the file.

    There are examples of using the code in this forum, using this search:

    https://support.searchandfilter.com/forums/search/sf_edit_query_args/

    But I am unable to provide help with the coding. For that you would need to hire a coder.

    #131208

    Anonymous
    Inactive

    I still think it’s me who is making the strange question 😉
    I hope I will give this one a last try before I have to give up, since I cannot help my customer a step further.

    I will give you a step by step list of what I have done and want to have:

    1. I just have a front end search form in which the customer uses two fields to insert the ‘height’ and the ‘width’ of something

    2. I also wnat to use S&F to search/filter on a third field (which does not need to be filled in by the customer, since that field should be the calculation of height x width. It is kind of unlogical to let the user fill in the calculated value of something so simple. Right?

    3. So I don’t need to see all the fields on the screen, I just need to get access to the two fields I mentioned. I found the names of them: _sfm_breedte (width) and _sfm_hoogte (height)

    4. I thought I could use the ‘Edit Query Arguments’ routine, and use it for the ID of my S&F serach (in my case 3954). I thought I could get the field values in that routine and make the needed calculation of the third field value there.

    5. Yes and that field might be made invisible in the front end, since the user does not need to see it. So you in ACF Pro I use the three fields, but only two shown in the search form. Ik hope that makes the appraoch for my request more clear.

    Questions:

    1. Is the routine I want to use the right one?
    2. So, how do I access and get the values of the two defined fields? I don’t need to dispaly them, just use them to make a calculation

    Example of non-PHP code in written words:

    function filter_function_name( $query_args, $sfid ) {

    //if search form ID = xxxx, the do something with this query
    if($sfid==xxxx)
    {
    get me the value of ‘height’

    }

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

    #127591

    Anonymous
    Inactive

    Yes it is, the code is as following:

    function sf_filter_query_args($query_args, $sfid) {
        echo '<pre>',print_r($query_args,true),'</pre>';
    
        if ($sfid == 327) {
            add_filter('posts_where', 'acf_posts_where_likes');
    
            $query_args['meta_query'] = array(
                'relation' => 'AND',
                array(
                    'key' => 'likes_%_gebruiker',
                    'value' => '16',
                    'compare' => 'LIKE'
                )
            );
        }
    
        echo '<pre>',print_r($query_args,true),'</pre>';
    
        return $query_args;
    }
    
    add_filter('sf_edit_query_args', 'sf_filter_query_args', 10, 400);

    Anonymous
    Inactive

    Hello guys!

    I have a custom sf_filter_query_args like so:

    function sf_filter_query_args($query_args, $sfid) {
        if ($sfid == 327) {
            add_filter('posts_where', 'acf_posts_where');
    
            $query_args['meta_query'] = array(
                'relation' => 'AND',
                array(
                    'key' => 'likes_%_gebruiker',
                    'value' => '16',
                    'compare' => 'LIKE'
                )
            );
        }
    
        return $query_args;
    }
    
    add_filter('sf_edit_query_args', 'sf_filter_query_args', 10, 2);

    Which works perfectly when I come on the page. It filters succesfully on the meta_query. But when I select a filter it returns all result and not the results where likes_%_gebruiker is 16 + the filters.

    Am i doing something wrong? Can someone help me out?

    Thanks

    #127343

    Anonymous
    Inactive

    It was actually firing too early. Still I can’t seem to add the post to the results.
    Here is my code:

    function people_filter( $query_args, $sfid ) {
       if($sfid == 7287)
        {
        	if ($_GET["_sfm_people"] != "") {
        		array_push($query_args['post__in'], intval($_GET["_sfm_people"]));
        		echo '<pre>',print_r($query_args,true),'</pre>';
      		}
        }
        return $query_args;
    }
    add_filter( 'sf_edit_query_args', 'people_filter', 20, 2 );

    And here is what the print_r is echoing:

    Array
    (
        [paged] => 1
        [search_filter_id] => 7238
        [search_filter_override] => 
        [posts_per_page] => -1
        [post_status] => Array
            (
                [0] => publish
            )
    
        [meta_query] => Array
            (
            )
    
        [post_type] => Array
            (
                [0] => lesson
                [1] => people
                [2] => video
                [3] => image
                [4] => article
            )
    
        [is_search] => 1
        [post__in] => 
    )
    #127340

    Anonymous
    Inactive

    Hmm, I’m getting an empty array. My code:

    function people_filter( $query_args, $sfid ) {
        if($sfid == 7287)
        {
          		echo '<pre>',print_r($query_args,true),'</pre>';
        }
        return $query_args;
    }
    add_filter( 'sf_edit_query_args', 'people_filter', 10, 200 );

    It’s strange because the archive page is displaying results, maybe it’s firing too late?

    #127149

    Anonymous
    Inactive

    I have a filter set up to find all posts with a relationship to an ACF called “people”, it returns all results with a relationship to a “person” post but I would also like to return the “person” post itself in the search results.

    What is the easiest way to include it in my results?
    I have tried something like this:

    function people_filter( $query_args, $sfid ) {
        if($sfid == 7287)
        {
        	if ($_GET["_sfm_people"] != "") {
          		$query_args['p'] = intval($_GET["_sfm_people"]);
      	}
        }
        return $query_args;
    }
    add_filter( 'sf_edit_query_args', 'people_filter', 10, 200 );

    But this returns the “person” post only, I would instead like to add it to the already compiled results.

    #125885

    Trevor
    Participant

    The first thing to do is, in the code, before you make a change, use the PHP print_r function to output the query array to the screen, and that, after you modify it, do the same again, and compare the arrays. Like this:

    echo '<pre>',print_r($sf_current_query,true),'</pre>';

    I have not actually used the sf_edit_query_args function myself.

Viewing 10 results - 81 through 90 (of 123 total)