1

I need to search in Custom Post Type 'Case Studies' based on tags which is specified in Shortcode Parameters like

[casestudyhook tag="show-this-casestudy-onhome"]

so image | title | link of whichever case study has tag with "show-this-casestudy-onhome" will be displayed at shortcode

this is my current code in shortcode

<?php
function display_casestudy($atts) {

    $handlearray   =   shortcode_atts(
        array(
            'tag' => ''
        ),
    $atts );

    $cstag = get_post_meta( get_the_ID(), esc_html( $handlearray['tag']), true );

    $args = array(
        'post_type' => 'case_studies',
        'posts_per_page' => 1,
        'tax_query' => array(
            array(
                'tag'    => $cstag
            ),
        ),
    );

    $query = new WP_Query( $args );



    // The Loop
    while ( $query->have_posts() ) : $query->the_post();  
      $return_string .= '<li><a href="'. get_the_permalink() .'">'. get_the_title() .'</a></li>';
    endwhile;

    return '<ul>'. $return_string .'</ul>';
    
    

    wp_reset_query();
}    

function register_csshortcodes(){
   add_shortcode('casestudyhook', 'display_casestudy');
}

add_action( 'init', 'register_csshortcodes');

?>
3
  • What is all of the commented out code? Also, you don't initialize $return_string
    – Howard E
    Commented Nov 20, 2021 at 12:41
  • now? can you clarify more? Commented Nov 20, 2021 at 13:18
  • What is the taxonomy_type? What field are you matching? This is the big problem... the tax_query is wrong.
    – Howard E
    Commented Nov 20, 2021 at 13:56

1 Answer 1

1

This is an answer that depending on how you ultimately configure things - should get you where you want to go.

In your question, you had posts_per_page as 1. If you only want 1 result, then that's fine. If not... modify as shown.

You also didn't initialize the concatenated string $return_string and finally you put the reset after the return, so it had no effect.

function display_casestudy( $atts ) {

    $handlearray = shortcode_atts(
        array(
            'tag' => '',
        ),
        $atts
    );

    $cstag = sanitize_text_field( $handlearray['tag'] );

    $args = array(
        'post_type'      => 'case_studies',
        'posts_per_page' => -1, // Putting -1 allows all results.
        'tax_query'      => array(
            'relation' => 'AND',
            array(
                'taxonomy' => 'post_tag', // Use Your Taxonomy Type Here.
                'terms'    => $cstag,
                'field'    => 'slug', // This depends on how your're passing it in the $cstag.
            ),
        ),
    );

    $query         = new WP_Query( $args );
    $return_string = '';
    // The Loop.
    while ( $query->have_posts() ) :
        $query->the_post();
        $return_string .= '<li><a href="' . get_the_permalink() . '">' . get_the_title() . '</a></li>';
    endwhile;

    wp_reset_postdata(); // Put before Return otherwise it does nothing.
    
    return '<ul>' . $return_string . '</ul>';
}
1
  • Thank you so much sir.! You just saved my job.!! Thanks a lot. Commented Nov 20, 2021 at 14:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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