0

I need help with this one. What I want to do is to display specific posts of the custom post type. Everything working fine, just I can't display more than 1(one) post. doesn't matter how much posts I select it always return to me just one. Here is a full code

<?php

$post_objects = get_sub_field('choose_blocks');
$string = "";
if( $post_objects ): ?>

    <?php foreach( $post_objects as $post_object):

        $post_id = $post_object->ID;

        $string .= $post_id.',';


    endforeach; ?>

    <?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
<?php endif; ?>

<?php $string =  rtrim($string, ','); ?>

 <?php
     $args = array(
        'post_type'=>'presentations',
        'post_status'=>'publish',
        'post__in' => array( $string )
    );
?>
 <?php // the query
    $all_presentations = new WP_Query($args); ?>

    <?php if ( $all_presentations->have_posts() ) : ?>

        <?php while ( $all_presentations->have_posts() ) : $all_presentations->the_post(); ?>

            <?php get_template_part( 'includes/content', get_post_format() ); ?>

        <?php endwhile; ?>
        <!-- end of the loop -->


        <?php wp_reset_postdata(); ?>

    <?php else : ?>
       <!-- do nothing -->
    <?php endif; ?>

<?php endif; ?>
1

now we are getting there.. you really need to try to explain, what your problem is, its not very clear in your question.

my assumption is, you tried this one, but it didn't work out:

foreach( $post_objects as $post_object):
  get_template_part( 'includes/content', get_post_format( $post_object->ID ) );
endforeach;

and here is why: you call the template, but the global $post is still set to the previous post/page. you need to set the global $post variable as you do it later on in your code ($all_presentations->the_post();). it works differently, when you do it in a foreach and not with WP_Query(), but you can set the global $post variable when you hand and object of that post to setup_postdata. when you do that, you can call all the other functions, like the_title() or the_content() and wp will grab it from the object, that you provided.

here is how:

<?php
 /**
   * when you call the_title() here, it will return the title of the current post/page (global $post defined by main query)
   */
if( $post_objects ):
  foreach( $post_objects as $post): //!!the variable MUST be called $post
    setup_postdata($post); //set new $post_object
    /**
      * now we switched global $post, so the_title() will return other the contents of your $post_object
      */
    get_template_part( 'includes/content', get_post_format() );
  endforeach;
  wp_reset_postdata(); //reset global $post
endif;
?>

you need to call setup_postdata()on your obect, to use get_post_format(). see the docs for more info on setup_postdata.

  • Thank you for your time. OBJECT doesn't contain post_format, that i really need for calling that specific template. Basically i use same php that you write, and that give me no result. – Stefan Aug 31 '18 at 8:31
  • that is correct, the object does not contain the post_format. but with the ID, that is inside $post, it will be able to get it.. foreach is running? do you get the ids when you simply run the_ID() in there..? – honk31 Aug 31 '18 at 16:49
  • see my answer once again, i edited it to be more clear about what is going on. if that is not your issue, create a new question and post the link here, since it tends to get confusing with too many comments and editing questions and ansers.. – honk31 Sep 3 '18 at 7:38
  • Definitely i will try your code when i go back to that project this month. This solution will help me a lot with future projects :-) – Stefan Sep 3 '18 at 7:59
-1

Here is a solution, i need to returns an array of string. It's not enough to pass string in array()

<?php $arr = explode(',', $string); ?>

Full code

<?php

$post_objects = get_sub_field('choose_blocks');
$string = "";
if( $post_objects ): ?>

    <?php foreach( $post_objects as $post_object):

        $post_id = $post_object->ID;

        $string .= $post_id.',';


    endforeach; ?>

    <?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
<?php endif; ?>

<?php $string =  rtrim($string, ','); ?>


// THIS IS THAT I NEED TO GET IT WORK
<?php $arr = explode(',', $string); ?>


 <?php
     $args = array(
        'post_type'=>'presentations',
        'post_status'=>'publish',
        'post__in' => array( $string )
    );
?>
 <?php // the query
    $all_presentations = new WP_Query($args); ?>

    <?php if ( $all_presentations->have_posts() ) : ?>

        <?php while ( $all_presentations->have_posts() ) : $all_presentations->the_post(); ?>

            <?php get_template_part( 'includes/content', get_post_format() ); ?>

        <?php endwhile; ?>
        <!-- end of the loop -->


        <?php wp_reset_postdata(); ?>

    <?php else : ?>
       <!-- do nothing -->
    <?php endif; ?>

<?php endif; ?>
  • Or instead of setting $string as a string variable, you can just make it an array. That way, you don't need to do the explode()-ing. :) See this (find all the $string on that page). And btw, in your code, you forgot to change the array( $string ) to $arr. – Sally CJ Aug 30 '18 at 10:35
  • 1
    This code doesn't make any sense. Why are you performing another query for posts that you already have? The posts you have are already in $post_objects, you don't need to do another WP_Query. – Jacob Peattie Aug 30 '18 at 10:43
  • I was actually going to ask the same question @JacobPeattie. Stefan, you could really just loop through the $post_objects you have and display the post anyway you like. There's the setup_postdata() function if you need to call the_content() or similar 'the_xxx'/'get_the_xxx' functions for the current post. – Sally CJ Aug 30 '18 at 10:50
  • @SallyCJ I do like this because $post_object don't have value in array about post_format. Also get_post_format() didn't work even if i pass ID of specific post. So i need to do this like this. If you know how to do this i will be more then grateful! – Stefan Aug 30 '18 at 18:56
  • @honk31 Thank you for your words. Everything is true what you said, but the OBJECT doesn't contain post_format, that i really need for calling that specific template – Stefan Aug 30 '18 at 19:00

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.