peterigz
Forum Replies Created
-
Pretty much have mine working again now, I don’t know how hacky this is, but it works for now. If anyone from the REST API team read this then a little tutorial covering the do’s and don’ts would be great, especially for tax and meta queries 🙂
I am using woocommerce and filtering products so what I need will differ to you but basically I had to first register the _price meta_key so that products could be properly ordered:
$args = array( 'type' => 'float', 'description' => 'Product price for woocommerce products', 'single' => false, 'show_in_rest' => true, ); register_meta( 'product', '_price', $args );I also had to modify the existing order and orderby collection parameters for the product post type to accept new values:
add_filter( 'rest_product_collection_params', 'swish_rest_product_collection_params', 10, 2); function swish_rest_product_collection_params($params, $post_type_obj) { $params['order'] = array( 'description' => __( 'Order sort attribute ascending or descending.' ), 'type' => 'string', 'default' => 'desc', 'enum' => array( 'asc', 'desc', 'ASC', 'DESC' ), ); $params['orderby'] = array( 'description' => __( 'Sort collection by object attribute.' ), 'type' => 'string', 'default' => 'date', 'enum' => array( 'date', 'relevance', 'id', 'include', 'title', 'slug', 'meta_value_num', 'menu_order title', 'menu_order', 'data' ), ); return $params; }Finally I replaced my original rest_query_vars filter with this:
add_filter( 'rest_product_query', 'swish_rest_product_query' ,10 ,2 ); function swish_rest_product_query( $args, $request ) { $args['meta_query'] = array( 'compare' => 'IN', 'key' => '_visibility', 'value' =>array('visible', 'catalog') ); if(isset($request['filter']['tax_query'])) { $tax_query = array(); foreach ($request['filter']['tax_query'] as $query) { $tax_query[] =array( 'taxonomy' => $query['taxonomy'], 'field' => $query['field'], 'terms' => $query['terms'], 'operator' => $query['operator'] ); } $args['tax_query'] = $tax_query; } if(isset($request['filter']['product_cat'])) { $args['product_cat'] = $request['filter']['product_cat']; } if(isset($request['filter']['meta_key'])) { $args['meta_key'] = $request['filter']['meta_key']; } return $args; }Your needs will differ but start with rest_{your_custom_post}_query, and build your standard wp_query with that by adding to the args, using the request object to grab the values as necessary.
Same problem here – rest_query_vars is no longer a filter so we need to look to the following filters:
rest_{$this->post_type}_query where you can filter the query args
rest_{$this->post_type}_collection_params where you add additional parameters for the collection.
rest_query_var-{$key} – similar to rest_query_vars but the key is dynamic.You’ll also need to make use of register_meta to enable any meta fields you might need. I’m still trying to figure things out, will come back here once I have done. Any other insights welcome! More info here for reference.
- This reply was modified 9 years, 5 months ago by peterigz. Reason: added rest_query_var-{$key}
Forum: Plugins
In reply to: [Order Export for WooCommerce] Company name field missingHi, I’d just like to raise this again as I can’t see the availability of the company name when exporting orders. I can see it for the customer export but not the order export. It’s also not shown as part of the pro version. Can you confirm?
Thanks.