wp query – Custom Taxonomy in custom REST API search

Question

I am building my first theme where I use a custom search and I would like to include the ability to search by custom taxonomy.

Currently, the taxonomy term shows up in Postman but no posts with that taxonomy appear in the search results.

"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>."
            }
        }
    ],

My current setup has a CPT called “albums” and a custom taxonomy called “genres”. This taxonomy is assigned only to “albums”. Every album is assigned with the lowest taxonomy term in the hierarchy. For example: If my genres are Jazz > Bebop > Big Band, the album is checked with only “Big Band”. In my case: Pop > 80s

In functions.php, I registered a custom Rest API search:

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');

In the search-route.php, I defined the search query like this:

<?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;


}

I would like to add the ability to search the albums by its exact taxonomy term and its parent taxonomy.
For example: If an album is assigned to “Big Band” and the taxonomy hierarchy is Jazz > Bebop > Big Band, this album shall appear when searching for “Jazz”, or “Bebop”, or “Big Band”.

How do I achieve that?

I have tried to modify the search query by using the pre_get_posts filter but I am not quite sure how and where to include them.

0
Charalambos 4 weeks 2023-02-28T20:51:56-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse