php - How to Pass Current Taxonomy Terms into new WP_Query?

admin2025-01-08  3

On my single portfolio pages, I want to add a "See more from this category" section under the post.

Here is my code that checks for the single CPT and creates the new query:

add_action( 'genesis_loop', 'bds_more_projects_query' );
    function bds_more_projects_query() {
        if ( is_singular( 'portfolio' ) ) {
            $categories = get_the_terms( $post->ID, 'portfolio_category' );
            $args = array(
                'post_type' => 'portfolio',
                'posts_per_page' => 4,
                'tax_query' => array(
                    array(
                        'taxonomy' => 'portfolio_category',
                        'field' => 'name',
                        'terms' => array( $categories ),
                    ),
                ),
            );
            $loop = new WP_Query( $args );
            if ( $loop->have_posts() ) {
                while ( $loop->have_posts() ) : $loop->the_post();
                echo the_title();
                endwhile;
            }
            wp_reset_postdata();
        }
    }

When I manually type in a term string, the appropriate titles all echo out as desired, but I cannot get the "get_the_terms" function to work for dynamic display of the current post category.

What am I missing is my code?

On my single portfolio pages, I want to add a "See more from this category" section under the post.

Here is my code that checks for the single CPT and creates the new query:

add_action( 'genesis_loop', 'bds_more_projects_query' );
    function bds_more_projects_query() {
        if ( is_singular( 'portfolio' ) ) {
            $categories = get_the_terms( $post->ID, 'portfolio_category' );
            $args = array(
                'post_type' => 'portfolio',
                'posts_per_page' => 4,
                'tax_query' => array(
                    array(
                        'taxonomy' => 'portfolio_category',
                        'field' => 'name',
                        'terms' => array( $categories ),
                    ),
                ),
            );
            $loop = new WP_Query( $args );
            if ( $loop->have_posts() ) {
                while ( $loop->have_posts() ) : $loop->the_post();
                echo the_title();
                endwhile;
            }
            wp_reset_postdata();
        }
    }

When I manually type in a term string, the appropriate titles all echo out as desired, but I cannot get the "get_the_terms" function to work for dynamic display of the current post category.

What am I missing is my code?

Share Improve this question asked May 4, 2017 at 22:25 danbru1989danbru1989 12 bronze badges 1
  • terms needs an array of term IDs, get_the_terms returns an array of term objects. Right now, your code places the array of term objects into another array. – Milo Commented May 4, 2017 at 23:05
Add a comment  | 

1 Answer 1

Reset to default 0

Try this:

add_action( 'genesis_loop', 'bds_more_projects_query' );
function bds_more_projects_query() {
    if ( is_singular( 'portfolio' ) ) {
        $categories = get_the_terms( $post->ID, 'portfolio_category' );
        $cat_ids = array();
        foreach ( $categories as $category ) {
            $cat_ids[] = $category->term_id;
        }
        $args = array(
            'post_type' => 'portfolio',
            'posts_per_page' => 4,
            'tax_query' => array(
                array(
                    'taxonomy' => 'portfolio_category',
                    'field' => 'id',
                    'terms' => array( $cat_ids ),
                ),
            ),
        );
        $loop = new WP_Query( $args );
        if ( $loop->have_posts() ) {
            while ( $loop->have_posts() ) : $loop->the_post();
            echo the_title();
            endwhile;
        }
        wp_reset_postdata();
    }
}

Basically, loop over the $categories array, adding the term_id of each to a new array, and then use that new array in the query arguments. The get_the_terms function returns an array of WP_Term objects, rather than just an array of IDs (see the WP Code Reference for more info).

The other thing to check is that $post is giving you the current post inside the function.

To verify, add var_dump($post) just inside the is_singular( 'portfolio' ) condition and make sure it’s giving you the current post object. If it’s not, add global $post inside the bds_more_projects_query function and try again.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736267719a1217.html

最新回复(0)