0

I am having problems with post__in shortcode atts. It does not show anything. I think the problem is that post__in must be an array but the shortcode ID returns string.

function courses_shortcode($atts) {

 extract(shortcode_atts( array(
    'limit' => 16,
    'id' => array(592, 862, 418, 491, 1227, 1015, 847, 738, 541, 1186, 664, 695, 785),

), $atts ));


  $q_courses = new WP_Query( array(
    'post_type' => array('course'),
    'posts_per_page'  => $limit,
    'post__in' => $id,
    'orderby'    => 'post__in',

));
add_shortcode('video-course', 'courses_shortcode');

The problem is that if I do [video-course limit=8 id="418, 1186"] does not show anything. Limit is working but the ID is not.

0

Correct. post__in requires an array but the id parameter in [video-course limit=8 id="418, 1186"] is passed in as a string, so in your shortcode, you would have to parse it into an array in order to use it:

if ( !is_array($id) ) {
    // If $id is not an array (not your default value in your example) 
    // then simultaneously parse it into an array and trim white space:
    $id = array_map('trim', explode(',', $id));
}

Then you can use:

'post__in' => $id
  • Thanks. If I do return $id; shows only Array. However, my posts are still not displayed? – Alexander Georgiev Jul 13 '16 at 13:18
  • @AlexanderGeorgiev You wouldn't want to return the $id to test it, you can't echo an array as-is, I would suggest a print_r() or var_dump() to check the contents of the array and make sure your function is both picking up the id parameter correctly and parsing it into an array correctly. You also need to actually do something with the $q_courses variable to see the results, obviously :) Can you include a paste bin or something to show updated code? – Tammy Shipps Jul 13 '16 at 13:24
  • pastebin.com/Q6wRpA73 – Alexander Georgiev Jul 13 '16 at 13:25
  • @AlexanderGeorgiev You need to parse the id parameter BEFORE the WP_Query call, or it can't use the array :) See: pastebin.com/K9FqvY27 – Tammy Shipps Jul 13 '16 at 13:45
  • @AlexanderGeorgiev No problem! – Tammy Shipps Jul 13 '16 at 13:57

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.