Skip Navigation

[Resolved] Query select field

This thread is resolved. Here is a description of the problem and solution.

Problem:

Use WP_Query to extract all order posts containing a specific value on the select field.

Solution:

The WP_Query will query posts in "Published" status by default:
https://developer.wordpress.org/reference/classes/wp_query/#status-parameters

But Woocommerce order posts do not use WP default status, so you need to define the post status parameter in WP_Query.

For example:

https://toolset.com/forums/topic/query-select-field/#post-2101145

Relevant Documentation:

http://hookr.io/functions/wc_get_order_statuses/

This support ticket is created 3 years, 6 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 2 replies, has 2 voices.

Last updated by Puntorosso 3 years, 6 months ago.

Assisted by: Luo Yang.

Author
Posts
#2101125

I have created a select custom field "Event of order" for WooCommerce orders .
This select field is populated with ids of a custom post "tc_events" using this function

add_filter( 'wpt_field_options', 'populate_eventoforder_dropdown', 10, 3);
 function populate_eventoforder_dropdown( $options, $title, $type ){
    switch( $title ){
        case 'Event of order':
            $options = array(
            array(
      '#value' => '',
      '#title' => '',
	  	)
	  );
            $args = array(
                'post_type'        => 'tc_events',
                'post_status'      => 'publish',
                'posts_per_page'   => -1,
                'orderby' => 'title',
				'order' => 'ASC'
      );
            $posts_array = get_posts( $args );
            foreach ($posts_array as $post) {
                $options[] = array(
                    '#value' => $post->ID,
                    '#title' => get_the_title( $post->ID ) . ' - ' . $post->ID
                );
            }
            break;
    }
    return $options;
}

I use then this query to extract all orders containing a specific value on this select field

$args = array(
    'post_type'  => 'shop_order',
    'posts_per_page' => -1,
    'meta_query' => array(
    	array(
           'key'   => 'wpcf-event-of-order',
           'value' => $event_id
        )
	)
);

$query = new WP_Query( $args );
if($query->have_posts()) :
        while($query->have_posts()) : $query->the_post();
        echo $query->post->ID;
        endwhile;
        wp_reset_postdata();
 endif;

but I don't get any results.

What I am doing wrong?

#2101145

Hello,

The WP_Query will query posts in "Published" status by default:
https://developer.wordpress.org/reference/classes/wp_query/#status-parameters

But Woocommerce order posts do not use WP default status, so you need to define the post status parameter in WP_Query.

For example:

$args = array(
    'post_type'  => 'shop_order',
    'post_status'  => 'wc-completed', // completed order
    'posts_per_page' => -1,
    'meta_query' => array(
        array(
           'key'   => 'wpcf-event-of-order',
           'value' => $event_id
        )
    )
);

More help:
hidden link

#2101401

Luo, you totally nailed it!
Works perfectly.
Thanks a lot