I have the following function written, the issue is when I set the category in my shortcode it ignores it and shows all posts from the custom post type. So it only sort of works.
/*--------------------------------------------------------------
## Resources Shortcode
--------------------------------------------------------------*/
function resources_query( $atts ) {
extract(shortcode_atts(array(
'category' => '',
'per_page' => -1,
'orderby' => 'date',
'order' => 'ASC'
), $atts));
$tax_query = array(
'taxonomy' => 'resources_categories',
'field' => 'slug',
'terms' => array( esc_attr($category) ),
'operator' => 'IN',
);
$args = array(
'post_type' => 'resources',
'post_status' => 'publish',
'posts_per_page' => $per_page,
'orderby' => $orderby,
'order' => $order,
'tax_query' => $tax_query
);
$resources_query = new WP_Query( $args );
if ( $resources_query->have_posts() ) :
$html_out = '<div class="fg-row row flex-row">';
while ( $resources_query->have_posts() ) :
$resources_query->the_post();
$title = get_the_title();
$content = get_the_excerpt();
$pdf = get_field( "download_pdf" );
// Do stuff with each post here
$html_out .= '<div class="fg-col col-xs-12 col-md-4 fg-text-dark"><section class="icon-box-v3 light-box text-left fg-text-dark"><i class="icon-box-v3-icons ff-font-et-line icon-document fg-text-dark ffb-icon-1"></i>';
$html_out .= '<h3 class="icon-box-v3-title fg-text-dark ffb-title-2">' . $title . '</h3><p class="icon-box-v3-title-paragraph fg-text-dark ffb-description-3">' . $content . '</p>';
if( $pdf ):
$html_out .= '<a href="' . $pdf . '" class="ffb-block-button-1-0 ffb-btn ffb-btn-v1 ffb-btn-link btn-base-brd-slide btn-slide btn-base-md btn-w-auto fg-text-dark ffb-button1-1 btn-light-box" target="_blank"><span class="btn-text">Download PDF</span></a>';
endif;
$html_out .= '</section></div>';
endwhile;
$html_out .= '</div>';
else : // No results
$html_out = "No Resources Found.";
endif;
wp_reset_query();
return $html_out;
}
add_shortcode( 'show_resources', 'resources_query' );
Here's what my shortcode actually looks like [show_resources category="post-surgery-information" per_page="-1"]
. I am not sure why it would ignore it and show all posts. I looked at other Stack articles to help me with setting this up.