Filtering & Search
The plugin includes a set of helper functions for building custom filter and search interfaces using template functions.
How Filtering Works
Section titled “How Filtering Works”Filtering is done through URL query parameters. When a page loads with ?workplace=oslo, the job feed is automatically limited to jobs where the workplace field contains “Oslo”.
The plugin provides helper functions to build filter URLs, count matches, and check active state.
Free-Text Search
Section titled “Free-Text Search”Add a simple search form that searches across all job data:
<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_filters()
Section titled “get_jobpost_filters()”Returns a list of all distinct values for a given field across all job posts. Use this to build filter options.
Syntax
Section titled “Syntax”get_jobpost_filters( $field, $reduce_by_filter );| Parameter | Type | Required | Description |
|---|---|---|---|
$field | string | Yes | The field name |
$reduce_by_filter | bool | No | If true, only returns values matching the current filter. Default: false |
Example
Section titled “Example”<?php$locations = get_jobpost_filters('workplace');
if ($locations): echo "<h3>Locations</h3><ul>"; foreach ($locations as $location): $url = get_jobpost_filter_url('workplace', $location); echo "<li><a href='$url'>$location</a></li>"; endforeach; echo "</ul>";endif;?>get_jobpost_filter_url()
Section titled “get_jobpost_filter_url()”Generates a URL that toggles a filter on or off. If the filter is already active, the URL removes it.
Syntax
Section titled “Syntax”get_jobpost_filter_url( $field, $value );| Parameter | Type | Required | Description |
|---|---|---|---|
$field | string | Yes | The field name to filter |
$value | string | Yes | The value to filter by |
Example
Section titled “Example”<?php$url = get_jobpost_filter_url('workplace', 'Oslo');echo "<a href='$url'>Oslo</a>";?>get_jobpost_filter_count()
Section titled “get_jobpost_filter_count()”Returns the number of jobs matching a specific filter value.
Syntax
Section titled “Syntax”get_jobpost_filter_count( $field, $value );Example
Section titled “Example”<?php$url = get_jobpost_filter_url('workplace', 'Oslo');$count = get_jobpost_filter_count('workplace', 'Oslo');echo "<a href='$url'>Oslo ($count)</a>";?>has_jobpost_filter()
Section titled “has_jobpost_filter()”Check if a filter is currently active.
Syntax
Section titled “Syntax”has_jobpost_filter( $field, $value );| Parameter | Type | Required | Description |
|---|---|---|---|
$field | string | Yes | The field name |
$value | string | No | If set, checks if this specific value is active |
Complete Filter Function
Section titled “Complete Filter Function”Here’s a reusable function that combines all the filter helpers:
<?phpfunction jobPostFilter(string $field, string $title, bool $reduce = false) { $items = get_jobpost_filters($field, $reduce);
if (empty($items)) return;
echo "<h3>$title</h3>"; echo "<ul>";
foreach ($items as $item) { $url = get_jobpost_filter_url($field, $item); $count = get_jobpost_filter_count($field, $item); $active = has_jobpost_filter($field, $item) ? 'active' : '';
echo "<li class='$active'>"; echo "<a href='$url' rel='nofollow'>$item ($count)</a>"; echo "</li>"; }
echo "</ul>";}?>
<aside> <div class="job-filter"> <?php jobPostFilter('workplace', 'Location', true); ?> </div> <div class="job-filter"> <?php jobPostFilter('type', 'Job Type', true); ?> </div></aside>Converting to a Custom Shortcode
Section titled “Converting to a Custom Shortcode”You can register the filter function as a shortcode in your theme’s functions.php or via a code snippets plugin:
<?phpfunction jobPostFilter(string $field, string $title, bool $reduce = false) { $items = get_jobpost_filters($field, $reduce); $html = '';
if (empty($items)) return $html;
$html .= "<h3>$title</h3>"; $html .= "<ul>";
foreach ($items as $item) { $url = get_jobpost_filter_url($field, $item); $count = get_jobpost_filter_count($field, $item); $active = has_jobpost_filter($field, $item) ? 'active' : '';
$html .= "<li class='$active'>"; $html .= "<a href='$url' rel='nofollow'>$item ($count)</a>"; $html .= "</li>"; }
$html .= "</ul>"; return $html;}
add_shortcode('job_filter', function ($params = []) { if (!function_exists('have_jobposts')) return '';
$params = array_change_key_case((array) $params, CASE_LOWER); $options = shortcode_atts([ 'field' => 'type', 'title' => 'Job type', 'reduce' => '1' ], $params);
return jobPostFilter( (string) $options['field'], (string) $options['title'], (bool) $options['reduce'] );});
// Usage: [job_filter field="workplace" title="Location" reduce="1"]