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>
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 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'
);
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0