zapytanie wp — niestandardowa taksonomia w niestandardowym wyszukiwaniu interfejsu API REST
Tworzę swój pierwszy motyw, w którym korzystam z wyszukiwania niestandardowego i chciałbym włączyć możliwość wyszukiwania według niestandardowej taksonomii.
Obecnie termin taksonomii pojawia się w Postman, ale w wynikach wyszukiwania nie pojawiają się żadne posty z tą taksonomią.
"albums": [
{
"albumTitle": "Aerobic Dance",
"albumImage": "<img src=\" />",
"catalog_id": "BLOOM001 Aerobic Dance",
"permalink": "
"postType": "albums",
"albumGenres": {
"genres": "Genres: <a href=\" Pop</a>."
}
}
],
Moja obecna konfiguracja ma CPT o nazwie „albumy” i niestandardową taksonomię o nazwie „gatunki”. Ta taksonomia jest przypisana tylko do „albumów”. Każdemu albumowi przypisany jest najniższy termin taksonomii w hierarchii. Na przykład: jeśli moje gatunki to Jazz > Bebop > Big Band, album jest sprawdzany tylko z „Big Band”. W moim przypadku: Pop > lata 80
W functions.php zarejestrowałem niestandardowe wyszukiwanie Rest API:
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');
W search-route.php zdefiniowałem zapytanie wyszukiwania w następujący sposób:
<?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;
}
Chciałbym dodać możliwość wyszukiwania albumów według dokładnego terminu taksonomii i taksonomii nadrzędnej. Na przykład: jeśli album jest przypisany do „Big Band”, a hierarchia taksonomii to Jazz > Bebop > Big Band, ten album pojawi się podczas wyszukiwania „Jazz”, „Bebop” lub „Big Band”.
Jak to osiągnąć?
Próbowałem zmodyfikować zapytanie wyszukiwania, używając metody pre_get_posts filter
ale nie jestem do końca pewien, jak i gdzie je uwzględnić.