Milan Poznan
Milan Poznan

Reputation: 307

WordPress Rest API filter by id or slug name

I am stuck with filtering taxonomy in my WordPress. Firstly i have this in function.php -> I made custom post types and custom taxonomies:

function create_post_types() {
register_post_type( 'skole',
    array(
        'labels' => array(
            'name' => _( 'Skole' ),
            'singular_name' => _( 'Skola' )
        ),
        'public' => true,
        'menu_icon' => 'dashicons-welcome-learn-more',
        'has_archive' => true,
        'show_in_rest' => true,
        'rest_base' => 'skole',
        'rest_controler_class' => 'WP_REST_Post_Controller'

    )
);
}
add_action( 'init', 'create_post_types');

function create_custom_taxonomies() {
register_taxonomy(
    'tip_skole',
    'skole',
    array(
        'label' => 'Tip skole',
        'hierarchical' => true,
        'show_in_rest'       => true,
        'rest_base'          => 'tip_skole',
        'rest_controller_class' => 'WP_REST_Terms_Controller',
    )
);
add_action( 'init', 'create_custom_taxonomies');

I have WP REST API plugin installed, and now I made .js file for this:

 var test = jQuery('#test');
 if (test) {
 test.on('click', function() {
    var ourRequest = new XMLHttpRequest();
    ourRequest.open('GET', 'http://localhost/skola/wp-json/wp/v2/tip_skole/');
    ourRequest.onload = function () {
        if (ourRequest.status >= 200 && ourRequest.status < 400) {
            var data = JSON.parse(ourRequest.responseText);
            console.log(data);
            createHtml(data);
        } else {
            console.log("We conected to the server, but error");
        }
    }
    ourRequest.onerror = function() {
        console.log('Connection error');
    }
    ourRequest.send();
});
 }
function createHtml(postData) {
var ourHTMLString = '';
for (var i = 0; i < postData.length; i++) {
    ourHTMLString += '<a href="' +postData[i].link +  '"><h2>' + postData[i].name + '</h2></a>';
    console.log(postData);
}
var x = jQuery('.skole__single');
// x.innerHTML = ourHTMLString;
jQuery('.skole__single').append(ourHTMLString);

console.log(ourHTMLString);

 }

So in this .js file i get right result, but when I want to see all posts in this taxonomy I don't know how. So in taxonomy 'tip_skole' i have "A", "B", "C" and "D". So here is what I tried:

http://localhost/skola/wp-json/wp/v2/tip_skole?filter[id]=2
http://localhost/skola/wp-json/wp/v2/tip_skole?id=2
http://localhost/skola/wp-json/wp/v2/tip_skole?[name]=A
http://localhost/skola/wp-json/wp/v2/tip_skole?filter[name]=A
http://localhost/skola/wp-json/wp/v2/tip_skole?filter[slug]=A

Any help?

Upvotes: 2

Views: 5096

Answers (2)

Michel Moraes
Michel Moraes

Reputation: 556

In order to filter by id you can simply do this way:

http://localhost/skola/wp-json/wp/v2/tip_skole/2

Where tip_skole is the taxonomy and "2" is the id of the taxonomy.

Upvotes: 1

Milan Poznan
Milan Poznan

Reputation: 307

Firstly WordPress does not support filter in 4.7+ version, so I get https://github.com/WP-API/rest-filter plugin, and than what i need is

http://localhost/skola/wp-json/wp/v2/skole/?filter[tip_skole]=skole-stranih-jezika

So it get my custom post type and filter it by taxonomy slug! Hope that this can help to someone!

Upvotes: 3

Related Questions