• Resolved cmehrabian

    (@cmehrabian)


    I’m using the Jobify theme, w/ WP job manager plugin. I’m attempting to modify / add search criteria to the job search widget. I was able to add a drop down, that passes in correct information and outputs the correct queried arguments but its not actually doing any filtering. I could be misinterpreting the example provided by WP Job Manager, and hooking it in incorrectly. Any advice would be much appreciated.

    Thanks in advance!

    WPJM example: http://wpjobmanager.com/document/tutorial-adding-a-salary-field-for-jobs/#section-4
    myGist: https://gist.github.com/cmehrabian/a5d1085061b1e0d17c48

    add_action( 'job_manager_job_filters_search_jobs_end', 'filter_by_school_field' );
    
    function filter_by_school_field() {
    	?>
    	<div class="search_categories">
    		<label for="search_categories"><?php _e( 'Salary', 'wp-job-manager' ); ?></label>
    		<select name="filter_by_school" class="job-manager-filter">
    			<option value=""><?php _e( 'Any School', 'wp-job-manager' ); ?></option>
    			<option value="cabrillo"><?php _e( 'Cabrillo', 'wp-job-manager' ); ?></option>
    			<option value="csumb"><?php _e( 'CSUMB', 'wp-job-manager' ); ?></option>
    			<option value="hartnell"><?php _e( 'Hartnell', 'wp-job-manager' ); ?></option>
    			<option value="miis"><?php _e( 'MIIS', 'wp-job-manager' ); ?></option>
    			<option value="mpc"><?php _e( 'MPC', 'wp-job-manager' ); ?></option>
    			<option value="ucsc"><?php _e( 'UCSC', 'wp-job-manager' ); ?></option>
    		</select>
    	</div>
    	<?php
    }
    /**
     * This code gets your posted field and modifies the job search query
     */
    add_filter( 'job_manager_get_listings', 'filter_by_school_field_query_args', 10, 2 );
    function filter_by_school_field_query_args( $query_args, $args ) {
    	if ( isset( $_POST['form_data'] ) ) {
    		parse_str( $_POST['form_data'], $form_data );
    		if ( ! empty( $form_data['filter_by_school'] ) ) {
    			$selected_range = sanitize_text_field( $form_data['filter_by_school'] );
    			switch ( $selected_range ) {
    				case 'cabrillo' :
    					$query_args['meta_query'][] = array(
    						'key'     => '_affiliated_school',
    						'value'   => 'cabrillo',
    						'compare' => '='
    					);
    				break;
    				case 'csumb' :
    					$query_args['meta_query'][] = array(
    						'key'     => '_affiliated_school',
    						'value'   => 'csumb',
    						'compare' => '='
    					);
    				break;
    				case 'hartnell' :
    					$query_args['meta_query'][] = array(
    						'key'     => '_affiliated_school',
    						'value'   => 'hartnell',
    						'compare' => '='
    					);
    				break;
    				case 'miis' :
    					$query_args['meta_query'][] = array(
    						'key'     => '_affiliated_school',
    						'value'   => 'miis',
    						'compare' => '='
    					);
    				break;
    				case 'mpc' :
    					$query_args['meta_query'][] = array(
    						'key'     => '_affiliated_school',
    						'value'   => 'mpc',
    						'compare' => '='
    					);
    				break;
    				case 'ucsc' :
    					$query_args['meta_query'][] = array(
    						'key'     => '_affiliated_school',
    						'value'   => 'ucsc',
    						'compare' => '='
    					);
    				break;
    				// default :
    				// 	$query_args['meta_query'][] = array(
    				// 		'key'     => 'affiliated_school',
    				// 		'value'   => array_map( 'absint', explode( '-', $selected_range ) ),
    				// 		'compare' => 'BETWEEN',
    				// 		'type'    => 'NUMERIC'
    				// 	);
    				// break;
    			}
    			// This will show the 'reset' link
    			add_filter( 'job_manager_get_listings_custom_filter', '__return_true' );
    		}
    	}
    	return $query_args;
    }

    https://wordpress.org/plugins/wp-job-manager/

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Mike Jolley (a11n)

    (@mikejolley)

    Your gist is working for me..

    Thread Starter cmehrabian

    (@cmehrabian)

    Hey Mike!

    Is it? It’s currently not working on the staging site.

    I’m logging query_args[‘meta_query’] at http://jobs.development.pub. The normal filtering is working but when I make a selection with our custom dropdown, all results disappear. Jobify’s support responded with this:

    The array should include all of the other query arguments too. Looks like you’re overwriting the existing array of arguments instead of appending it.

    How can I append the new search arguments? Any advice would be appreciated!

    Plugin Author Mike Jolley (a11n)

    (@mikejolley)

    Well, when I said working, I meant it gave not results which shows your code is doing ‘something’.

    You could var_dump or log $form_data[‘filter_by_school’] and see what you’re getting back.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Modifying search Criteria’ is closed to new replies.