开发者

Trying to query multiple posts with WP_Query

开发者 https://www.devze.com 2023-03-05 07:49 出处:网络
I have a custom field in my posts so the admin can enter a list of specific post ids they want included for the related content section in the sidebar. I am trying to insert the variable into my wp_qu

I have a custom field in my posts so the admin can enter a list of specific post ids they want included for the related content section in the sidebar. I am trying to insert the variable into my wp_query_object but it is only querying the first item.

When I echo the $related_vids variable it displays the ids I entered into the custom field: 45,14,10.

Any help with what I'm doing wrong would be awesome. I feel like I'm close but I've hit the wall.

<?php $related_vids = get_post_meta($post->ID, '_simple_fields_fieldGroupID_3_fieldID_2_numInSet_0', true);
$the_query = new WP_Query( array( 'post__in' => array( $related_vids ) ) );
while ( $the_query->have_posts() ) : $the_query->the_post(); ?>

<p><a href="<?php the_permalink(); ?&开发者_运维百科gt;"><?php the_title(); ?></a></p>

<?php endwhile; wp_reset_postdata(); ?>


The problem is that array( $related_vids ) actually creates an array that contains 1 element: ["45,14,10"] and not an array that contains 3 elements: [45, 14, 10]

What you need is explode( ', ', $related_vids )

0

精彩评论

暂无评论...
验证码 换一张
取 消