WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Get posts with WP_Query by postname (9 posts)

  1. ashwinnaidu
    Member
    Posted 4 years ago #

    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; } ?>
  2. MichaelH
    Member
    Posted 4 years ago #

    <?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]

  3. ashwinnaidu
    Member
    Posted 4 years ago #

    Thanks Michael.

  4. MichaelH
    Member
    Posted 4 years ago #

    Note the change I made to the SQL statement --- UCASE(post_title)

  5. ashwinnaidu
    Member
    Posted 4 years ago #

    Can I add category_name, meta_key=specs&meta_value=, in the args ?

    regards,
    Ashwin.

  6. MichaelH
    Member
    Posted 4 years ago #

    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
      );
  7. ashwinnaidu
    Member
    Posted 4 years ago #

    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.

  8. MichaelH
    Member
    Posted 4 years ago #

    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
      );
  9. ashwinnaidu
    Member
    Posted 4 years ago #

    lovely, thanks very much. :)

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags