0

On my WordPress site, I'm using a plugin called "Vera Meta Boxes" that lets me add custom meta boxes to pages (not referring to custom post types).

So now every page has meta box that says, "Show on Home page" with a checkbox, that when selected has the value of "yes".

Now on my homepage, I want to show the titles and featured image of any page that has "Show on Homepage" checked.

The vera meta box plugin says to use:

<?php get_post_custom_values('your_custom_field_key_here'); ?>

So I would use:

<?php get_post_custom_values('show_on_homepage'); ?>

But how do I do the rest? Conceptually, it would be something like:

LOOP Query Pages > if <?php get_post_custom_values('show_on_homepage'); ?> = yes show title and featured image and repeat loop until all pages with "show_on_homepage" are shown.

1 Answer 1

1

Well, I haven't tested this, but according to the docs you should be able to use this query:

array('post_type'=>'page', 'meta_query' => array( array('key' => 'show_on_homepage') ) )

Note that meta_query is an array of arrays

This is 3.1 code, the 3.0 version should look like this:

array('post_type'=>'page', 'meta_key' => 'show_on_homepage')
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks Nikolay, I'm using 3.1 so the first line should certainly be a good start. But being new to WP I'm not sure about the other elements. That is adding that line to my template would not work I assume, and would need wpQuery or something on front and such. Any pushes in the right direction will be very helpful, thanks!
On the Codex Template Tags/get posts page I found an example for posts and added your code to it: <?php $args = array('post_type'=>'page', 'meta_query' => array('key' => 'show_on_homepage')); $postslist = get_posts( $args ); foreach ($postslist as $post) : setup_postdata($post); ?> <div> <?php the_title(); ?> </div> <?php endforeach; ?> but no luck. Do I need to change the words post to page?
Oh, I'm using WP 3.1, but I just tried the one you said is for 3.0 and that one does work. Woot! Thanks!
@heavymark, sorry, it slipped to me that it shouldn't be a single array, but an array of arrays, thats why they've changed it. I suppose the proper way is this: array('post_type'=>'page', 'meta_query' => array(array('key' => 'show_on_homepage')))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.