Dynadot Aftermarket: discover domain gems worth investing in. Visit our aftermarket.

Need help with wordpress code

SpaceshipSpaceship
SpaceshipSpaceship
SpaceshipSpaceship
Watch

nirav.dave

Capsicum MediaworksEstablished Member
Impact
5
Hi guys,

I am trying to work on my wordpress site and need some help with this piece of wordpress code. Bascially this code currently searches for titles for posts across the website. I want to modify it to search only within one category.

anyone has got any idea or can help me with it?

PHP:
<?php 
require('../../../wp-blog-header.php'); 

if (isset($_GET['s']) && trim($_GET['s']) != '') {
  
  $term = mysql_real_escape_string(trim($_GET['s']));
  $sql = "SELECT 
            * 
          FROM 
            wp_posts wp 
          WHERE 
            wp.post_type = 'post' AND 
            wp.post_status = 'publish' AND 
            wp.post_date <= '" . date('Y-m-d H:i:s', time()) . "' AND
            ((wp.post_title LIKE '%${term}%') OR (wp.post_content LIKE '%${term}%')) 
          ORDER BY 
            wp.post_date DESC 
          LIMIT 10";
  $posts = $wpdb->get_results($sql);
  
  if (count($posts) > 0) {
    echo '<ul>';
    foreach($posts as $post) {
      echo '<li><a href="' . get_permalink($post->ID) . '">' . the_title('', '', false) . '</a></li>';
    }
    echo '</ul>';
  } else {
    echo '<p>No results found.</p>';
  }
  
}
?>
 
0
Close
The views expressed on this page by users and staff are their own, not those of NamePros.
GoDaddyGoDaddy
I have no idea when it comes to the WP database structure som I'm just guessing what it looks like.

I've added

wp.post_category = 'your_choice' AND

after WHERE.

What "wp.post_category" really is called I have no idea. Where it says "your_choice" you should put the category name.

PHP:
$sql = "SELECT 
            * 
          FROM 
            wp_posts wp 
          WHERE
            wp.post_category = 'your_choice' AND 
            wp.post_type = 'post' AND 
            wp.post_status = 'publish' AND 
            wp.post_date <= '" . date('Y-m-d H:i:s', time()) . "' AND
            ((wp.post_title LIKE '%${term}%') OR (wp.post_content LIKE '%${term}%')) 
          ORDER BY 
            wp.post_date DESC 
          LIMIT 10";
 
0
Close
AIVikingsAIVikings

We're social

Escrow.com
Spaceship
Domain Recover
CryptoExchange.com

NamePros updates

Switch between accounts
You can now stay logged in to more than one NamePros account in the same browser. Click your avatar, then "Accounts in this browser", to...
AIVikings
Catchy
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back