WordPress – Taxonomy filter in custom post type shortcode

I’m trying to query posts from CPT with a given taxonomy called ‘category’.

Custom post type for FAQ

function create_post_type_gon_cpt_faq() {
register_post_type( ‘gon_cpt_faq’,
array(
‘labels’ => array(
‘name’ => __( ‘FAQs’ ),
‘singular_name’ => __( ‘FAQ’ )
),
‘public’ => true,
‘has_archive’ => false,
‘menu_position’ => 5,
‘taxonomies’ => array(‘post_tag’),
‘capability_type’ => ‘post’,
‘rewrite_slug’ => ‘faq’,
‘supports’ => array(‘title’, ‘editor’,’page-attributes’)
)
);

}
add_action( ‘init’, ‘create_post_type_gon_cpt_faq’ );

Shortcode declaration

add_shortcode( ‘gon-faq-display’, ‘gon_faq_display_function’ );

function gon_faq_display_function($atts, $content = null){
extract(shortcode_atts(array(), $atts));

$args = array(
‘post_type’ => ‘gon_cpt_faq’,
‘tax_query’ => array(
array(
‘taxonomy’ => ‘post_tag’,
‘field’ => ‘slug’,
‘terms’ => ‘category’,
),
),
);
$faq_string = ”;
ob_start();
$query = new WP_Query( $args );
if( $query->have_posts() ){
while( $query->have_posts() ){
$query->the_post();?>
<div class=”gon-faq faq-pair”>
<a style=”text-decoration:none;” href=”#”>
<h2 class=”entry-title faq-title faq-question”>
<span class=”plus”>+</span><span class=”minus”>-</span> <?php the_title();?></h2>
</a>
<div class=”faq-answer”><?php echo wpautop(get_the_content());?></div>
</div>
<?php }
}
wp_reset_postdata();
$faq_string = ob_get_contents();
ob_end_clean();
return $faq_string;
}

Tags can be added in wp-admin, but with [gon-faq-display category=”1″] shows nothing.

Where is my mistake?
Thank you!

Read more here:: WordPress – Taxonomy filter in custom post type shortcode

Leave a Reply

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