I would dump this get_post_types(array(‘show_in_rest’ => true), ‘objects’ – are WooCommerce products post type included?
I got array of objects and one of the objects is Woocommerce product:
post
page
attachment
wp_block
product
slider
news
"product" string(6)
_builtin: false
_edit_link: "post.php?post=%d"
can_export: true
cap: {edit_post: "edit_product", read_post: "read_product", delete_post: "delete_product", edit_posts: "edit_products", edit_others_posts: "edit_others_products", …}
capability_type: "product"
delete_with_user: null
description: "Здесь вы можете добавлять новые товары в ваш магазин."
exclude_from_search: false
has_archive: "shop"
hierarchical: false
label: "Товары"
labels: {name: "Товары", singular_name: "Товар", add_new: "Добавить", add_new_item: "Добавить товар", edit_item: "Изменить товар", …}
map_meta_cap: true
menu_icon: null
menu_position: null
name: "product"
public: true
publicly_queryable: true
query_var: "product"
register_meta_box_cb: null
rest_base: false
rest_controller: null
rest_controller_class: false
rewrite: {slug: "product", with_front: false, feeds: true, pages: true, ep_mask: 1}
show_in_admin_bar: true
show_in_menu: true
show_in_nav_menus: true
show_in_rest: true
show_ui: true
taxonomies: [] (0)
I also noticed that this part of program executes in urls like this:
site.com/wp-json/wc/v3/products
( echo 1 prints to screen )
add_action('rest_api_init', 'wp_rest_filter_add_filters');
function wp_rest_filter_add_filters() {
/* Adds Filter for All Post Type*/
foreach (get_post_types(array('show_in_rest' => true), 'objects') as $post_type) {
add_filter('rest_' . $post_type->name . '_query', 'wp_rest_filter_add_filter_param', 10, 2);
}
/* Adds Filter for All Taxonomy Type*/
foreach (get_taxonomies(array('show_in_rest' => true), 'objects') as $tax_type) {
add_filter('rest_' . $tax_type->name . '_query', 'wp_rest_filter_add_filter_param', 10, 2);
}
/* Adds Filter for User Type*/
foreach (get_taxonomies(array('show_in_rest' => true), 'objects') as $tax_type) {
add_filter('rest_' . 'user' . '_query', 'wp_rest_filter_add_filter_param', 10, 2);
}
}
But not this part:
( echo 1 doesn’t print to screen )
function wp_rest_filter_add_filter_param($args, $request) {
// Bail out if no filter parameter is set.
if (empty($request['filter']) || !is_array($request['filter'])) {
return $args;
}
$filter = $request['filter'];
// posts_per_page
if (isset($filter['posts_per_page']) && ((int) $filter['posts_per_page'] >= 1 && (int) $filter['posts_per_page'] <= 100)) {
$args['posts_per_page'] = $filter['posts_per_page'];
}
global $wp;
$vars = apply_filters('rest_query_vars', $wp->public_query_vars);
function allow_meta_query($valid_vars) {
$valid_vars = array_merge($valid_vars, array('meta_query', 'meta_key', 'meta_value', 'meta_compare'));
return $valid_vars;
}
$vars = allow_meta_query($vars);
foreach ($vars as $var) {
if (isset($filter[$var])) {
$args[$var] = $filter[$var];
}
}
return $args;
}
So problem in this line:
add_filter(‘rest_’ . ‘user’ . ‘_query’, ‘wp_rest_filter_add_filter_param’, 10, 2)
‘rest_product_query’ doesn’t work with Woocommerce.
I also tried to change products endpoint from /wc/v3 to /wp/v2/ but that didn’t work.