0

Today the shortcode I created below is working normally if I pass only one value, however, now I am trying this shortcode so that the attribute can receive multiple values. The idea is for the shortcode to look like this: [youlike id_post = '6059, 76912']. I tried to add it like this in the post, but it didn't work.

Below the function I have

function youCanLike( $atts ) {
    ob_start();
    
    $value = shortcode_atts( array(
        'id_post' => '',
    ), $atts );

    // Check if href has a value before we continue to eliminate bugs
    if ( !$value ['id_post'] )
        return false;

    $args = array(
        'post__in' => array( esc_attr( $value['id_post'] ) ),
        'orderby' => 'date',
        'order' => 'DESC'
    );

    get_template_part( 'global-templates/blocks/block', 'you-can-like', $args );

    return ob_get_clean();   
} 
add_shortcode( 'youlike', 'youCanLike' );

And this is where the get_template_part file is calling. i.e. block-you-can-like.php

<div class="content-box--primary">
    <h2 class="content-box--primary__head">You may also be interested:</h2>
    
        <?php
        
        $args = wp_parse_args( $args );

        $query = new WP_Query( $args );

        if( $query->have_posts() ):
            while( $query->have_posts() ): $query->the_post();

        ?>

    <article class="content-box--primary__content">
        <a href="<?php the_permalink(); ?>" class="content-box--primary__link">
            <h3 class="content-box--primary__title"><?php the_title(); ?></h3>
            <span class="content-box--primary__more">Read More</span>   
        </a>
    </article>

    <?php
        endwhile;
        wp_reset_postdata();
        endif;                                  
    ?>

</div>

1 Answer 1

1

post__in takes an array of post IDs. What you are passing is a string into an array

Try to change code a bit e.g.

   // Check if href has a value before we continue to eliminate bugs
    if ( !$value ['id_post'] )
        return false;


    $post_ids_array = explode(',', $value['id_post']);

    $args = array(
        'post__in' => $post_ids_array,
        'orderby' => 'date',
        'order' => 'DESC'
    );
1
  • Excellent Awais! Thanks a lot! Commented Dec 3, 2020 at 1:03

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.