0

Hi I have developed a shortcode that lists posts from a custom post type depending upon taxonomy terms. But I am having an issue. It's displaying all posts from all terms instead of the terms being called.
The code is below:

function course_listings( $atts ) {

// Attributes
extract( shortcode_atts(
    array(
        'course_category' => '',
    ), $atts )
);

// Code

$args = array(
    'post_type' => 'modules',
    'tax_query' => array(
            'taxonomy' => 'courses',
            'field' => 'slug',
             'terms' => $course_category
        )
);

$mod_query = new WP_Query( $args );

$output='<div class="course-listings">';

    while ( $mod_query->have_posts() ) {

        $mod_query->the_post();

        $output .='<ul>';
        $output .='<li>' .get_the_title(). '</li>';
        $output .='</ul>';

    }

    wp_reset_postdata();
    $output .='</div>';

    return $output;
}
add_shortcode( 'course', 'course_listings' );

Any help will be highly appreciated.

Thanks.

0

Two issues here

  • Never use extract(). It has been removed from core functions, that should tell you a lot. For more info, check this trac ticket #22400

  • Your tax_query is incorrect, it should be an array of an array, yours is just an array

Your shortcode should look something like this

function course_listings( $atts ) 
{
    // Attributes
    $defaults = shortcode_atts( array(
        'course_category' => '',
    ), $atts );

    // Code

    $args = array(
        'post_type' => 'modules',
        'tax_query' => array(
            array(
                'taxonomy' => 'courses',
                'field'    => 'slug',
                 'terms'   => $defaults['course_category'],
            ),   
        ),
    );

    $mod_query = new WP_Query( $args );

    $output='<div class="course-listings">';

    while ( $mod_query->have_posts() ) {

        $mod_query->the_post();

        $output .='<ul>';
        $output .='<li>' .get_the_title(). '</li>';
        $output .='</ul>';

    }

    wp_reset_postdata();
    $output .='</div>';

    return $output;
}
add_shortcode( 'course', 'course_listings' );

Just a few tip, you would most probably first want to check if you have a term entered before executing the query

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.