1

I am having some hard time figuring out why my get_posts function return all posts instead of specific ones.

I have a post type called mall, in it a taxonomy called shop which have two terms Compi and Macho. In the variable $store_name one of these terms is stored (depending on the product which is in the order) and when I am using the variable in terms for the $malls_args, I am getting all the malls. Even those without the term stored in the variable $store_name.

I tried to hard-code the variable and used "Copmi" inside the terms but I am still getting all the malls.

add_action( 'woocommerce_admin_order_data_after_order_details', 'asdf_test' );
function asdf_test( $order ) {

    $dokanOrderManager = new Dokan_Order_Manager();

    $vendor_id = get_post_meta( $order->get_id(), '_dokan_vendor_id', false )[0];

    foreach( $order->get_items() as $item_id => $line_item ) {

        $prod =  $line_item->get_product();
        $prod_id = $prod->get_id();
        $author = get_user_by( 'id', $prod->post->post_author );
        $store_info = dokan_get_store_info( $author->ID );
        $store_name = $store_info['store_name'];
        $store_address = $store_info['address']['street_1'] . ', ' . $store_info['address']['city'];
        $malls_args = array(
            "post_type" => "mall",
            "tax_query" => array(
                "taxonomy" => "shop",
                "field" => "name",
                "terms" => "Compi" // - - - - - - $store_name gives all posts too.
            )
        );

        $malls = get_posts( $malls_args );

        foreach ( $malls as $mall ) {
            echo $mall->ID . '<br/>';
        }

    }

}

Any idea? If anymore information is needed please tell me and I will do my best to provide it.

Thanks.

1 Answer 1

1

You are not using correctly the tax_queryargument. This argument takes array of tax query arguments arrays, to allow for complex queries with multiple taxonomies. You can learn more in the WP_Query documentation

This is the correct way:

$malls_args = array(
    "post_type" => "mall",
    "tax_query" => array(
        array(
            "taxonomy" => "shop",
            "field" => "name",
            "terms" => "Compi" // - - - - - - $store_name gives all posts too.        
        ),
    )
);

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.