mariostella on "php query in post"
April 22, 2010 by David Olsen
Hi,
I downloaded the plugin to execute php in post, and it works for code that does not involve long and complicated queries.
Inside a page I want to list a series of pages that have a specific meta key and value. The wp_list_pages would work great, but only outputs a crude list of links, whereas I need a nice “foreach” so I can customize each result adding an excerpt, and other fun stuff.
I usually do this inside a template file with the following function
<?php
$querystr = "
SELECT $wpdb->posts.* FROM $wpdb->posts
LEFT JOIN $wpdb->postmeta ON($wpdb->posts.ID = $wpdb->postmeta.post_id)
WHERE $wpdb->posts.post_status = 'publish'
AND $wpdb->posts.post_type = 'post'
AND $wpdb->postmeta.meta_value = 'CUSTOM VALUE HERE'
ORDER BY $wpdb->posts.post_modified DESC
";
$pageposts = $wpdb->get_results($querystr, OBJECT);?>
<?php if ($pageposts): ?>
<?php foreach ($pageposts as $post): ?>
<?php setup_postdata($post); ?>
THE FUN STUFF GOES HERE
<?php endforeach; ?>
<?php else : ?>
WHATEVER
<?php endif; ?>
Unfortunately it does not work a bit. So I tried using
<?php
$my_query = new WP_Query();
$my_query->query('meta_key=tools&meta_value=tool');
while ($my_query->have_posts()): $my_query->the_post(); ?>
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a>
<?php endwhile; ?>
It gives no errors but does not output anything either.
What am I getting wrong?
Thanks for any reply!






Comments
Facebook comments:
Feel free to leave a comment...
and oh, if you want a pic to show with your comment, go get a gravatar!