Title: Get  posts with WP_Query by postname
Last modified: August 19, 2016

---

# Get posts with WP_Query by postname

 *  Resolved [ashwinnaidu](https://wordpress.org/support/users/ashwinnaidu/)
 * (@ashwinnaidu)
 * [16 years, 1 month ago](https://wordpress.org/support/topic/get-posts-with-wp_query-by-postname/)
 * I need query for getting posts that matches the given string.
 * I have constructed this, which is not working.
 *     ```
       <!-- if Product is mentioned -->
       <?php
   
       if($product!="") {
       $query = "name=" . $product . "&posts_per_page=10";
       $my_query = new WP_Query($query); ?>
   
       <?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
   
       <h2>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
       <?php the_title(); ?>
       <?php endwhile; } ?>
       ```
   

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

 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [16 years, 1 month ago](https://wordpress.org/support/topic/get-posts-with-wp_query-by-postname/#post-1432735)
 *     ```
       <?php
       $product='xyz';
       $product=strtoupper($product);
       $ids = $wpdb->get_col("SELECT ID FROM $wpdb->posts WHERE UCASE(post_title) LIKE '%$product%' AND post_type='post' AND post_status='publish'");
       if ($ids) {
         $args=array(
           'post__in' => $ids,
           'posts_per_page' => -1,
           'caller_get_posts'=> 1
         );
         $my_query = null;
         $my_query = new WP_Query($args);
         if( $my_query->have_posts() ) {
           echo 'List of 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;
         }
       wp_reset_query();  // Restore global post data stomped by the_post().
       }
       ?>
       ```
   
 * [moderated fixed code]
 *  Thread Starter [ashwinnaidu](https://wordpress.org/support/users/ashwinnaidu/)
 * (@ashwinnaidu)
 * [16 years, 1 month ago](https://wordpress.org/support/topic/get-posts-with-wp_query-by-postname/#post-1432751)
 * Thanks Michael.
 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [16 years, 1 month ago](https://wordpress.org/support/topic/get-posts-with-wp_query-by-postname/#post-1432752)
 * Note the change I made to the SQL statement — `UCASE(post_title)`
 *  Thread Starter [ashwinnaidu](https://wordpress.org/support/users/ashwinnaidu/)
 * (@ashwinnaidu)
 * [16 years, 1 month ago](https://wordpress.org/support/topic/get-posts-with-wp_query-by-postname/#post-1432753)
 * Can I add category_name, meta_key=specs&meta_value=, in the args ?
 * regards,
    Ashwin.
 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [16 years, 1 month ago](https://wordpress.org/support/topic/get-posts-with-wp_query-by-postname/#post-1432755)
 * Note you didn’t put anything after `meta_value=` so if you want all posts with`
   meta_key=specs` you don’t have to specify the `meta_value=` unless you want to
   limit it to a specific value.
 * You’d have to use `category__in` so you’d need to know the ID of the categories
   you wanted:
 *     ```
       //get specified post IDs in category 1, 53, or 3, where "meta_key" is "season" and "meta_value" is "2"
         $args=array(
           'category__in' => array(1,56,3),
           'meta_key'=>'season',
           'meta_value'=> '2',
           'post_type' => 'post',
           'post__in' => $ids,
           'posts_per_page' => -1,
           'caller_get_posts'=> 1
         );
       ```
   
 *  Thread Starter [ashwinnaidu](https://wordpress.org/support/users/ashwinnaidu/)
 * (@ashwinnaidu)
 * [16 years, 1 month ago](https://wordpress.org/support/topic/get-posts-with-wp_query-by-postname/#post-1432757)
 * I have two select fields(one for metakey/value and one for category) one input
   field(for post name) in a custom search page.
 * I have written the code for both
 * <?php
    $location = $_POST[“location”]; $product = $_POST[“product”]; $category
   = $_POST[“cat”]; $catname = get_cat_name($category); ?>
 *  <!– if custom field location is mentioned Category is not Mentioned –>
    <?php
   if($location!=”All” && $category==0) { $query = “meta_key=specs&meta_value=” .
   $location; $my_query = new WP_Query($query); ?>
 * <?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
 *  <h2>” rel=”bookmark” title=”Permanent Link to <?php the_title_attribute(); ?
   >”>
    <?php the_title(); ?> <?php endwhile; } ?>
 *  <!– if category and location both are mentioned –>
    <?php if($category!=0 &&
   $location!=”All”) { $query = “meta_key=specs&meta_value=” . $location . “&category_name
   =” . $catname; $my_query = new WP_Query($query); ?>
 * <?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
 *  <h2>” rel=”bookmark” title=”Permanent Link to <?php the_title_attribute(); ?
   >”>
    <?php the_title(); ?> <?php endwhile; } ?>
 * Now, I need to get posts based on postname($product) and from a specific category(
   $catname). Also, based on postname and the post with the give meta_key/value 
   pair. Lastly all three.
 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [16 years, 1 month ago](https://wordpress.org/support/topic/get-posts-with-wp_query-by-postname/#post-1432759)
 * Then you’ll need to replace the $args statement with something like:
 *     ```
       $cat_id = get_cat_ID($catname');
         $args=array(
           'category__in' => array($cat_id),
           'meta_key'=>'specs',
           'meta_value'=> $location,
           'post_type' => 'post',
           'post__in' => $ids,
           'posts_per_page' => -1,
           'caller_get_posts'=> 1
         );
       ```
   
 *  Thread Starter [ashwinnaidu](https://wordpress.org/support/users/ashwinnaidu/)
 * (@ashwinnaidu)
 * [16 years, 1 month ago](https://wordpress.org/support/topic/get-posts-with-wp_query-by-postname/#post-1432761)
 * lovely, thanks very much. 🙂

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

The topic ‘Get posts with WP_Query by postname’ is closed to new replies.

## Tags

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

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 8 replies
 * 2 participants
 * Last reply from: [ashwinnaidu](https://wordpress.org/support/users/ashwinnaidu/)
 * Last activity: [16 years, 1 month ago](https://wordpress.org/support/topic/get-posts-with-wp_query-by-postname/#post-1432761)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
