0

While searching, I want all posts from all categories except one category

This is my search query

SELECT * FROM wp_posts WHERE post_title like '%$search%' AND post_type = 'post' AND post_status = 'publish' ORDER BY post_date DESC

In this query, i don't want book category posts. when i search any keyword, need to display all posts except book category posts

Any help?

Guna
  • 15
  • 7
  • I tried this code. it's not working. `SELECT * FROM wp_posts WHERE post_title like '%$search%' AND category != 'book' AND post_type = 'post' AND post_status = 'publish' ORDER BY post_date DESC` – Guna Jul 24 '14 at 11:10
  • Sorry. i dono, where i get the category name.In wp_posts have full of post details.can u help me. – Guna Jul 24 '14 at 11:27
  • I found the category name in my table. name is my category name. i tried name != 'book'.not working – Guna Jul 24 '14 at 13:07
  • i checked the category name in my table, same like 'name'. In wp_terms table, category name column is here. Need to write any join query? – Guna Jul 25 '14 at 07:18
  • what information you want? i 'll tell you – Guna Jul 25 '14 at 07:28
  • You have to make the join query from wp_posts, wp_terms, wp_term_relationships – Khushboo Jul 25 '14 at 10:34
  • can u help me.i dono how to write join query. – Guna Jul 25 '14 at 10:38

1 Answers1

0

Try below :-

14 is the category id

 $request = $wpdb->prepare("SELECT * FROM $wpdb->posts 
  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 <> 14  
     AND $wpdb->term_taxonomy.taxonomy = 'category'   
     AND $wpdb->posts.post_type = 'post'  
     AND $wpdb->posts.post_status = 'publish'
     AND post_title like '%$search%'
     AND post_date >= '" . date('Y-m-d').")");
Khushboo
  • 1,819
  • 1
  • 11
  • 17
  • you can replace $wpdb from wp_ – Khushboo Jul 25 '14 at 11:21
  • Getting syntax error.all my table name starts work_ instead of wp_..can u help me – Guna Jul 26 '14 at 05:23
  • then, add work_. It was just an example. wp_ is common, so I used that :) – Khushboo Jul 26 '14 at 05:27
  • 'SELECT * FROM work_posts LEFT JOIN work_term_relationships ON(work_posts.ID = work_term_relationships.object_id) LEFT JOIN work_term_taxonomy ON(work_term_relationships.term_taxonomy_id = work_term_taxonomy.term_taxonomy_id) WHERE (work_term_taxonomy.term_id <> 14 AND work_term_taxonomy.taxonomy = 'category' AND work_posts.post_type = 'post' AND work_posts.post_status = 'publish')' – Guna Jul 26 '14 at 05:27
  • u r really gr8. it's works. pls keep in touch with me. – Guna Jul 26 '14 at 05:41
  • Sure, I will. Please except both the answers :) – Khushboo Jul 26 '14 at 05:46
  • Hi, In this query, I want to get today date post only.how to edit? pls help – Guna Aug 01 '14 at 11:53
  • add AND post_date >= '" . date('Y-m-d') . "'" – Khushboo Aug 01 '14 at 12:02
  • Showing error...`#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Y-m-d') . "'"` – Guna Aug 01 '14 at 12:08
  • I tried this `AND post_date >= CURRENT_DATE`..it's works – Guna Aug 01 '14 at 12:35
  • Hi, this is my site webservice, it's working good. but one issue is here on "post_count". I used two types of query 1) date wise 2) normal . In normal search, result show all the posts of category. In date wise search, result show particular date posts. so post_count should be differnt. how can i display the post_count of these results.any hep? Chk this link. i have uploaded my file. http://www.filedropper.com/postcount – Guna Aug 02 '14 at 06:38
  • you can use $count = mysql_num_rows($sql_res) – Khushboo Aug 02 '14 at 07:00
  • Now, please except my both answers now. :P – Khushboo Aug 02 '14 at 07:24
  • Means, what should i do? i can't understand. – Guna Aug 02 '14 at 07:29
  • Hi, i want to create one page wordpress scrolling site like this http://themes.opendept.net/retro-portfolio-4/ any idea? – Guna Aug 07 '14 at 13:04
  • you will get wordpress theme like this – Khushboo Aug 08 '14 at 04:54
  • http://www.mageewp.com/demo/?theme=onetone http://demo.enginethemes.com/oneengine/ – Khushboo Aug 08 '14 at 04:57
  • Hi, i want to show my aboutus page content in my home page(index.php).i use this query. ` $pages = get_pages(); foreach ($pages as $page_data) { $content = apply_filters('the_content', $page_data->post_content); $title = $page_data->post_title; echo "

    ".$title."

    " ."
    ".$content."
    "; } ?>`it shows all page content. i want particular page.can u help me?
    – Guna Aug 14 '14 at 06:45
  • Use get_post() instead of get_pages() – Khushboo Aug 14 '14 at 09:17
  • Is is possible to use jquery datepicker for fetching data from db on selecting particular date ? – Guna Aug 22 '14 at 10:18
  • I'm using the JQuery DatePicker, it's working. now i want to get particular posts date wise. how can i connect datepicker with mysql query – Guna Aug 22 '14 at 10:28
  • chk this page http://dev.clonet.in/seyassah/category/social/ date picker jquery is working – Guna Aug 22 '14 at 10:29
  • i got code from this link.. http://stackoverflow.com/questions/1328025/jquery-ui-datepicker-change-date-format – Guna Aug 22 '14 at 10:30
  • you can use filter_where – Khushboo Aug 22 '14 at 11:06
  • check this :- http://wordpress.stackexchange.com/questions/97459/add-filter-and-remove-filter-added-before-and-after-wp-query – Khushboo Aug 22 '14 at 11:07
  • how to connect datepicker date to mysql date...did u chk my link..dev.clonet.in/seyassah/category/social..in this page, click text box, calender will open.if i click aug 15, aug 15 th posts should be display..now current date posts are displaying – Guna Aug 22 '14 at 11:42
  • ` Date: ` – Guna Aug 22 '14 at 11:46
  • check this :- http://stackoverflow.com/questions/24709817/process-mysql-function-and-display-using-jquery-ajax-with-time-invterval-for-ea/24709852#24709852 – Khushboo Aug 22 '14 at 11:54
  • I have given answer to use ajax – Khushboo Aug 22 '14 at 11:55
  • Sorry for asking, what is url:, type:, data:. i dono anything in ajax. pls help me..see my above datepicker code.this is file of query page. how to connect. http://www.filedropper.com/category – Guna Aug 22 '14 at 12:12
  • do you have sky pe? I can help you after my office hours i.e; after 7 pm IST. I am in office rite now – Khushboo Aug 22 '14 at 12:17
  • thanks. gunabalan22. this is my skype id.am also in office. i ll reach home at 7.45 pm. – Guna Aug 22 '14 at 12:20
  • will u come online now? – Guna Aug 22 '14 at 16:22
  • hi, i tried this code. please check is it correct or not. b'coz it's not retrive anything..current date posts and datepicker date also not working..this is my code..http://www.filedropper.com/catey – Guna Aug 27 '14 at 06:26
  • Please check this link and help me.. http://stackoverflow.com/questions/25521014/jquery-datepicker-with-ajax-is-not-working – Guna Aug 27 '14 at 08:43
  • hi chk this link. http://stackoverflow.com/questions/25795769/drupal-how-to-add-html-conent-into-page-tpl-php – Guna Sep 11 '14 at 20:00