Support » Plugins » Hacks » Searching for a single instance of a value in a string

  • So I’ve set up a custom meta value for a plugin WordPress Job Manager however I think any one knowledgeable in WordPress / PHP could help me.

    The field I set up is for language and it works well when only 1 language is entered into the field however the dropdown menu I set up to search for languages can’t handle it when any more then one language is entered into it.

    The drop down is set up like this :

    add_action( 'job_manager_job_filters_search_jobs_end', 'filter_by_language_field' );
    function filter_by_language_field() {
      ?>
      <div class="search_language">
        <label for="search_language"><?php _e( 'Language', 'wp-job-manager' ); ?></label>
        <select name="filter_by_language" class="job-manager-filter">
         <option value="">All languages</option>
         <option value="french"><?php _e( 'French', '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_language_field_query_args', 10, 2 );
    function filter_by_language_field_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_language'] ) ) {
          $selected_range = esc_html( $form_data['filter_by_language'] );
          switch ( $selected_range ) {
            case 'french' :
              $query_args['meta_query'][] = array(
                'key'     => '_job_language',
                'value'   => 'French',
                'compare' => '=',
                'type'    => 'CHAR'
              );
            break;
          }      
          // This will show the 'reset' link
          add_filter( 'job_manager_get_listings_custom_filter', '__return_true' );
        }
      }
      return $query_args;
    }

    So as I said earlier this format works great for when a single language is entered into the custom field e.g. French however if multple are entered in e.g. French, German, Italian, Polish, Spanish it wont detect that french is there.

    I’ve tried using various compare operators but can’t seem to get it quite right.

    Any help is appreciated

Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    I moved your topic to the Hacks forum. It’s the best place for custom coding issues.

    Have you considered a multiple select dropdown? Provide a list of languages and users can select any number of them. Add the “multiple” attribute to the select tag and add [] to the tag’s name. IMO this is preferable because you don’t have to worry about odd user input like foreign terms(Französisch), misspellings and unusual (or no) separation characters.

    To determine if “French” occurs in a free form user supplied string, you could use stripos()
    http://php.net/manual/en/function.stripos.php

Viewing 1 replies (of 1 total)
  • The topic ‘Searching for a single instance of a value in a string’ is closed to new replies.