Title: get_posts and custom fields
Last modified: August 19, 2016

---

# get_posts and custom fields

 *  [wmhunter](https://wordpress.org/support/users/wmhunter/)
 * (@wmhunter)
 * [15 years, 6 months ago](https://wordpress.org/support/topic/get_posts-and-custom-fields-1/)
 * I’m trying to make a page in a WordPress installation that shows the page’s content,
   then lists a few featured posts (in a “Featured” category) at the bottom. Each
   of the featured posts should have an image to go with it, defined by a custom
   field.
 * I took my page.php and added this at the bottom:
 * `\`<?php
    global $post; $myposts = get_posts('numberposts=2&category=26'); foreach(
   $myposts as $post): setup_postdata($post); ?> <div class="feat"> <?php getFeatImg();?
   > <p>[Post excerpt goes here]</p> </div> <?php endforeach; ?>\`
 * The getFeatImg() function does this:
 * `\`global $post;
    $filename = get_post_meta($post->ID, 'feat-img', true); if (
   $filename) { echo '<img src="/post-images/' . $filneame . '" alt="' . $filename.'"/
   >'; }\`
 * What I would expect to happen is the images show up if their filenames are in
   the feat-image custom field, and if the field isn’t set, no images show up at
   all. What happens instead is that images don’t show up if the field isn’t set,
   but if the field is set, I get broken images – evidently $filename is set enough
   to trigger the img tag, but it’s not actually appending the content of the feat-
   img field to the path, so all the images point to the directory /post-images/.
   Can anyone tell me what’s going on here?

Viewing 1 replies (of 1 total)

 *  Thread Starter [wmhunter](https://wordpress.org/support/users/wmhunter/)
 * (@wmhunter)
 * [15 years, 6 months ago](https://wordpress.org/support/topic/get_posts-and-custom-fields-1/#post-1810472)
 * Argh! I figured it out rereading the post: I have $filename misspelled in my 
   functions.php file. D’oh!

Viewing 1 replies (of 1 total)

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

## Tags

 * [custom fields](https://wordpress.org/support/topic-tag/custom-fields/)
 * [get_posts](https://wordpress.org/support/topic-tag/get_posts/)
 * [images](https://wordpress.org/support/topic-tag/images/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 1 reply
 * 1 participant
 * Last reply from: [wmhunter](https://wordpress.org/support/users/wmhunter/)
 * Last activity: [15 years, 6 months ago](https://wordpress.org/support/topic/get_posts-and-custom-fields-1/#post-1810472)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
