Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Ok, so I'm pretty confident on how to display a certain post's custom field content on my home page, or wherever I wanted it. But what I'm trying to do is this: I have my main home page that I want to display content from the newest post everyday. In other words, I have an area where I need to display the newest post's custom field content daily. Automatically. I will be posting a new post every morning. I don't want to have to go in and edit the home template page everyday though. I would like to setup the home page template to automatically pull in the info. So every morning, after publishing my post, this block will show the custom field content in it. Can anyone point me in the right direction? Thanks!

share|improve this question
    
I mean, I realize that I could just display the loop in that block, and just display one post of the loop, but I've already built my custom home page template, and it would be easier for me to just insert custom field content into my template, instead of going through having to style the whole post. I guess what I'm saying is that I didn't really style my template with full posts in mind. I hope that makes some kind of sense? –  Anthony Myers Aug 21 '12 at 2:22

1 Answer 1

up vote 2 down vote accepted

It sounds like what you need is a custom secondary loop, probably using WP_Query, that just queries for your single most recent post. Once you have that, you can use get_post_meta() to grab the custom meta from that post in a new loop.


Edit:

Alternately, you can use get_posts() to get the first post's ID and use that. Something like this:

$my_most_recent_post = get_posts( 'numberposts=1' );
$my_most_recent_post_id = $my_most_recent_post[0]->ID;
get_post_meta( $my_most_recent_post_id, 'my_custom_field', true );
share|improve this answer
    
Ok, so I'm confused on how using WP_Query is different from how I normally pull in the loop in my projects. which I posted here: pastebin.com/ZHcjKbPr Can you help me out a bit more? How exactly do I get a custom secondary loop going? Can I get a secondary loop to work with the code I posted on pastebin? Or does it have to be completely separate? –  Anthony Myers Aug 22 '12 at 2:59
    
I'm trying it this way because just popping in <?php echo get_post_meta($post->ID, 'my_custom_field', true); ?> into my template is so much easier than trying to style the whole post that is ouput. I find it difficult to style the loop for some reason. It just has to automatically pull in the most recent data from the most recent published post. So the "published" post won't actually be visible, just portions of it that have been entered into the custom fields. I feel like I'm not explaining it well enough. I'm making it more confusing? –  Anthony Myers Aug 22 '12 at 2:59
    
See the edit. I should add that "styling the loop" doesn't really have anything to do with the loop. The loop is purely PHP that outputs HTML. The HTML is what you style and you control what that HTML is. –  mrwweb Aug 22 '12 at 15:27
    
Awesome, thank you for replying again! Now, that code you posted would have to be manually edited every time I post a new post though right? I would have to change the post ID number each day? –  Anthony Myers Aug 22 '12 at 19:41
1  
Two issues. 1) get_post_meta() doesn't echo anything, so if you want to display it, tack an echo in front of the last line of code. 2) It doesn't go in the loop. The loop is for querying posts which is what get_post() is doing. If you put it in the loop, you'll run that code snippet once for every post on your front page. Make sure to read the codex pages for each function so you understand what's going on. –  mrwweb Aug 23 '12 at 3:54

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.