分类术语未正确显示为数组

时间:2021-05-06 作者:Norseback

我正在尝试使用自定义分类法获取帖子。我有以下功能:

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;
}
我在另外两个函数上调用这个函数

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) );
    
    ...
}
我可以做一个像[job_dashboard category=\'healthcare\'] 但是做一些[job_dashboard category=\'healthcare, others\'] 当它应该返回包含这两个类别中任何一个的帖子时,不会返回任何结果。我试着在括号内对它们进行硬编码,它们工作得很好,但当我使用变量时,它们就不工作了。有什么问题吗?

1 个回复
SO网友:Norseback

我发现我忘了对上的数组值进行内爆public function job_dashboard( $atts ) 作用更新代码如下:

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) );
    
    ...
}

相关推荐

如何访问do_action_ref_array()中的所有数组值?

我正在尝试理解do\\u action\\u ref\\u array()的工作原理以及do\\u action()和do\\u action\\u ref\\u array()之间的区别。我通过了(Difference between do_action_ref_array() and do_action()) 并得到do\\u action\\u ref\\u array()现在不用于自定义挂钩,因为do\\u action()可以。do\\u action\\u ref\\u array()似乎允许