7

I have the following situation:

I created a custom Post Type names Works. And also attached to it custom taxonomy named Work Types

Here is the code

function rk_work_post_type(){
$labels = array(
    'name'               => 'Work',
    'singular_name'      => 'Work',
    'add_new'            => 'Add Work',
    'all_items'          => 'All Works',
    'add_new_item'       => 'Add Work',
    'edit_item'          => 'Edit Work',
    'new_item'           => 'New Work',
    'view_item'          => 'View Work',
    'search_item'        => 'Search Work',
    'not_found'          => 'No items found',
    'not_found_in_trash' => 'No items found in trash',
    'parent_item_colon'  => 'Parent Item'
);
$args = array(
    'labels' => $labels,
    'public' => true,
    'has_archive' => true,
    'publicly_queryable' => true,
    'query_var' => true,
    'rewrite' => true,
    'capability_type' => 'post',
    'hierarchical' => false,
    'supports' => array(
        'title',
        'editor',
        'excerpt',
        'thumbnail',
        'revisions',
        'comments',
    ),
    // 'taxonomies' => array('category', 'post_tag'),
    'menu_icon' => 'dashicons-hammer',
    'menu_position' => 5,
    'exclude_from_search' => false,
    'show_in_rest'       => true,
    'rest_base'          => 'works',
    'rest_controller_class' => 'WP_REST_Posts_Controller',      
);
register_post_type('work',$args);
}

add_action('init','rk_work_post_type');


function rk_work_taxonomies() {
//add new taxonomy hierarchical
$labels = array(
    'name' => 'Work Types',
    'singular_name' => 'Work type',
    'search_items' => 'Search Types',
    'all_items' => 'All Work Types',
    'parent_item' => 'Parent Type',
    'parent_item_colon' => 'Parent Type:',
    'edit_item' => 'Edit Work Type',
    'update_item' => 'Update Work Type',
    'add_new_item' => 'Add New Work Type',
    'new_item_name' => 'New Work Type Name',
    'menu_name' => 'Work Types'
);

$args = array(
    'hierarchical' => true,
    'labels' => $labels,
    'show_ui' => true,
    'show_admin_column' => true,
    'query_var' => true,
    'rewrite' => array( 'slug' => 'work_type' ),
    'show_in_rest'       => true,
    'rest_base'          => 'work_type',
    'rest_controller_class' => 'WP_REST_Terms_Controller',      
);

register_taxonomy('work_type', array('work'), $args);
}
add_action( 'init' , 'rk_work_taxonomies' );

The issue that i have is that i cant find a way to get the Works filtered by Work Type using REST API v2

I read that after some wordpress update filter query was removed from REST API. So what is the proper way to do this now? Can you provide some example please?

Thank you in advance!

11

Ok, the solution was:

example.com/wp-json/wp/v2/works?work_type=10

It views the work_types as id's. You can view the id's in:

example.com/wp-json/wp/v2/works
4
  • to retrive by slug just add _slug at the end of category slug. work_type_slug={slug}
    – user254153
    Sep 1 '18 at 6:13
  • Adding _slug to the end of the param doesn't work for me. I'm using WordPress 4.9.8, REST API v2. Using the id works great though. If you want to get the single term from the REST API based on the slug, then you use slug={term_slug} eg: example.com/wp-json/wp/v2/works?slug=work-type-slug
    – Tim
    Oct 1 '18 at 13:49
  • 3
    In order for it to work, you need 'show_in_rest' => true, on register_taxonomy. May 20 '19 at 15:26
  • @ThatBrazilianGuy could you please point to some documentation about it? The technique doesn't seem to work with WP 5.3.2
    – jgangso
    Jan 2 '20 at 8:09
4

I installed the WP REST Filter plugin and then i'm able to filter post by category slug like:

http://www.example.com/wp-json/wp/v2/my_custom_posts?filter[my_custom_taxomony]=my_custom_taxonomy_slug

in your case:

http://www.example.com/wp-json/wp/v2/works?filter[work_type]=building
2
  • please mark as solution. ;)
    – DARKVIDE
    Feb 5 '20 at 12:38
  • I think use a plugin is not "the solution"
    – ZecKa
    Apr 29 '20 at 8:55
3

If you really need to use slug as url parameter you can add a custom filter, take a look to rest_{$this->post_type}_query hook

You can do something like that (post_type: work, taxonomy: work_type):

/**
 * Filter work post type by work_type slug
 *
 * @param array $args
 * @param WP_Rest_Rquest $request
 * @return array $args
 */
function filter_rest_work_query( $args, $request ) { 
    $params = $request->get_params(); 
    if(isset($params['work_type_slug'])){
        $args['tax_query'] = array(
            array(
                'taxonomy' => 'work_type',
                'field' => 'slug',
                'terms' => explode(',', $params['work_type_slug'])
            )
        );
    }
    return $args; 
}   
// add the filter 
add_filter( "rest_work_query", 'filter_rest_work_query', 10, 2 ); 

and then

https://example.com/wp-json/wp/v2/work?work_type_slug=slug01,slug02
1
  • 1
    Using this code will only work if there are no other taxonomies being passed in. (Setting $args['tax_query'] explicitly like this will overwrite any other values of the tax_query which may be set.) You will need to check to see if the array is defined, and extend it if so, rather than overwriting it. 2 days ago
-1

https://developer.wordpress.org/reference/functions/get_posts/

This link help

It includes get_posts() method Which is used to get post

Pass post type in argument

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.