最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - ACF - Can't get custom post type by taxonomy

programmeradmin1浏览0评论

Although I've read many other answers with the same question on StackOverflow none of them where able to fix the issue I have.

I'm using ACF and have a custom post type called "services". The CPT has a custom taxonomy called "category", this returns the TermID of the taxonomy.

This was working before when I used a standard taxonomy, but since i've switched to the ACF taxonomy it has stopped working.

//This works fine
$serviceName = $wp_query->query_vars['test'];

//CURRENT The loop issue with ACF which doesnt work
   $args = array(
        'post_type' => 'services',
        'tax_query' => array(
            array(
            'taxonomy' => 'category',
            'field' => 'slug',
            'terms' => $serviceName
             )
          )
    );


//OLD The loop when using standard taxonomy that works
   $args = array(
        'post_type' => 'services',
        'category_name' => $serviceName,
    );


//Display
$service_posts = get_posts( $args );

In Functions.php I have created a function which makes the ACF categories filter out the ones I don't want. This is to prevent the user choosing categories which aren't services.

function service_categories( $args, $field, $post_id ) {

    $args['orderby'] = 'count';
    $args['order'] = 'DESC';
    $args['include'] = '6, 7, 8, 9, 10, 15, 16';
    return $args;

}

This works on the dashboard, however I wonder if this at all may be interfering at all?

Thank you for your time.

EDIT:

Also noticed when you view all the CPT's the category tab is blank, not sure why this is since as far as I'm aware the ACF should overwrite this anyway.

发布评论

评论列表(0)

  1. 暂无评论