Title: php query in post
Last modified: August 19, 2016

---

# php query in post

 *  [mariostella](https://wordpress.org/support/users/mariostella/)
 * (@mariostella)
 * [15 years, 11 months ago](https://wordpress.org/support/topic/php-query-in-post/)
 * 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!

Viewing 1 replies (of 1 total)

 *  Thread Starter [mariostella](https://wordpress.org/support/users/mariostella/)
 * (@mariostella)
 * [15 years, 11 months ago](https://wordpress.org/support/topic/php-query-in-post/#post-1474382)
 * Solved.
 *     ```
       <?php
       $my_query = new WP_Query('post_type=page&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; ?>
       ```
   

Viewing 1 replies (of 1 total)

The topic ‘php query in post’ is closed to new replies.

## Tags

 * [php in post](https://wordpress.org/support/topic-tag/php-in-post/)

 * 1 reply
 * 1 participant
 * Last reply from: [mariostella](https://wordpress.org/support/users/mariostella/)
 * Last activity: [15 years, 11 months ago](https://wordpress.org/support/topic/php-query-in-post/#post-1474382)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
