wp query – Taksonomi Kustom dalam pencarian REST API kustom

Saya sedang membangun tema pertama saya di mana saya menggunakan pencarian khusus dan saya ingin menyertakan kemampuan untuk mencari berdasarkan taksonomi khusus.

Saat ini, istilah taksonomi muncul di Postman tetapi tidak ada postingan dengan taksonomi tersebut yang muncul di hasil pencarian.

"albums": [
        {
            "albumTitle": "Aerobic Dance",
            "albumImage": "<img src=\"https://vebajefo.local/wp-content/uploads/2022/12/BLOOM001_AerobicDance_Cover-400x400.jpg\" />",
            "catalog_id": "BLOOM001 Aerobic Dance",
            "permalink": "https://vebajefo.local/albums/bloom001-aerobic-dance/",
            "postType": "albums",
            "albumGenres": {
                "genres": "Genres: <a href=\"https://vebajefo.local/genre/80s-pop/\">80s Pop</a>."
            }
        }
    ],

Pengaturan saya saat ini memiliki CPT yang disebut “album” dan taksonomi khusus yang disebut “genre”. Taksonomi ini ditugaskan hanya untuk “album”. Setiap album diberi istilah taksonomi terendah dalam hierarki. Misalnya: Jika genre saya adalah Jazz > Bebop > Big Band, album hanya akan dicentang dengan “Big Band”. Dalam kasus saya: Pop> 80-an

Di functions.php, saya mendaftarkan pencarian API Istirahat khusus:

require get_theme_file_path('/inc/search-route.php');



function aftersunsetmusic_custom_rest () {
    register_rest_field( 'post', 'authorName', 'albumGenres', array(
        'get_callback' => function() {return get_the_author() ;}
    ) );
}

add_action('rest_api_init', 'aftersunsetmusic_custom_rest');

Di search-route.php, saya mendefinisikan permintaan pencarian seperti ini:

<?php

add_action('rest_api_init', 'aftersunsetmusicRegisterSearch');

function aftersunsetmusicRegisterSearch() {
    register_rest_route( 'aftersunsetmusic/v1', 'search', array(
        'methods' => WP_REST_SERVER::READABLE,
        'callback' => 'aftersunsetmusicSearchResults',
        'permission_callback' => '__return_true',
    ) );
}

function aftersunsetmusicSearchResults ($data) {
    $mainQuery = new WP_Query(array(
        'post_type' => array('post', 'page', 'albums', 'artists'),
        's' => sanitize_text_field($data['term'])
    ));

    $results = array(
        'blog' => array(),
        'albums' => array(),
        'artists' => array(),
    );

    while($mainQuery->have_posts()) {
        $mainQuery->the_post();


        if (get_post_type() == 'post') {

            $description = null;
            if(has_excerpt()) {
                $description = get_the_excerpt();
            } else {
                $description = wp_trim_words( get_the_content(),18);
            }

            array_push($results ['blog'], array(
                'title' => get_the_title(),
                'permalink' => get_the_permalink(),
                'blogImage' => get_the_post_thumbnail(), 
                'authorName' => get_the_author(),
                'description' => $description,
            ));
        }

        if (get_post_type() == 'albums') {
            $relatedArtists = get_field('album_artists');

            if ($relatedArtists) {
                foreach($relatedArtists as $artist) {
                    array_push($results['artists'], array(
                        'artistName' => get_the_title($artist),
                        'permalink' => get_the_permalink($artist),
                        'image' => get_the_post_thumbnail_url( $artist, '' ),
                        'postType' => get_post_type($artist),
                    ));
                }
            }
            

            $albumtitle = get_field('album_title');

            array_push($results ['albums'], array(
                'albumTitle' => $albumtitle,
                'albumImage' => get_the_post_thumbnail(0,'medium'),
                'catalog_id' => get_the_title(),
                'permalink' => get_the_permalink(), 
                'postType' => get_post_type(),
                'albumGenres' => get_the_taxonomies( ),
            ));
        }
        

        if (get_post_type() == 'artists') {

            array_push($results['artists'], array(
                'artistName' => get_the_title(),
                'permalink' => get_the_permalink(),
                'image' => get_the_post_thumbnail_url( ),
                'postType' => get_post_type(),
            ));
        }
    }




    $results ['artists'] = array_values(
        array_unique($results['artists'], SORT_REGULAR));

    return $results;


}

Saya ingin menambahkan kemampuan untuk mencari album dengan istilah taksonomi yang tepat dan taksonomi induknya. Misalnya: Jika album ditetapkan ke “Big Band” dan hierarki taksonominya adalah Jazz > Bebop > Big Band, album ini akan muncul saat mencari “Jazz”, atau “Bebop”, atau “Big Band”.

Bagaimana saya mencapai itu?

Saya telah mencoba mengubah permintaan pencarian dengan menggunakan pre_get_posts filter tapi saya tidak yakin bagaimana dan di mana memasukkannya.

Leave a Reply

Your email address will not be published. Required fields are marked *