0

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?

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
    May 4, 2017 at 23:05

1 Answer 1

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.

2
  • I verified that I was getting the current post, first by using the_title() and second by using var_dump($post). The var_dump can be seen here [link] (drive.google.com/file/d/0B72pZs-sUlzHRWZySl9GaENSeHM/…). I did not see any info in there about my custom taxonomy. I thought it would be there. Problem? After trying your code, I started getting error "Cannot use object of type WP_Term as array".
    – danbru1989
    May 6, 2017 at 0:13
  • @danbru1989 Sorry…forgot that get_the_terms returns an array of objects, not an array of arrays. Changed line 7 of the sample code above and it should work correctly now. May 7, 2017 at 20:19

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.