Title: Query Problems
Last modified: August 19, 2016

---

# Query Problems

 *  [brokenartist](https://wordpress.org/support/users/brokenartist/)
 * (@brokenartist)
 * [16 years, 7 months ago](https://wordpress.org/support/topic/query-problems/)
 * I am trying to run a specific query to pull only posts from a specific catergory.
   The catergory id is alled e-backpack and the id is 4. I have no idea where I 
   went wrong in this. I have looked into so many posts and still no avail. Can 
   someone please help
 *     ```
       <div id="homepagetop">
                                       <div class="hptop">
                                               <h3 style="padding-bottom: 20px;">E-Backpack</h3>
   
                               <?php   $querystr = "
                                       SELECT * FROM $wpdb->posts
       				LEFT JOIN $wpdb->postmeta ON($wpdb->posts.ID = $wpdb->postmeta.post_id)
       				LEFT JOIN $wpdb->term_relationships ON($wpdb->posts.ID = $wpdb->term_relationships.object_id)
       				LEFT JOIN $wpdb->term_taxonomy ON($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
       				WHERE $wpdb->term_taxonomy.term_id = 4
       				AND $wpdb->term_taxonomy.taxonomy = 'category'
       				AND $wpdb->posts.post_status = 'publish'
       				AND $wpdb->postmeta.meta_key = 'ebackpack'
       				AND $wpdb->postmeta.meta_value = '$today'
       				ORDER BY $wpdb->postmeta.meta_value ASC
   
       				LIMIT 1
                                       ";
                                        $show = $wpdb->get_results($querystr, OBJECT);
                               ?>
                                        <?php if ($pageposts): ?>
                                               <?php foreach ($pageposts as $post): ?>
                                                       <?php setup_postdata($post); ?>
                                                       <div class="newsicon">
                                                               <?php if( get_post_meta($post->ID, "thumbnail", true) ): ?>
                                                                       <a href="<?php the_permalink() ?>" title="<?php the_title(); ?>" rel="bookmark"><img style="float:left;margin:0px 10px 0px 0px;" src="<?php echo get_post_meta($post->ID, "thumbnail", true); ?>" alt="<?php the_title(); ?>" /></a>
                                                               <?php else: ?>
                                                                       <a href="<?php the_permalink() ?>" title="<?php the_title(); ?>" rel="bookmark"><img style="float:left;margin:0px 10px 0px 0px;"  src="<?php bloginfo('template_url'); ?>/images/thumbnail.png" alt="<?php the_title(); ?>" /></a>
                                                               <?php endif; ?>
                                                       </div><!-- end newsicon -->
                                                       <div class="newsbody">
                                                               <h3><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>" rel="bookmark"><?php the_title(); ?></a></h3>
                                                               <p>Posted <?php the_time('F j, Y'); ?></p>
                                                               <?php the_excerpt(); ?>
   
                                                       </div><!-- end newsbody -->
                                                       <div style="border-bottom:1px dotted #BBBBBB; margin-bottom:10px; padding:0px 0px 10px 0px; clear:both;"></div>
                                               <?php endforeach; ?>
                                       <?php else : ?>
                                               <p class="center">No announcements at this time.</p>
                                       <?php endif; ?>
                                       </div><!-- end hptop -->
                               </div><!-- end homepagetop -->
       ```
   

Viewing 3 replies - 1 through 3 (of 3 total)

 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [16 years, 7 months ago](https://wordpress.org/support/topic/query-problems/#post-1275344)
 *     ```
       <?php
         $args=array(
           'cat' => 4,
           'post_type' => post,
           'post_status' => 'publish',
           'posts_per_page' => -1,
           'caller_get_posts'=> 1
           );
         $my_query = null;
         $my_query = new WP_Query($args);
         if( $my_query->have_posts() ) {
           while ($my_query->have_posts()) : $my_query->the_post(); ?>
             <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
            <?php
           endwhile;
         } //if ($my_query)
       } // foreach
       wp_reset_query();  // Restore global post data stomped by the_post().
       ?>
       ```
   
 *  Thread Starter [brokenartist](https://wordpress.org/support/users/brokenartist/)
 * (@brokenartist)
 * [16 years, 7 months ago](https://wordpress.org/support/topic/query-problems/#post-1275347)
 * i placed that code in but it breaks the page showing no data
 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [16 years, 7 months ago](https://wordpress.org/support/topic/query-problems/#post-1275355)
 * Can’t attest to `$today` or the test of the `get_post_meta` but this works:
 *     ```
       <div id="homepagetop">
       <div class="hptop">
       <h3 style="padding-bottom: 20px;">E-Backpack</h3>
   
       <?php
       $cat = 4;
       $meta_key = 'ebackpack';
       $meta_value = $today;
       $args=array(
         'cat' => $cat,
         'meta_key' => $meta_key,
         'meta_value' => $meta_value,
         'posts_per_page' => -1,
         'caller_get_posts' => 1
       );
       $my_query = null;
       $my_query = new WP_Query($args);
       if( $my_query->have_posts() ) {
         while ($my_query->have_posts()) : $my_query->the_post(); ?>
   
       <div class="newsicon">
   
       <?php if ( get_post_meta($post->ID, "thumbnail", true) ) { ?>
       <a href="<?php the_permalink() ?>" title="<?php the_title(); ?>" rel="bookmark"><img style="float:left;margin:0px 10px 0px 0px;" src="<?php echo get_post_meta($post->ID, "thumbnail", true); ?>" alt="<?php the_title(); ?>" /></a>
       <?php } else { ?>
       <a href="<?php the_permalink() ?>" title="<?php the_title(); ?>" rel="bookmark"><img style="float:left;margin:0px 10px 0px 0px;"  src="<?php bloginfo('template_url'); ?>/images/thumbnail.png" alt="<?php the_title(); ?>" /></a>
   
       <?php } // if (get_post_meta ?>
   
       </div><!-- end newsicon -->
       <div class="newsbody">
       <h3><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>" rel="bookmark"><?php the_title(); ?></a></h3>
       <p>Posted <?php the_time('F j, Y'); ?></p>
       <?php the_excerpt(); ?>
       </div><!-- end newsbody -->
       <div style="border-bottom:1px dotted #BBBBBB; margin-bottom:10px; padding:0px 0px 10px 0px; clear:both;"></div>
   
       <?php endwhile; ?>
   
       <?php } else { ?>
   
       <p class="center">No announcements at this time.</p>
   
       <?php } //if (have_posts
   
       wp_reset_query();  // Restore global post data stomped by the_post().
       ?>
       </div><!-- end hptop -->
       </div><!-- end homepagetop -->
       ```
   

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Query Problems’ is closed to new replies.

## Tags

 * [category](https://wordpress.org/support/topic-tag/category/)
 * [query](https://wordpress.org/support/topic-tag/query/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 3 replies
 * 2 participants
 * Last reply from: [MichaelH](https://wordpress.org/support/users/michaelh/)
 * Last activity: [16 years, 7 months ago](https://wordpress.org/support/topic/query-problems/#post-1275355)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
