Filter job listing shortcode arguments
Adds a job roles to the available attributes in the job listings shortcode.
/**
* Adds a job roles to the available atts in the job listings shortcode.
*
* @param array $out The current shortcode atts.
* @param array $pairs The supported attributes and their defaults.
* @param array $atts The user defined attributes.
* @param string $shortcode The shortcode name.
*
* @return array The maybe modified shortcode atts.
*/
function hd_jobrelay_filter_recpress_job_listings_shortcode_args( $out, $pairs, $atts, $shortcode ) {
// if the user has not defined job roles.
if ( empty ($atts['job_roles'] ) ) {
return $out;
}
// add job role and a default to the atts.
$out['job_roles'] = sanitize_text_field( $atts['job_roles'] );
// return the args.
return $out;
}
add_filter( 'shortcode_atts_recpress_job_listings', 'hd_jobrelay_filter_recpress_job_listings_shortcode_args', 10, 4 );
/**
* Maybe adds job roles tax queries to the job listing query args.
*
* @param array $args The current job listing shortcode query args.
* @param array $attrs The array of args passed to the shortcode.
*
* @return array $args The maybe modified query args.
*/
function hd_jobrelay_add_job_roles_job_listings_query_args( $args, $attrs ) {
// if we have no job roles attribute.
if ( empty( $attrs['job_roles'] ) ) {
return $args; // do nothing.
}
// turn the job roles args into an array.
$job_roles = explode( '|', $attrs['job_roles'] );
// if job roles is for some reason empty.
if ( empty( $job_roles ) ) {
return $args; // do nothing.
}
// loop through each job roles.
foreach ( $job_roles as $job_roles ) {
// add this job roles to the tax query array.
$args['tax_query']['types'][$job_roles] = [
'taxonomy' => 'job_listing_role',
'field' => 'slug',
'terms' => sanitize_text_field( $job_roles ),
];
}
// return the query args.
return $args;
}
add_filter( 'recpress_job_listings_query_args', 'hd_jobrelay_add_job_roles_job_listings_query_args', 10, 2 );