0

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?

3
  • Interesting question. If everything works fine besides this, I guess categories or terms should be passed as array. Inside shortcode, when you check if $category is not set, make "else" and use $category = explode(',', $category); . Explode will return you a working array with categories, no matter if its category='healthcare' or category='healthcare, others', just need to be comma separated.
    – anton
    May 6, 2021 at 20:10
  • @anton, wait so my $category is not even an array? I am passing an array to my tax_query, so my get_job_categories_all() function doesn't even return an array? please clarify
    – Norseback
    May 7, 2021 at 5:31
  • I've included the else clause under the if clause and now, if I didn't include the category argument in the shortcode, it would show no results - it got reversed this time.
    – Norseback
    May 7, 2021 at 6:05

1 Answer 1

0

I found out that I forgot to implode the array values on public function job_dashboard( $atts ) function. Updated code below:

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 = explode(",", $category);
    } elseif (!$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'       => implode(",", $categories_all_array), // output: item1,item2,item3 ...
    ], $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) );
    
    ...
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.