• Resolved greencode

    (@greencode)


    I have the following code that lists posts on a page:

    <ul>
     <?php
     global $post;
     $myposts = get_posts(array('post__in' => array(46,30)));
     foreach($myposts as $post) :
       setup_postdata($post);
     ?>
        <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
     <?php endforeach; ?>
     </ul>

    I would like the post id’s in the array section to be brought in from a custom field called “featured-posts”. So the site admin can add the id’s of the posts he would like to feature on the page. I’m certain this can be done but I’m unsure how to lay out the code. Any help would be greatly appreciated.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Use the ‘meta_key’ => ‘featured-posts’, and ‘meta-value’ => ‘yes’ in your get_posts function call. This is assuming that you put ‘yes’ for the custom field of the posts you will display as featured posts.

    Thread Starter greencode

    (@greencode)

    Hi, Sorry I don’t think I explained clearly enough. I don’t want each post marked with “featured-posts” in the custom field to appear on the page.

    Basically if you could write the code then what I would want is this:

    $myposts = get_posts(array('post__in' => array(<?php echo get_post_meta($post->ID, 'custom-field-name', true); ?>)));

    I know you can’t write it like that but I’m looking for the content of my custom field to be brought into the array.

    Thread Starter greencode

    (@greencode)

    Ah ha, I’ve done it! Here’s the code should anyone else require it:

    <ul>
     <?php
     $key = get_post_meta($post->ID, 'custom-field-name', true);
     global $post;
     $myposts = get_posts(array('post__in' => array($key)));
     foreach($myposts as $post) :
       setup_postdata($post);
     ?>
        <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
     <?php endforeach; ?>
     </ul>
    Thread Starter greencode

    (@greencode)

    Umm, actually I’m not sure that does work. If I add more than one post id in the custom field i.e. 26,3,589,8 – it will only display the first one and not all posts.

    Thread Starter greencode

    (@greencode)

    This has since been resolved in the following post: http://wordpress.org/support/topic/425472

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘get_posts and custom field question’ is closed to new replies.