Support

Account

Forum Replies Created

  • Nobody have a solution to update fields on an options page with CSV ?

  • Well, if it can help somebody, this is my working code :

    // array of filters (field key => field name)
    $GLOBALS['my_query_filters'] = array(
    	'field_5cb6ef1f75209'  => 'alcool',
    	'field_5cb74afb68539'  => 'caracteristiques',
    );
    
    // action
    add_action('pre_get_posts', 'my_pre_get_posts', 10, 1);
    
    function my_pre_get_posts( $query ) {
    
    	// bail early if is in admin
    	if( is_admin() ) return;
    
    	// bail early if not main query
    	// - allows custom code / plugins to continue working
    	//if( !$query->is_main_query() ) return;
    
    	// get meta query
    	$meta_query = $query->get('meta_query');
    
    	// loop over filters
    	$meta_query = array(
    		'relation' => 'AND',
    	);
    	$i = 0;
    	foreach( $GLOBALS['my_query_filters'] as $key => $name ) {
    
    		// continue if not found in url
    		if( empty($_GET[ $name ]) ) {
    			continue;
    		}
    
    		$i++;
    
    		$meta_query[$i] = array(
    			'relation' => 'OR',
    		);
    
    		// get the value for this filter
    		$values = explode( ",", $_GET[ $name ] );
    
    		foreach( $values as $value ) {
    			$meta_query[$i][] = array(
    				'key'      => $name,
    				'value'    => $value,
    				'compare'  => 'LIKE',
    			);
    
    		}
    	}
    	// update meta query
    	$query->set('meta_query', $meta_query);
    	return;
    
    }
    

    Just a question, is there a way to “ajaxify” this ? With no page reload ?

  • No problem. I updated my code :

    $GLOBALS['my_query_filters'] = array(
    	'field_5cb6ef1f75209'  => 'alcool',
    );
    
    // action
    add_action('pre_get_posts', 'my_pre_get_posts', 10, 1);
    
    function my_pre_get_posts( $query ) {
    
    	// bail early if is in admin
    	if( is_admin() ) return;
    
    	// get meta query
    	$meta_query = $query->get('meta_query');
    
    	// loop over filters
    	foreach( $GLOBALS['my_query_filters'] as $key => $name ) {
    
    		// continue if not found in url
    		if( empty($_GET[ $name ]) ) {
    
    			continue;
    
    		}
    
    		// get the value for this filter
    		// eg: http://www.website.com/events?city=melbourne,sydney
    		$value = explode(',', $_GET[ $name ]);
    
    		// append meta query
    		$meta_query = array(
    			array(
    				'key'     => $name,
    				'value'       => $value,
    				'compare'  => 'IN',
    			)
    		);
    
    	}
    
    	// update meta query
    	$query->set('meta_query', $meta_query);
    	return;
    
    }
    

    But it doesn’t work. I don’t have any result when I select a checkbox filter. I think checkbox field is stored as serialized array, so I can not use the IN operator and array (if I select multiple checkbox filters).
    I think I have to create a loop with an OR relation in my meta_query.

  • Well, my log now works. I have this error :
    PHP Fatal error: Uncaught Error: [] operator not supported for strings

    I change my code :

    $meta_query[] = array(
        'key'		=> $name,
        'value'		=> $value,
        'compare'	=> 'LIKE',
    );

    to :

    
    $meta_query = [];
    $meta_query[] = array(
        'key'		=> $name,
        'value'		=> $value,
        'compare'	=> 'LIKE',
    );
    

    Now I have this error in my log :

    PHP Warning: trim() expects parameter 1 to be string, array given in /home/vingtquazk/www/24prod/test/wp-includes/class-wp-meta-query.php on line 613

    I know I can use trim only with a string, but I think I have to use an array…

  • Thank you for your response !
    My debug.log file steal empty. I remove the var_dump, now when I select a filter I have an error 500 page.

Viewing 5 posts - 1 through 5 (of 5 total)