0

I have a script , show post by id , and id post from array here my code :

if ( have_posts() ) : while ( have_posts() ) : the_post();
$f = cherry_get_option('blog-featured-post',false);
foreach ($f as $fs ) {
$q = new WP_Query( array(
'post__in' => array($fs),
'orderby' => 'date',
'order' => 'asc'
 ) );}

 if ( $q->have_posts() ) : while ( $q->have_posts() ) : $q->the_post();
 //show post 
 the_title();
 endwhile; endif;
 endwhile; else:
 echo "no post found";
 endif;
 wp_reset_query();

nah from that code $fswill output id post what will display , i am try to vardump($fs) it work fine (the output is string(4) "1254" string(4) "1310") but in post_in just first id can read (string(4) "1254") I dont know what happen with my code , if anyone can help , i will happy

3
  • What is your question exactly?
    – Pim
    Commented Sep 12, 2018 at 14:50
  • actually i just ask why , my code only just show 1 post , but actually if my code use vardump($fs) they will show 2 id
    – jack
    Commented Sep 12, 2018 at 15:14
  • See my answer, if you need extra help, please update your question.
    – Pim
    Commented Sep 12, 2018 at 15:18

1 Answer 1

0

You are getting the option "blog-featured-post", which apparently returns two posts. Then, for each of these posts, you are making a query.

Only the second one is actually used, since your if ( $q->have_posts() ) is outside of the loop. This query only has one post, since you use the post ID with the post__in option.

So this is expected behavior.

3
  • ohh ya i see , so if ( $q->have_posts() ) is on the oustside my loop on foreach , and i just move if ( $q->have_posts() )into foreach right?
    – jack
    Commented Sep 12, 2018 at 15:29
  • Yeah you could do that... basically move the bracket of the foreach loop to the end of the file.
    – Pim
    Commented Sep 12, 2018 at 15:35
  • 1
    xoxo , yeah its works , thankyou buddy
    – jack
    Commented Sep 12, 2018 at 15:54

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.