Skip to content

Commit

Permalink
Add/featured first argument (#2675)
Browse files Browse the repository at this point in the history
* Add featured first argument
* Fix case where featured and rand_featured are set to the order
  • Loading branch information
mikeyarce authored Dec 12, 2023
1 parent 9e27664 commit e7f72db
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 0 deletions.
2 changes: 2 additions & 0 deletions assets/js/ajax-filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,7 @@ jQuery( document ).ready( function( $ ) {
var $results = $target.find( '.job_listings' );
var per_page = $target.data( 'per_page' );
var orderby = $target.data( 'orderby' );
var featured_first = $target.data( 'featured_first' );
var order = $target.data( 'order' );
var featured = $target.data( 'featured' );
var filled = $target.data( 'filled' );
Expand Down Expand Up @@ -380,6 +381,7 @@ jQuery( document ).ready( function( $ ) {
filter_post_status: post_status,
per_page: per_page,
orderby: orderby,
featured_first: featured_first,
order: order,
page: page,
featured: featured,
Expand Down
2 changes: 2 additions & 0 deletions includes/class-wp-job-manager-ajax.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ public function get_listings() {
$featured = isset( $_REQUEST['featured'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['featured'] ) ) : null;
$remote_position = isset( $_REQUEST['remote_position'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['remote_position'] ) ) : null;
$show_pagination = isset( $_REQUEST['show_pagination'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['show_pagination'] ) ) : null;
$featured_first = isset( $_REQUEST['featured_first'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['featured_first'] ) ) : null;
// phpcs:enable WordPress.Security.NonceVerification.Recommended

if ( is_array( $search_categories ) ) {
Expand All @@ -154,6 +155,7 @@ public function get_listings() {
'post_status' => $filter_post_status,
'orderby' => $orderby,
'order' => $order,
'featured_first' => $featured_first,
'offset' => ( $page - 1 ) * $per_page,
'posts_per_page' => max( 1, $per_page ), // phpcs:ignore WordPress.WP.PostsPerPage.posts_per_page_posts_per_page -- Known slow query.
];
Expand Down
3 changes: 3 additions & 0 deletions includes/class-wp-job-manager-shortcodes.php
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,7 @@ public function output_jobs( $atts ) {
'featured' => null, // True to show only featured, false to hide featured, leave null to show both.
'filled' => null, // True to show only filled, false to hide filled, leave null to show both/use the settings.
'remote_position' => null, // True to show only remote, false to hide remote, leave null to show both.
'featured_first' => false, // True to show featured first, false to show in default order.

// Default values for filters.
'location' => '',
Expand All @@ -613,6 +614,7 @@ public function output_jobs( $atts ) {
$atts['show_category_multiselect'] = $this->string_to_bool( $atts['show_category_multiselect'] );
$atts['show_more'] = $this->string_to_bool( $atts['show_more'] );
$atts['show_pagination'] = $this->string_to_bool( $atts['show_pagination'] );
$atts['featured_first'] = $this->string_to_bool( $atts['featured_first'] );

if ( ! is_null( $atts['featured'] ) ) {
$atts['featured'] = ( is_bool( $atts['featured'] ) && $atts['featured'] ) || in_array( $atts['featured'], [ 1, '1', 'true', 'yes' ], true );
Expand Down Expand Up @@ -679,6 +681,7 @@ public function output_jobs( $atts ) {
'order' => $atts['order'],
'categories' => implode( ',', $atts['categories'] ),
'disable-form-state-storage' => $disable_client_state,
'featured_first' => $atts['featured_first'] ? 'true' : 'false',
];

if ( $atts['show_filters'] ) {
Expand Down
8 changes: 8 additions & 0 deletions wp-job-manager-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ function get_job_listings( $args = [] ) {
'filled' => null,
'remote_position' => null,
'fields' => 'all',
'featured_first' => 0,
]
);

Expand Down Expand Up @@ -180,6 +181,13 @@ function get_job_listings( $args = [] ) {
];
}

if ( 'true' === $args['featured_first'] && 'featured' !== $args['orderby'] && 'rand_featured' !== $args['orderby'] ) {
$query_args['orderby'] = [
'menu_order' => 'ASC',
$query_args['orderby'] => $query_args['order'],
];
}

$job_manager_keyword = sanitize_text_field( $args['search_keywords'] );

if ( ! empty( $job_manager_keyword ) && strlen( $job_manager_keyword ) >= apply_filters( 'job_manager_get_listings_keyword_length_threshold', 2 ) ) {
Expand Down

0 comments on commit e7f72db

Please sign in to comment.