简体   繁体   中英

How to place ACF get_field inside an array post__in Wordpress

I have the code below but it's just posting one post or showing 1 post but if i add the post id inside the array it working, but if i add the $tguides_value its just showing one item

<?php
  $tguides_value = get_field('recommended_tguides');

         $args = array(

            'post_type' => 'travelguides',
            'posts_per_page' => 3,
            'post__in' => array($tguides_value),

Question: How would I place the ACF get_field inside the array post__in in Wordpress

Try using as follows,

$tguides_value = get_field('recommended_tguides', $postID);

         $args = array(

            'post_type' => 'travelguides',
            'posts_per_page' => 3,
            'post__in' => array($tguides_value),

You need to turn your text field return of a string into an array by exploding at the comma, you can't just pass your text string into it as an array.

<?php
  $tguides_value = get_field('recommended_tguides');
  $tguides_array = explode (",", $tguides_value);

         $args = array(

            'post_type' => 'travelguides',
            'posts_per_page' => 3,
            'post__in' => $tguides_array,

I figured this out thanks to your help Daniel Vickers, but it should be implode instead of explode .

Here is what I ended up doing for myself in my particular use and it worked:

    <?php
    $values = get_field('homepage_magic_test');
    $valuett = implode(",",$values);
    ?>
    <?php get_jig(array(
        'img_alt_field' => 'alternate',
        'title_field' => 'title',
        'recent_posts' => 'yes',
        'recents_title_override' => 'custom_title',
        'recents_post_type' => 'page',
        'post_ids' => $valuett
    ));
        ?>

So I assume the following would work for the code above.

$tguides_value = get_field('recommended_tguides');
$tguides_imp = implode(",",$tguides_value);

         $args = array(

            'post_type' => 'travelguides',
            'posts_per_page' => 3,
            'post__in' => $tguides_imp,

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM