I'm trying to get posts with custom taxonomies. I have this function below:
public function get_job_categories_all () {
$get_categories = get_terms( array(
'taxonomy' => 'job_listing_category',
'hide_empty' => false,
) );
$categories = array();
foreach ( $get_categories as $cat ) {
array_push($categories, $cat->slug);
}
return $categories;
}
I'm calling this function on two other functions
private function get_job_dashboard_query_args( $posts_per_page = -1, $post_status = null, $category = null ) {
$post_status_array = [ 'publish', 'expired', 'pending', 'draft', 'preview' ];
if (!$post_status) { // if not set
$post_status = $post_status_array;
}
$job_categories = $this->get_job_categories_all;
if (!$category) {
$category = $job_categories;
}
$job_dashboard_args = array(
'post_type' => 'job_listing',
'post_status' => $post_status, // [ 'publish', 'expired', 'pending', 'draft', 'preview' ],
'ignore_sticky_posts' => 1,
'posts_per_page' => $posts_per_page,
'orderby' => 'date',
'order' => 'desc',
'author' => get_current_user_id(),
'tax_query' => array( // tax_query = taxonomy query, queried under $wpdb->term_taxonomy
array(
'taxonomy' => 'job_listing_category',
'field' => 'slug',
'terms' => $category,
),
),
);
if ( $posts_per_page > 0 ) {
$job_dashboard_args['offset'] = ( max( 1, get_query_var( 'paged' ) ) - 1 ) * $posts_per_page;
}
return apply_filters( 'job_manager_get_dashboard_jobs_args', $job_dashboard_args );
}
public function job_dashboard( $atts ) {
...
$categories_all_array = $this->get_job_categories_all();
$new_atts = shortcode_atts( [
'posts_per_page' => '25',
'post_status' => [ 'publish', 'expired', 'pending', 'draft', 'preview' ],
'category' => $categories_all_array,
], $atts );
$posts_per_page = $new_atts['posts_per_page'];
$post_status = $new_atts['post_status'];
$categories_all = $new_atts['category'];
wp_enqueue_script( 'wp-job-manager-job-dashboard' );
ob_start();
$jobs = new WP_Query( $this->get_job_dashboard_query_args( $posts_per_page, $post_status, $categories_all) );
...
}
I can do a shortcode like [job_dashboard category='healthcare']
but doing something like [job_dashboard category='healthcare, others']
returns no results when it's supposed to return posts with either of those two categories. I tried hardcoding them inside the brackets and they work fine but when I use a variable, they don't. What could be wrong?
category='healthcare'
orcategory='healthcare, others'
, just need to be comma separated.$category
is not even an array? I am passing an array to my tax_query, so myget_job_categories_all()
function doesn't even return an array? please clarifycategory
argument in the shortcode, it would show no results - it got reversed this time.