Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Display list of recent custom posts, if custom meta field is true WordPress

  • SOLVED

I have a custom content type ('films') and a custom field ('recommended').

I'd like to display a list, in the sidebar of individual 'films' posts, of recent films that have the 'recommended' custom field ticked.

This is what I've got so far - but it shows all films entries, not just the recommended ones.

<?php
global $post;
$myposts = get_posts('post_type=films&meta_key=recommended&meta_value=true&numberposts=5&offset=0');
foreach($myposts as $post) :
setup_postdata($post);
?>
<li><a href="<?php the_permalink(); ?>">Click here</a></li>
<?php endforeach; ?>


Can anyone get this working for me?

Thanks in advance.

Answers (3)

2011-09-21

Ivaylo Draganov answers:

Hello,

This is correct if the value of the custom field is actually the string "true". You should pass as a meta_value the exact value that you save in the custom field.

If the custom field is set up as a checkbox, then you should look at the "value" HTML attribute of the <input> element that is your actual checkbox.

What are you using to create the custom field?

2011-09-21

Julio Potier answers:

Hello

Try this :
get_posts('post_type=films&orderby=meta_value&meta_key=recommended&meta_value=true&numberposts=5&offset=0');

I added <strong>&orderby=meta_value</strong>

See you !

2011-09-21

Sébastien | French WordpressDesigner answers:

in this case you can't use the_permalink()
you can use, for example, $post->guid


Sébastien | French WordpressDesigner comments:

or use
query_posts( 'post_type=films&meta_key=recommended&meta_value=true&numberposts=5&offset=0' );
and the wp loop


Sébastien | French WordpressDesigner comments:

in this case, with the loop, the_permalink will work, of course
tell me if you have a problem with my solution :-)