Skip to content

Filtering & Search

The plugin includes a set of helper functions for building custom filter and search interfaces using template functions.

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.

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>

Returns a list of all distinct values for a given field across all job posts. Use this to build filter options.

get_jobpost_filters( $field, $reduce_by_filter );
ParameterTypeRequiredDescription
$fieldstringYesThe field name
$reduce_by_filterboolNoIf true, only returns values matching the current filter. Default: false
<?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;
?>

Generates a URL that toggles a filter on or off. If the filter is already active, the URL removes it.

get_jobpost_filter_url( $field, $value );
ParameterTypeRequiredDescription
$fieldstringYesThe field name to filter
$valuestringYesThe value to filter by
<?php
$url = get_jobpost_filter_url('workplace', 'Oslo');
echo "<a href='$url'>Oslo</a>";
?>

Returns the number of jobs matching a specific filter value.

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

Check if a filter is currently active.

has_jobpost_filter( $field, $value );
ParameterTypeRequiredDescription
$fieldstringYesThe field name
$valuestringNoIf set, checks if this specific value is active

Here’s a reusable function that combines all the filter helpers:

<?php
function 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>

You can register the filter function as a shortcode in your theme’s functions.php or via a code snippets plugin:

<?php
function 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"]