Ich habe eine benutzerdefinierte Taxonomie, die als Alben bezeichnet wird.
Ich muss in der Lage sein, den Taxonomie-Begriffstitel als Text zu durchsuchen. Dies ist offensichtlich nicht die Standard-Suche WP. Ich frage mich nur, wie ich das am besten angehen würde.
Angenommen, es gibt ein Album namens "Football Hits",
Ich fange an, foot zu tippen und suche danach. Alles, was ich brauche, um zu erscheinen, ist der Albumtitel und der Permalink.
Vielen Dank!
// We get a list taxonomies on the search box
function get_tax_by_search($search_text){
$args = array(
'taxonomy' => array( 'my_tax' ), // taxonomy name
'orderby' => 'id',
'order' => 'ASC',
'hide_empty' => true,
'fields' => 'all',
'name__like' => $search_text
);
$terms = get_terms( $args );
$count = count($terms);
if($count > 0){
echo "<ul>";
foreach ($terms as $term) {
echo "<li><a href='".get_term_link( $term )."'>".$term->name."</a></li>";
}
echo "</ul>";
}
}
// sample
get_tax_by_search('Foo');
Sie können also definitiv nach Taxonomietiteln suchen - benutzerdefiniert oder auf andere Weise. Die Antwort wird im " tax_query " Teil von WP_Query sein. Hier ist ein Beispiel aus dem Codex, angepasst an Ihre Bedürfnisse:
<ul>
<?php
global $post;
$album_title = $_GET['album-title'];
$args = array(
'post_type' => 'post',
'posts_per_page' => 5,
'tax_query' => array( // NOTE: array of arrays!
array(
'taxonomy' => 'albums',
'field' => 'name',
'terms' => $album_title
)
)
);
$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post ); ?>
<li>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
</li>
<?php endforeach;
wp_reset_postdata();?>
</ul>
UPDATE
Ich habe das nicht getestet, aber theoretisch denke ich, dass es funktionieren könnte. So passen Sie zu allem, was "foot" enthält:
<ul>
<?php
global $post;
$album_title = $_GET['album-title']; // say the user entered 'foot'
$args = array(
'post_type' => 'post',
'posts_per_page' => 5,
'tax_query' => array( // NOTE: array of arrays!
array(
'taxonomy' => 'albums',
'field' => 'name',
'terms' => $album_title,
'operator' => 'LIKE'
)
)
);
$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post ); ?>
<li>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
</li>
<?php endforeach;
wp_reset_postdata();?>
</ul>
Hoffentlich hilft das!