Pass custom field value to get_posts query
-
I have the following:
<ul> <?php $key = get_post_meta($post->ID, 'custom-field-name', true); global $post; $myposts = get_posts(array('post__in' => array("$key"), 'posts_per_page' => -1)); foreach($myposts as $post) : setup_postdata($post); ?> <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li> <?php endforeach; ?> </ul>so that whatever value is in the specified custom field is then passed through to the get_posts query. This works well if there’s only one number but if I add more than one it still only shows the first post e.g. array(1) – will show post with ID of 1. array(1,2,3) – will still only show post with ID of 1 and not all three as specified.
If I type these in directly to the code then everything works and shows perfectly but it’s when I pass it through the custom field.
Any ideas?
Viewing 4 replies - 1 through 4 (of 4 total)
Viewing 4 replies - 1 through 4 (of 4 total)
The topic ‘Pass custom field value to get_posts query’ is closed to new replies.