• Resolved lancha131

    (@lancha131)


    Hello!
    I added custom taxonomy which is called job industry.
    i added also dropdown selection menu to job filtering.
    Now, i suppose that i need to include job industry to search crtieris, because it doesnt work.
    Suppose that need to add that to ajax.
    Can somebody can give me hint where(in which php file and how)

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

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

    (@mikejolley)

    Your new field will be ‘posted’ along with the other fields, but to change the results you’ll need to filter on this:

    https://github.com/Automattic/WP-Job-Manager/blob/master/wp-job-manager-functions.php#L107

    and look for your posted value, and then manipulate the query.

    Thread Starter lancha131

    (@lancha131)

    Ok, can you explain me “filter on this”?
    Maybe on some example, please!
    Appreciate it!

    Plugin Author Mike Jolley (a11n)

    (@mikejolley)

    Thread Starter lancha131

    (@lancha131)

    Ok, at first :
    in job-filters.php i first need to add a field
    I added row (need industry sector filter)

    <?php job_manager_dropdown_categories( array( 'taxonomy' => 'industry_sectors', 'hierarchical' => 1,'name' => 'search_categories', 'orderby' => 'name', 'selected' => $selected_category, 'hide_empty' => false ) ); ?>

    I don’t know is this right way, but no idea how to do that in other way.
    problem is here : ‘name’ => ‘search_categories’ – if i change this to anything else, when open filter page no dropdown with choice selection, just multiple dropdown, and when i clicked nothing happens.

    Secondly, i added this to wp_job_manager_functions.php file:

    if ( ! empty( $args['industry_sectors'] ) ) {
    		$query_args['tax_query'][] = array(
    			'taxonomy' => 'industry_sectors',
    			'field'    => 'slug',
    			'terms'    => $args['industry_sectors']
    		);
    	}

    is it correct step? i know what and how filter is doing job, and i think i only need to add this before filter.

    Plugin Author Mike Jolley (a11n)

    (@mikejolley)

    You need to name it unique – don’t use search_categories.

    I’ve written a simple example showing how it can be done. You’ll need this change in core if you use this method as I added handling for a new field class: https://github.com/Automattic/WP-Job-Manager/commit/49d84487eb51f168c26f806083c537e54a33c05f

    https://wpjobmanager.com/document/tutorial-adding-a-salary-field-for-jobs/#section-4

    Thanks for the tutorial, Mike!

    Have you had any luck with getting the taxonomy to work, @lancha131 ? Ideally without modifying any core files? Using Mike’s tutorial, I can make it work with fields I add as post_meta, but I’m having trouble adapting that to a custom taxonomy.

    I need this too. A lot! If you guys find anything, please respond here 🙂

    I’ve modified Mike’s tutorial to get it to work with taxononmy. In this example, my custom taxonomy is called class_format

    //Add the search filter for custom tax
    
    add_action( 'job_manager_job_filters_search_jobs_end', 'filter_by_salary_field' );
    function filter_by_salary_field() {
    ?>
    <div class="search_categories">
        <select style="display:block;" name="filter_by_class_format" class="job-manager-filter">
            <option value=""><?php _e( 'Any Class Format', 'wp-job-manager' ); ?></option>
            <?php
                         //Get the terms for class format
                                       $terms = get_terms( 'class_format', array(
                                           'hide_empty' => 0,
                                       ) );
                                       foreach ($terms as $term){
                                           echo'<option value="'.$term->slug.'">'.$term->name.'</option>';
                                       }
                                       ?>
     </select>
    </div>
    <?php
     }//end function
    
    /**
     * This code gets your posted field and modifies the job search query
     */
    add_filter( 'job_manager_get_listings', 'filter_by_class_format_query_args', 10, 2 );
    function filter_by_class_format_query_args( $query_args, $args ) {
        if ( isset( $_POST['form_data'] ) ) {
            parse_str( $_POST['form_data'], $form_data );
            // If this is set, we are filtering by salary
            if ( ! empty( $form_data['filter_by_class_format'] ) ) {
                $classformat = sanitize_text_field( $form_data['filter_by_class_format'] );
                        $query_args['tax_query'][] = array(
                            'taxonomy' => 'class_format',
                            'field'    => 'slug',
                            'terms'    => $classformat,
                        );
    
                // This will show the 'reset' link
                add_filter( 'job_manager_get_listings_custom_filter', '__return_true' );
            }
        }
        return $query_args;
    }

    I think @lukelukeluke is very helpful, i hope you can help me too.
    My industryes are showed properly in my html markup, but i couldn’t figure out the search filter, I got this code:

    add_action( 'job_manager_job_filters_search_jobs_end', 'industry_field_field' );
    add_filter( 'job_manager_get_listings', 'filter_by_industry_args', 10, 2 );
    
    function industry_field_field(){
    	?><label for="search_industry">Industry: </label>
            <select class="form-control adv_frm_style_s" id="search_industry" name="search_industry">
            <option value=”” disabled selected>Select</option>
              <?php foreach ( get_job_listing_industries() as $ind ) : ?>
                  <option value="<?php echo esc_attr( $ind->term_id ); ?>" ><?php echo esc_html( $ind->name ); ?></option>
                <?php endforeach; ?>
            </select>
    
          <script>
    		jQuery( document ).ready( function( $ ) {
    
    			$( '#search_industry' )
    				.change( function() { $('#search_keywords').change(); } )
    				.keyup( function( e ) {
    					13 === e.which && $(this).trigger("change")
    				} )
    			;
    		} );
    	</script>
    		<?php
    }//end function
    
    function filter_by_industry_args( $query_args, $args ) {
    	if ( isset( $_POST['form_data'] ) ) {
    		parse_str( $_POST['form_data'], $form_data );
    
    		if ( ! empty( $args['search_industry'] ) ) {
    			$search_industry = sanitize_text_field( $form_data['search_industry'] );
                    $query_args['tax_query'][] = array(
                        'taxonomy' => 'job_industry',
                        'field'    => 'term_id',
                        'terms'    => $search_industry,
                    );
    
    			// $field    = is_numeric( $args['search_industry'][0] ) ? 'term_id' : 'slug';
    			// $query_args['tax_query'][] = array(
    			// 	'taxonomy'         => 'job_industry',
    			// 	'field'            => $field,
    			// 	'terms'            => array_values( $args['search_industry'] ),
    			// );
    		}
    	}
    	return $query_args;
    }

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘How to include custom taxonomy into job search filtering’ is closed to new replies.