• Resolved zahedkamal87

    (@zahedkamal87)


    Hi, I followed this tutorial – https://wpjobmanager.com/document/tutorial-creating-custom-job-search-form/

    Though, i’ve created a shortcode for it. See the function below, search_categories not working at all. Please visit the link (Link to the page you need help with:), you can test the form.

    
    function ed_job_search_form($atts, $content = null) {
    	global $wpdb; 
    	$atts = shortcode_atts(
    		array(
    			'id' => '',
    			'class' => '',
    		),
    		$atts
    	);
    
    	$job_locations = $wpdb->get_results("SELECT DISTINCT meta_value FROM $wpdb->postmeta pm, $wpdb->posts p WHERE meta_key  = '_job_location' and pm.post_id=p.ID  and p.post_type='job_listing' ORDER BY pm.meta_value ",ARRAY_A);
    
    	$html = '';
    
    	$search_keywords = isset($_GET['search_keywords']) ? $_GET['search_keywords'] : '';
    	$search_location = isset($_GET['search_location']) ? $_GET['search_location'] : '';
    	$search_categories = isset($_GET['search_categories']) ? $_GET['search_categories'] : '';
    
    	$html .= '<form method="GET" action="' . job_manager_get_permalink('jobs') . '" class="job-search-form">';
    		$html .= '<div class="row">';
    		    $html .= '<div class="col-xl-3">';
    		        $html .= '<input type="text" id="search_keywords" name="search_keywords" class="form-control" placeholder="Keywords" value="' . $search_keywords . '"  />';
    		    $html .= '</div>';
    		    $html .= '<div class="col-xl-3">';
    		        $html .= '<select id="search_location" name="search_location" class="form-control">';
    		        	$html .= '<option value="">Location</option>';
    			  		foreach ( $job_locations as $job_location ) {
    			  			$html .= '<option value="' . esc_attr( $job_location['meta_value'] ) . '" ' . ($search_location == $job_location['meta_value'] ? 'selected' : '') . '>' .esc_html( $job_location['meta_value'] ) .'</option>';
    			  		}
    		  		$html .= '</select>';
    		    $html .= '</div>';
    		    $html .= '<div class="col-xl-3">';
    		        $html .= '<select name="search_categories" id="search_categories" class="form-control">';
    		        	$html .= '<option value="">Category</option>';
    			  		foreach ( get_job_listing_categories() as $cat ) {
    			  			$html .= '<option value="' . esc_attr( $cat->term_id ) . '" ' . ($search_categories == $cat->term_id ? 'selected' : '') . '>' .esc_html( $cat->name ) .'</option>';
    			  		}
    		  		$html .= '</select>';
    		    $html .= '</div>';
    		    $html .= '<div class="col-xl-3">';
    		        $html .= '<button type="submit" class="btn btn-hexagon">';
    		        	$html .= '<span class="btn-hexagon-label">Search <i class="fas fa-search"></i></span>';
    		        $html .= '</button>';
    		    $html .= '</div>';
    		$html .= '</div>';
    	$html .= '</form>';
    
        return $html;
    }
    
    add_shortcode( 'ed-job-search-form', 'ed_job_search_form' );
    

    Thank you

    The page I need help with: [log in to see the link]

Viewing 11 replies - 1 through 11 (of 11 total)
  • The search categories seem to be working correctly for me! If you found something in the code that you fixed, could you post the fix here for everyone to learn from?

    Plugin Contributor Richard Archambault

    (@richardmtl)

    Hi! Have you resolved this?

    Thread Starter zahedkamal87

    (@zahedkamal87)

    No..
    When i had the search form with category dropdown name “search_categories”. It didnt work at all. So i changed it to “search_category”. And have these codes –

    
    add_filter( 'job_manager_get_listings', 'add_job_search_by_categories', 10, 2 );
    function add_job_search_by_categories ($query_args, $args) {
        if ( ! empty( $_GET['search_category'] ) ) {
            $categories = is_array( $_GET['search_category'] ) ? $_GET['search_category'] : array_filter( array_map( 'trim', explode( ',', $_GET['search_category'] ) ) );
            $field      = is_numeric( $categories[0] ) ? 'term_id' : 'slug';
            $operator   = 'all' === get_option( 'job_manager_category_filter_type', 'all' ) && sizeof( $categories ) > 1 ? 'AND' : 'IN';
            $category_tax = array(
                'taxonomy'         => 'job_listing_category',
                'field'            => $field,
                'terms'            => array_values( $categories ),
                'include_children' => $operator !== 'AND' ,
                'operator'         => $operator
            );
    
            $query_args['tax_query']['relation'] = 'AND';
    	    $query_args['tax_query'][] = $category_tax;
    
        }
        return $query_args;
    }
    

    Also tried to get AJAX to work by doing this, but it doesnt work at all –

    
    add_filter( 'job_manager_get_listings_args', 'add_job_search_by_categories_ajax', 10, 1 );
    function add_job_search_by_categories_ajax ($query_args) {
    	$queries = array();
    	parse_str($_SERVER['QUERY_STRING'], $queries);
    
        if ( isset($queries['search_category']) ) {
            $query_args['search_categories'] = [$queries['search_category']];
        }
        
        return $query_args;
    }
    

    visit here – https://www.portmanrecruitment.com
    I added a search form at top of every page, and job page is located at – https://www.portmanrecruitment.com/jobs.

    Suppose, if i search jobs on category “Adults”, results shows jobs under “Adults” category. But when you click on “Load more listings”, it loads jobs from all categories! Keywords and Location works fine.

    Thank you..

    Plugin Contributor Richard Archambault

    (@richardmtl)

    Sorry, but we aren’t able to help with custom code requests like this, as per our support policy:

    https://wpjobmanager.com/support-policy/

    The tutorials are there to help you, but if you are doing custom things on top of the tutorials, I can’t provide further assistance for it. You may want to look into one of these services for help:

    https://codeable.io/

    https://studio.envato.com/

    https://jobs.wordpress.net/

    Thanks!

    Thread Starter zahedkamal87

    (@zahedkamal87)

    Its fine if you cannot help. But i guess its within support (Free) to tell how can i manipulate ajax part?

    Thread Starter zahedkamal87

    (@zahedkamal87)

    I tried this – jobs/?search_keywords=experience&search_location=&search_categories[]=9

    I get jobs where “experience” is found. But not “category” doesnt work.

    “If the URL of the page contains a query string and location or keywords is set, the values in the query string will populate the location and keywords fields instead of the above options.”
    — Copied from here – https://wpjobmanager.com/document/shortcode-reference/

    Does that mean, only Keywords and Location get read from URL, but not Categories (search_categories)?

    Hope this is not some kinda support that require “paid” service.

    Thanks

    For preselecting the category on the [jobs] shortcode, use the search_category query string variable (not search_categories; which is what the shortcode JS ajax-filters.js sends to the ajax request handler). Right now it only accepts one term ID.

    For example:
    https://example.com/jobs?search_category=6

    There is an issue to expand on that to allow for [multiple] term slugs here:
    https://github.com/Automattic/WP-Job-Manager/issues/1300

    Note that once categories are changed in the client, the client’s state is preserved during that session and overrides what might come from the query string. Opening in a new tab should clear that.

    Jake

    Thread Starter zahedkamal87

    (@zahedkamal87)

    @jakeom thanks for the response.
    As its a kinda problem that After adding a Category, refreshing the page, doesnt remove it/them, is there any JS way to remove them? within the plugin i mean.

    Thank you

    Hi Zahed, it seems to be working on the clients site. Did you resolve it? If so, what did you do.

    • This reply was modified 5 years, 5 months ago by ebmp.

    @zahedkamal87 There isn’t right now, but I’ve proposed a possible fix for the next major version:
    https://github.com/Automattic/WP-Job-Manager/pull/1805

    Issue to follow:
    https://github.com/Automattic/WP-Job-Manager/issues/1804

    Jake

    Plugin Contributor Richard Archambault

    (@richardmtl)

    I’m marking this thread as Resolved as it’s been more than a month since the last reply. If you still need help, please do reply again and mark the thread as Unresolved!

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Custom job search form with Category not working properly’ is closed to new replies.