简体   繁体   中英

Filter custom post type shortcode output by category WordPress

I've created a custom post type for a "Clubs" section on a site, and I need to filter those clubs by their categories, using a shortcode.

Not entirely sure why the shortcode isn't working to filter the clubs by category (it does display all clubs).

For example, if one of the categories is AV Clubs, I've tried using both the title [club category="AV Clubs"] and the slug [club category="av-clubs"].

// Shortcode
function shortcode_club_viusu( $atts ) {
//default arguments
$args = shortcode_atts( array(
    'cat' => null,
    'category' => '',
    'order' => 'DESC',
    'orderby' => 'date',
    'posts_per_page'=> -1,
    'post_type' => 'club'
), $atts );

$content = "";

// The Query
$the_query = new WP_Query( $args );

// The Loop
if ( $the_query->have_posts() ) {
    //beginning of the FAQ element
    $content .= '<ul class="collapsible" data-collapsible="accordion">';

    while ( $the_query->have_posts() ) {
        $the_query->the_post();

        $content .= '<li>';
        $content .= '<div class="collapsible-header">'.get_the_title().'</div>';
        $content .= '<div class="collapsible-body"><p>'.get_the_content().'</p></div>';
        $content .= '</li>';
    }

    //closing the FAQ element
    $content .= '</ul>';

} else {
    $content .= "No Clubs found";
}
/* Restore original Post Data */
wp_reset_postdata();

return $content;
}
add_shortcode( 'club', 'shortcode_club_viusu' );

Turns out I was missing one line from the list above:

'cat' => null,
'club_tax' => '',
'category' => '',
'order' => 'DESC',
'orderby' => 'date',
'posts_per_page'=> -1,
'post_type' => 'club'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM