Pages

Thursday, November 8, 2012

Wordpress get posts on basis of name,category,tag

Since we have post name involved here long with category and tag we need to use custom query here which will provide us to search specific posts which contain special text in name and will belong to a particular category and will contain a tag as per requirements

Here is the custom query

I used a get in my search form with normal request parameters

 if (!(empty($_GET['section'])) || !(empty($_GET['article']))) {  
       global $wpdb, $post, $paged, $max_num_pages, $current_date;  
       $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;  
       $post_per_page = intval(get_query_var('posts_per_page'));  
       $offset = ($paged - 1) * $post_per_page;  
       $val = $_GET['s'];  
       $cat = $_GET['category'];  
       $tag = $_GET['tag'];  
 $querystr = "  
   SELECT DISTINCT $wpdb->posts.*  
   FROM $wpdb->posts  
   LEFT JOIN $wpdb->postmeta wpostmeta ON ($wpdb->posts.ID = wpostmeta.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->posts.post_type = 'post'  
     AND $wpdb->posts.post_status='publish'";  
 //Tags and categories were like dropdowns so each time either "0" for no select or tag_id or //category_id or both were sent via GET as url params  
       if($tag > 0 && $cat > 0)  
       {  
         $querystr = $querystr . " AND ($wpdb->posts.post_title like '%$val%' AND ($wpdb->term_taxonomy.taxonomy = 'post_tag'  
       AND $wpdb->term_taxonomy.term_id IN($tag))) OR ($wpdb->posts.post_title like '%$val%' AND ($wpdb->term_taxonomy.taxonomy = 'category'  
       AND $wpdb->term_taxonomy.term_id IN($cat)))";  
       }  
       elseif ($tag > 0) {  
         $querystr = $querystr ." AND $wpdb->posts.post_title like '%$val%' AND ($wpdb->term_taxonomy.taxonomy = 'post_tag'  
       AND $wpdb->term_taxonomy.term_id IN($tag))";  
       }  
       elseif ($cat > 0) {  
               $querystr = $querystr . " AND $wpdb->posts.post_title like '%$val%' AND ($wpdb->term_taxonomy.taxonomy = 'category'  
         AND $wpdb->term_taxonomy.term_id IN($cat))";  
          }  
       $pageposts = $wpdb->get_results($querystr, OBJECT);  
       $querystr = $querystr . " LIMIT " . $offset . ", " . $post_per_page . "; ";  
       $sql_posts_total = $wpdb->get_var("SELECT FOUND_ROWS();");  
       $max_num_pages = ceil($sql_posts_total / $post_per_page);  

 //And can use pagination as
  <?php if ($wp_query->max_num_pages > 1) : ?>  
           <nav id="<?php echo $nav_id; ?>">  
             <div class="nav-previous"><?php next_posts_link(__('<span class="meta-nav">&larr;</span> Older posts', 'old')); ?></div>  
             <div class="nav-next"><?php previous_posts_link(__('Newer posts <span class="meta-nav">&rarr;</span>', 'next')); ?></div>  
           </nav>  
   <?php endif; ?>   


This is one way out...

Other way out is to put this filter in your functions.php

 add_filter('posts_where','wpse_posts_where', 10, 2 );  
 function wpse_posts_where( $where, &$wp_query )  
 {  
   global $wpdb;  
   if ( $wpse_title = $wp_query->get( 'wpse_title' ) ) {  
     $where .= ' AND ' . $wpdb->posts . '.post_title LIKE \'%' . esc_sql( like_escape( $wpse_title ) ) . '%\'';  
   }  
   return $where;  
 }  


and then make a call to

 <?php if(!(empty($_GET['category'])) || !(empty($_GET['tag'])))  
 {  
   $val=$_GET['s'];  
   $cat=$_GET['category'];  
   $tag=$_GET['tag'];  
      global $query_string;  
      $qstr= $query_string."wpse_title=$val";  
      $args['wpse_title']=$val;  
      if($cat>0)  
      {  
           $args['category__and']=$cat;  
           $qstr=$qstr."&cat=$cat";  
      }  
      if ($tag>0)  
   {  
           $args['tag__in'] = $tag;  
           $qstr=$qstr."&tag=$tag";  
   }  
      query_posts($args);  
      query_posts($qstr);  
 ?>  


They both work how ever in later case youcan either make via name and category or name and tag

If you try to search via name category and tag you will end up getting a response which contains all the posts of that category..so I preffered using a custom query as it suited me

1 comment:

  1. Good article

    https://wordpress-special-plugin.blogspot.co.id

    ReplyDelete