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

custom taxonomy - how do you pull data from two taxonomies?

programmeradmin2浏览0评论

I have posts loaded from taxonomy, but I'm trying to get the data of another, that's also attached to the same post. In the code below, the main focus is getting $location_slug. Knowing how to get that I think I'd know how to get more data out of the second taxonomy.

// args for the term_query
    $sub_taxonomy = 'department_categories';
    $tax_args = array(
        'taxonomy'               => $sub_taxonomy,
        'orderby'                => 'menu_order',
        'order'                  => 'ASC',
        'hide_empty'             => false,
    );

$the_query = new WP_Term_Query($tax_args );

// loop through the terms returned
foreach($the_query->get_terms() as $term){

    $term_id = $term->term_id;
    $term_name = $term->name;
    $term_slug = $term->slug;

    // args for the post query
    $post_type = 'career';

    $tax_query = array(
        array(
            'taxonomy' => $sub_taxonomy,
            'field'    => 'id',
            'terms'    => $term_id,
            // 'operator' => 'IN',
        )
    );
    $post_args = array(
        'posts_per_page' => -1,
        'orderby' => 'name',
        'order' => 'asc',
        'post_type' => $post_type,
        'tax_query' => $tax_query
    );

    $primary_taxonomy = 'location_categories';
    $location = get_term( $term_id, $primary_taxonomy);
    $location_slug = $location->slug;

    query_posts( $post_args );

    $html_out .= '<h6>' . $term_name . '</h6>';

    $html_out .= '<div data-id="' . $location_slug . '" class="tab-pane fade in">';
            $html_out .= '<div>';
                $html_out .= '<div class="uncode_text_column">';

                // lopp through the posts
                if (have_posts()) : while (have_posts()) : the_post();

                    $product_title = get_the_title();
                    $product_link = get_the_permalink();

                    $html_out .= $product_title;

                endwhile; else: endif;

                $html_out .= '</div>';
            $html_out .= '</div>';
        $html_out .= '</div>';

}

I have posts loaded from taxonomy, but I'm trying to get the data of another, that's also attached to the same post. In the code below, the main focus is getting $location_slug. Knowing how to get that I think I'd know how to get more data out of the second taxonomy.

// args for the term_query
    $sub_taxonomy = 'department_categories';
    $tax_args = array(
        'taxonomy'               => $sub_taxonomy,
        'orderby'                => 'menu_order',
        'order'                  => 'ASC',
        'hide_empty'             => false,
    );

$the_query = new WP_Term_Query($tax_args );

// loop through the terms returned
foreach($the_query->get_terms() as $term){

    $term_id = $term->term_id;
    $term_name = $term->name;
    $term_slug = $term->slug;

    // args for the post query
    $post_type = 'career';

    $tax_query = array(
        array(
            'taxonomy' => $sub_taxonomy,
            'field'    => 'id',
            'terms'    => $term_id,
            // 'operator' => 'IN',
        )
    );
    $post_args = array(
        'posts_per_page' => -1,
        'orderby' => 'name',
        'order' => 'asc',
        'post_type' => $post_type,
        'tax_query' => $tax_query
    );

    $primary_taxonomy = 'location_categories';
    $location = get_term( $term_id, $primary_taxonomy);
    $location_slug = $location->slug;

    query_posts( $post_args );

    $html_out .= '<h6>' . $term_name . '</h6>';

    $html_out .= '<div data-id="' . $location_slug . '" class="tab-pane fade in">';
            $html_out .= '<div>';
                $html_out .= '<div class="uncode_text_column">';

                // lopp through the posts
                if (have_posts()) : while (have_posts()) : the_post();

                    $product_title = get_the_title();
                    $product_link = get_the_permalink();

                    $html_out .= $product_title;

                endwhile; else: endif;

                $html_out .= '</div>';
            $html_out .= '</div>';
        $html_out .= '</div>';

}
Share Improve this question asked Jan 24, 2020 at 16:26 Darren BachanDarren Bachan 3752 gold badges7 silver badges17 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

First, get all term slugs from the custom example taxonomy space by current post ID.

$space_terms = wp_get_post_terms( $post->ID, 'space' );
if( $space_terms ) {
$space_terms = array();
  foreach( $space_terms as $term ) {
   $space_terms[] = $term->slug;
 }

}

You should specify the logical relationship between each inner taxonomy array when there is more than one.

The relation key in the array describes the relationship. Possible values are OR and AND.

'tax_query' => array(
'relation' => 'OR',
array(
    'taxonomy' => 'department_categories',
    'field'    => 'slug',
    'terms'    => $course_terms,
),
array(
    'taxonomy' => 'space',
    'field'    => 'slug',
    'terms'    => $space_terms,
 ),
),
发布评论

评论列表(0)

  1. 暂无评论