I defined a custom taxonomy in WordPress as below:
function content_selector() {
register_taxonomy(
'contentselector',
'post',
array(
'label' => __( 'Content Selector' ),
'rewrite' => array( 'slug' => 'cs' ),
'hierarchical' => true,
)
);
}
add_action( 'init' , 'content_selector' );
It shows up in new posts and values can be assigned, actually it seems working. But when I use following function for calling posts by this taxonomy, there is no success.
add_shortcode('rps', 'rpsf');
function rpsf() {
$args =[ 'posts_per_page' => 1, array(
'tax_query' => array(
array(
'taxonomy' => 'contentselector',
'field' => 'slug',
'terms' => 'one-of-the-assigned-terms')
))];
$query = new WP_Query( $args );
if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post();
ob_start(); ?>
<div class="rpst">
<a href="<?php the_permalink(); ?>"><span><?php the_title(); ?></span</a>
</div>
<?php endwhile; endif; wp_reset_postdata();
return ob_get_clean();
}
Did I make a mistake in defining taxonomy or in calling posts?
It is easier to debug your code if you properly format it, as well as staying consistent regarding the notation you use (for example, []
vs array()
).
After 'cleaning' up your $args
definition, it's clear to see that it is incorrectly structured:
$args = array(
'posts_per_page' => 1,
array(
'tax_query' => array(
array(
'taxonomy' => 'contentselector',
'field' => 'slug',
'terms' => 'one-of-the-assigned-terms'
)
)
)
);
It should look more like this:
$args = array(
'posts_per_page' => 1,
'tax_query' => array(
array(
'taxonomy' => 'contentselector',
'field' => 'slug',
'terms' => 'one-of-the-assigned-terms'
)
)
);