Filtering Job Posts

The Bonsy Recman WP plugin has a built-in function to filter job posts.

For a fully working example, se the bottom of this page.

By defining a GET query on your job post listing page, with a valid field as the parameter, the page limits the result to job posts containing the given value.

For example, if given a workplace=oslo query, the result is limited to job posts containing the word 'Oslo' in the workplace field.

To fully make use of this functionality, we have created some helper functions. Learn more about these below.

Important!

When adding filters, there will be almost an infinite number of possible URL paths. When the search engines are crawling your site, they might end up crawling an aweful amount of pages because of this.

This may lead to a overload on your server.

Please make sure the search engines are not crawling your jobpost listing. So if you list your job posts on yourdomain.com/jobs/ please add this to your robots.txt file:

Disallow: jobs/*

The content of this page:

  • Display job post count

  • Display total position count

  • Implement freetext search

  • Create a link to filter job post feed

  • Create a filters for a given field for the job post feed.

get_jobpost_count()

Get the count of all published job posts, or get the count based on the current active filter. Learn more about how to filter job posts below.

Parameters

get_jobpost_count( $filtered );
  • $filtered {boolean} (optional) Will return count of all job posts if false, default is set to true and will return the count of the current active filter.

Example

This example will output something like "62 job posts are available. Showing: 34".

<?php

$jobs_total = get_jobpost_count( false );
$jobs_displayed = get_jobpost_count(); # defaults to true

echo "$jobs_total job posts are available. Showing: $jobs_displayed";

get_jobpost_position_count()

Get the count of all positions available, or get the position count based on the current active filter.

Parameters

get_jobpost_position_count( $filtered );
  • $filtered {boolean} (optional) Will return count of all job post positions if false, default is set to true and will return the position count of the current filter.

Example

<?php

$pos_total = get_jobpost_position_count( false );
$pos_displayed = get_jobpost_position_count();

echo "$pos_total positions are available. Showing: $pos_displayed";
<form method="get">
    <label for"jobSearch">Search</label>
    <input 
        type="text" 
        value="<?php echo $_GET['jobSearch'] ?? '';?>"
        name="jobSearch"
        id="jobSearch"
     />
    <button type="submit">Search</button>
</form>

get_jobpost_filter_url()

Filtering job posts are done through GET queries.

By defining a GET query on your job post feed page, with a valid field as the parameter, the page limits the result to job posts containing the given value.

For example, if given a workplace=oslo query, the result is limited to job posts containing the word 'Oslo' in the workplace field.

You may want to support the use of multiple filters and the option for user to turn of filters on the job post feed page. In that case, this function can be quite handy.

Parameters

get_jobpost_filter_url( $field, $value );
  • $field {string} {required}. The field name to filter.

  • $value {string} {required}. The value to filter on or to remove from current filter.

Example

Here is an example on how to show a link that will only show job posts with location that contains the word Oslo.

<?php 
    $url = get_jobpost_filter_url('workplace', 'Oslo');
    echo "<a href='$url'>Oslo</a>";
?>

If the job post feed is already filtered with showing location = oslo, the same link will remove this filter.

This helper function is quite handy in combination with the get_jobpost_filters() function.

get_jobpost_filter_count()

Similar to get_jobpost_filter_url() but instead of generating a URL it shows the number of job posts with this filter.

Parameters

get_jobpost_filter_count( $field, $value );
  • $field {string} {required}. The field name to filter.

  • $value {string} {required}. The value to count.

Example

<?php 
    $url = get_jobpost_filter_url('workplace', 'Oslo');
    $count = get_jobpost_filter_count('workplace', 'Oslo')
    echo "<a href='$url'>Oslo ($count)</a>";
?>

get_jobpost_filters()

Retrieve a list of all distinct values from a given field across all job posts. A useful function to show available filtering values.

For instance, if you want to include a sidebar on your job post feed page, where the user can click to limit the result of job posts. An example of this could be to build a filter option based on location.

Parameters

get_jobpost_filters( $field, $reduce_by_filter );
  • $field {string} (required) The field name.

  • $reduce_by_filter {boolean} (optional) Default is false, which will always return all distinct values. If set to true the result will return distinct values based on the current filter.

Example

Here is an example on how one could create filter options based on location. This example uses the get_jobpost_filter_url() helper function.

<?php

# Load all distinct values for the field «workplace»
$locations = get_jobpost_filters('workplace');

# Only show this filter if the list contains values
if( $locations ) {

    echo "<h2>Locations</h2>";
    echo "<ul>";
    
    # Loop through all the distinct values
    foreach( $locations as $location ) {
    
        # Create a filter URL
        # Link will add or remove the parameter workplace=$location
        # Example site.com/jobs/?workplace=oslo
        $url = get_jobpost_filter_url('workplace', $location);
    
        # Display link
        echo "<li><a href='$url'>$location</a></li>";
        
    }
    
    echo "</ul>";
}

The example above will always show all locations. If you would like to reduce the number of locations based on the current active filter, then set the second parameter to true. Like this: get_jobpost_filters('workplace', true);.

has_jobpost_filter()

Returns true if there is an active filter on a given field. Or if a second parameter with a field value is given, then it will return true if there is an active filter with this field and this value.

Parameters

has_jobpost_filter( $field, $value );
  • $field {string} (required) The field name

  • $value {boolean} (optional) If set, the result will return true if there is an active filter on this field with the given value.

Example on a WP function that combines the filter helpers

In this example we have included the has_jobpost_filter() function in the example we gave in the get_jobpost_filters() section. This allows us to add a CSS class to the list element to show that this filter is active.

<?php

function jobPostFilter(string $field, string $filter_title, bool $reduce_if_active = false) {

    # Load all distinct values for the given field
    $items = get_jobpost_filters( $field, $reduce_if_active );

    # Bail early if no items
    if( empty( $items ) ) return;

    # Show title and create list
    echo "<h3>$filter_title</h3>";
    
    echo "<ul>";
    
    foreach( $items as $item ) {
        
        # Generate URL to turn on/off filter
        $url = get_jobpost_filter_url( $field, $item );
        # Count job posts with this filter
        $count = get_jobpost_filter_count( $field, $item );
        # Set class as active if filter with this item is active.
        $active = ( has_jobpost_filter( $field, $item ) ) ? 'active' : '';
        
        echo "<li class='$active'>";
            # Added nofollow to tell browser not to crawl
            echo "<a href='$url' rel='nofollow'>$item ($count)</a>";
        echo "</li>";
        
    }

    echo "</ul>";

} 

?>

<aside>

    <div class="job_filter">
        <?php jobPostFilter( 'workplace', 'Work Place', true ); ?>
    </div>

    <div class="job_filter">
        <?php jobPostFilter( 'type', 'Position type', true ); ?>
    </div>

</aside>

Convert the above code to shortcode.

This can be added to your theme functions.php file or use a plugin that allows to add PHP snippets, like for instance: https://wordpress.org/plugins/code-snippets/

function jobPostFilter(string $field, string $filter_title, bool $reduce_if_active = false) {

    # Load all distinct values for the given field
    $items = get_jobpost_filters( $field, $reduce_if_active );
    
    $html = '';

    # Bail early if no items
    if( empty( $items ) ) return $html;

    # Show title and create list
    $html .= "<h3>$filter_title</h3>";
    
    $html .= "<ul>";
    
    foreach( $items as $item ) {
        
        # Generate URL to turn on/off filter
        $url = get_jobpost_filter_url( $field, $item );
        # Count job posts with this filter
        $count = get_jobpost_filter_count( $field, $item );
        # Set class as active if filter with this item is active.
        $active = ( has_jobpost_filter( $field, $item ) ) ? 'active' : '';
        
        $html .= "<li class='$active'>";
            # Added nofollow to tell browser not to crawl
            $html .= "<a href='$url' rel='nofollow'>$item ($count)</a>";
        $html .= "</li>";
        
    }

    $html .= "</ul>";
    
    return $html;

}

// Some code
add_shortcode( 'job_filter', static function ($params = [], $content = null, $tag = '') {
    
    # Bail if plugin is not active
    if( ! function_exists( 'have_jobposts' ) ) return $content;
    
    // Normalize attribute keys, lowercase
    $params = array_change_key_case( (array)$params, CASE_LOWER );
    
    // Override default attributes with user attributes
    $options = shortcode_atts( [
        'field' => 'type',
        'title' => 'Job type',
        'reduce' => '1'
    ], $params, $tag );
    
    $content = jobPostFilter( (string)$options['field'], (string)$options['title'], (bool) $options['reduce'] );

    return $content;

} );

# No you can use the shortcode [job_filter field='location_city' title='Area' reduce='1']

Last updated