0

I have this code which I am using to loop through all products, then it should print the $current_product array if a product has term 'composite' from the 'product_type' taxonomy - it doesn't.

BUT if I replace $product->ID in the has_term function with the actual number it works fine and prints the array. Why?!

public function test12() {

    $args = array(
        'post_type' => 'product',
        'posts_per_page' => '-1',
    );

    $products = get_posts( $args ); ?>

    <?php foreach( $products as $product ) {

        if( has_term( 'composite', 'product_type', $product->ID ) ) {

            $current_product = array(
                'ID'            => $product->ID,
                'post_status'   => 'publish',
            );

            echo 'has';

            print_r($current_product);

        }

    }

}
6
  • what is the output of var_dump($product->ID); ? Jul 21, 2017 at 21:31
  • the var dump is: int(3833) where 3833 is the looped ID Jul 21, 2017 at 21:33
  • I tested and worked fine. Where are you using this function? On a theme? Plugin?
    – dbeja
    Jul 21, 2017 at 22:24
  • After a WP All Import job using the action pmxi_after_xml_import Jul 21, 2017 at 22:25
  • If $product is a WP_Post instance, you can pass that directly as 3rd argument to has_term rather than $product->ID.
    – Milo
    Jul 21, 2017 at 22:31

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.