• Hi!

    I just updated my website (on local host) to 4.7. I use the API call to retrieve posts from an CPT, filtered on a custom taxonomy. This looks like:
    /wp-json/wp/v2/reizen-api?filter[reizen-land]=test123

    ‘reizen-api’ is the rest_base of my CPT. ‘reizen-land’ is the custom taxonomy.

    This used to work well before the update (and works again when downgrading to 4.6.1.).

    However, in 4.7 it doesn’t seem to filter on the custom taxonomy. Is this an issue that is caused by the WP update?

    Thanks!

Viewing 15 replies - 1 through 15 (of 19 total)
  • We seem to be experiencing a similar issue (the same issue?) that appeared immediately after the upgrade to WordPress 4.7.

    We have custom post types and use a filter on rest_query_vars to allow an authenticated user to query on meta_query.

    add_filter( 'rest_query_vars', array( $this, 'allow_meta_query' ) );
    ...
            public function allow_meta_query( $valid_vars ) {
                    $valid_vars = array_merge( $valid_vars, array(
                            'meta_key', 'meta_value', 'meta_compare', 'meta_query'
                    ) );
    
                    return $valid_vars;
            }
    

    So, a query such as:

    /wp-json/wp/v2/our-custom-post-type?filter[meta_query][0][key]=a_meta_field&[meta_query][0][value]=something&[meta_query][0][relation]=AND

    would return items that have the meta field a_meta_field with the value something.

    After the upgrade to WordPress 4.7, such a query as above now returns all of the custom post type, rather than just items that match that meta query.

    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 8 years, 2 months ago by peterigz. Reason: added rest_query_var-{$key}

    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.

    Following.

    I’m experiencing the same issue after update to 4.7. For my case media filter doesn’t work, not custom taxonomy.

    wp-json/wp/v2/media?filter[post_parent]=ID

    I am also experiencing this.

    I did find this plugin, but it doesn’t seem to help with filtering by custom fields (even after I registered the meta). I am rolling back to WP 3.6 for now.

    https://github.com/WP-API/rest-filter

    I had a similar problem with /wp-json/wp/v2/bigram/?filter[s-letter]=H

    where bigram is a custom post type and s-letter is a custom taxonomy for the post type

    The REST API team meeting notes at

    REST API Team Meeting Notes, 2016-10-17


    includes the following

    The ?filter query parameter will be removed from the REST API plugin prior to core merge, a breaking change that improves the consistency of querying the API and eliminates a set of parameters that could introduce backwards compatibility issues were they to be committed to WordPress core. A separate plugin will be published to reinstate the filter parameter on a strictly opt-in basis.

    My solution was to change the request to use the taxonomy term directly.
    /wp-json/wp/v2/bigram/?s-letter=1729

    where 1729 is the term_id for the term with a name of ‘H’, slug ‘h’.

    I deactivated the WP REST API plugin.
    The overall result was significantly better.

    I didn’t try the rest-filter plugin.

    Ran into the same problem this morning. Deactivating the original REST plugin and adding the filter plugin referenced by @mtnporcupine fixed the problem without having to modify anything else in my case. The plugin is only about 10 lines of code — you could just as easily add it into your own templates.

    Is anyone out there filtering by meta_key/meta_value? The plugin does NOT seem to resolve that type of filter query, but trying to confirm if it’s just me.

    re: https://github.com/WP-API/WP-API/issues/1599#issuecomment-143412680

    This plugin restores the original filter functionality from the plugin in WordPress core: https://github.com/WP-API/rest-filter

    nickrigby

    (@nickrigby)

    @mtnporcupine Adding the meta keys to the $args collection in the Rest Filter plugin should make it work. I had the very same issue.

    Add this line:

    array_push($vars, 'meta_value', 'meta_key', 'meta_compare');

    Before this line (line 41 of the rest-api/plugin.php)

    $vars = apply_filters( 'query_vars', $wp->public_query_vars );

    I don’t encourage modifying core plugins, so you can either turn this into your own plugin, or there could be a cleaner way using WordPress hooks/filters.

    • This reply was modified 8 years ago by nickrigby.
    • This reply was modified 8 years ago by nickrigby.
    • This reply was modified 8 years ago by nickrigby.
    Nate Zander

    (@mtnporcupine)

    Interesting, thanks @nickrigby. I will test it out and report back.

    aaylersghost

    (@aaylersghost)

    @mtnporcupine, did you try applying the filters (in the plugin) to rest_query_vars rather than query_vars?

    Thanks for your help.

    @nickrigby your solution did not work for me, v0.1 of the plugin

    @aaylersghost that worked great!

    I changed line 42 to:

    $vars = apply_filters( 'rest_query_vars', $wp->public_query_vars );

    Yes, I did modify the plugin directly. Since I installed it manually, there is no risk of it accidentally being overwritten.

    Is it safe to assume this will be the permanent ‘opt-in’ fix to enable this functionality in the API?

    @aaylersghost Finally something that works! Been pulling my hairs about this last night. Thanks!
    @mtnporcupine You can also just copy the contents of the plugin file to your functions.php file.

Viewing 15 replies - 1 through 15 (of 19 total)
  • The topic ‘WordPress 4.7: Custom taxonomy filter stopped working’ is closed to new replies.