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; ?>

2 Answers 2

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.

4
  • 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
    Commented Aug 31, 2018 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
    Commented Aug 31, 2018 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
    Commented Sep 3, 2018 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
    Commented Sep 3, 2018 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; ?>
10
  • 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
    Commented Aug 30, 2018 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. Commented Aug 30, 2018 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
    Commented Aug 30, 2018 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
    Commented Aug 30, 2018 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
    Commented Aug 30, 2018 at 19:00

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.