web-dev-qa-db-fra.com

Comment filtrer le type de message personnalisé par taxonomie personnalisée dans la dernière API Wordpress RESTful?

J'ai un type de message personnalisé appelé clock et une taxonomie appelée clock_category sur mon site Web. Il fonctionne bien et toutes les horloges sont accessibles à partir du REST endpoint https://myapp.dev/wp-json/wp/v2/clock

Mais je ne sais pas comment les filtrer par la catégorie Horloge, dis que je veux obtenir toutes les horloges qui sont en bois

J'ai essayé différentes URL mais aucune ne semble fonctionner/ https://myapp.dev/wp-json/wp/v2/clock?clock_category=woodhttps://myapp.dev/wp-json/wp/v2/clock? clock_category = 10

et beaucoup plus. Tout ce que j'ai, c'est l'ensemble des résultats.

Voici le code

<?php
add_action( 'init', 'clock' );

function clock() {
    $labels = array(
        "name" => __( 'Clock', 'mywp' ),
        "singular_name" => __( 'clock', 'mywp' ),
        "menu_name" => __( 'Clock', 'mywp' ),
        "all_items" => __( 'All Clock', 'mywp' ),
        "add_new" => __( 'Add New Clock', 'mywp' ),
        "add_new_item" => __( 'Add New Clock', 'mywp' ),
        "edit_item" => __( 'Edit Clock', 'mywp' ),
        "new_item" => __( 'New Clock', 'mywp' ),
        "view_item" => __( 'View Clock', 'mywp' ),
        "search_items" => __( 'Search Clock', 'mywp' ),
        "not_found" => __( 'No Clock Found', 'mywp' ),
        "not_found_in_trash" => __( 'No Clock found in trash', 'mywp' ),
        "parent_item_colon" => __( 'Parent Clock', 'mywp' ),
        "featured_image" => __( 'Feature Image for Clock', 'mywp' ),
        "set_featured_image" => __( 'Set featured Clock image', 'mywp' ),
        "remove_featured_image" => __( 'Remove featured image for Clock', 'mywp' ),
        "use_featured_image" => __( 'Use featured image for Clock', 'mywp' ),
        "archives" => __( 'Clock archives', 'mywp' ),
        "insert_into_item" => __( 'Insert into Clock', 'mywp' ),
        "uploaded_to_this_item" => __( 'Uploaded to this Clock', 'mywp' ),
        "items_list_navigation" => __( 'Clock list navigation', 'mywp' ),
        "items_list" => __( 'Clock List', 'mywp' ),
        "parent_item_colon" => __( 'Parent Clock', 'mywp' ),
        );

$args = array(
    "label" => __( 'Clock', 'mywp' ),
    "labels" => $labels,
    "description" => "Clock",
    "public" => true,
    "publicly_queryable" => true,
    "show_ui" => true,
    "show_in_rest" => true,
    "rest_base" => "clock",
    "has_archive" => false,
    "show_in_menu" => true,
            "exclude_from_search" => false,
    "capability_type" => "post",
    "map_meta_cap" => true,
    "hierarchical" => false,
    "rewrite" => array( "slug" => "clock", "with_front" => true ),
    "query_var" => true,

    "supports" => array( "title", "editor", "thumbnail", "custom-fields", "page-attributes", "post-formats" ),      
    "taxonomies" => array( ),
        );
register_post_type( "clock", $args );

}

add_action( 'init', 'create_clock_tax' );

function create_clock_tax() {
    register_taxonomy(
        'clock_category',
        'clock',
        array(
            'label' => __( 'Clock Category' ),
            'rewrite' => array( 'slug' => 'clock_category' ),
            'hierarchical' => true,
        )
    );
}

J'espère que quelqu'un m'aide à ce sujet. J'essaie de résoudre ce problème depuis 5 heures et rien ne semble fonctionner.

3
user3407278

Nous pouvons vérifier les arguments du /wp/v2/clock endpoint ici:

https://myapp.dev/wp-json/wp/v2/

Si nous ajoutons l'argument 'show_in_rest' => true,, lors de l'enregistrement de la taxonomie personnalisée clock_category, le support de cet argument GET/POST sera ajouté:

clock_category: {
    required: false,
    default: [ ],
    description: "Limit result set to all items that have the 
        specified term assigned in the clock_category taxonomy.",
    type: "array",
    items: {
        type: "integer"
    }
},

et cet argument GET:

clock_category_exclude: {
    required: false,
    default: [ ],
    description: "Limit result set to all items except those that have the 
        specified term assigned in the clock_category taxonomy.",
    type: "array",
    items: {
        type: "integer"
    }
}

Cela ajoutera également le point final /wp/v2/clock_category.

Donc, pour filtrer les messages d'horloge par un seul ID de catégorie d'horloge:

https://myapp.dev/wp-json/wp/v2/clock?clock_category=10

ou par plusieurs ID de catégorie d'horloge:

https://myapp.dev/wp-json/wp/v2/clock?clock_category=10,11,12

Pour exclure d'un ID de catégorie unique:

https://myapp.dev/wp-json/wp/v2/clock?clock_category_exclude=13

ou exclure de plusieurs ID de catégorie d'horloge:

https://myapp.dev/wp-json/wp/v2/clock?clock_category_exclude=13,14
2
birgire