1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Need Help with this wordpress code.

Discussion in 'Programming' started by niravdave, Jun 21, 2010.

  1. #1
    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 
    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>';
      }
      
    }
    ?>
    PHP:

     
    niravdave, Jun 21, 2010 IP
  2. Kaimi

    Kaimi Peon

    Messages:
    60
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #2
    1. Get category id (term_id) by name from wp_terms
    2. Get object id's from wp_term_relationships where term_taxonomy_id = term_id
    3. Search through posts and then filter posts using object id's from previous query
     
    Kaimi, Jun 21, 2010 IP
  3. niravdave

    niravdave Active Member

    Messages:
    675
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    88
    #3
    Hi Kaimi,

    Thanks for your help. I am not a wordpress expert, but do you think this is what its should be like?

    <?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_terms wp, wp_term_relationships wp
    WHERE wp.term_taxonomy_id = '2'
    WHERE wp.term_id = (
    SELECT term_id
    FROM wp_terms
    WHERE name = 'Stores' )";		  
    		  
      $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>';
      }
      
    }
    ?>
    PHP:
    response appreciated.



     
    niravdave, Jun 21, 2010 IP
  4. Kaimi

    Kaimi Peon

    Messages:
    60
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Try this
    
    <?php
    require('wp-blog-header.php');
    $category_name = 'Uncategorized';
    
    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}%')) AND
    			wp.ID IN
    				(
    					SELECT object_id
    					FROM wp_term_relationships
    					WHERE term_taxonomy_id =
    					(
    						SELECT term_id
    						FROM wp_terms
    						WHERE name = '$category_name'
    					)
    				)
              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>';
      }
     
    }
    ?>
    
    PHP:
     
    Kaimi, Jun 21, 2010 IP
  5. niravdave

    niravdave Active Member

    Messages:
    675
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    88
    #5
    Kiami,

    You are a rockstar mate. Please check you inbox.

    Dave

     
    niravdave, Jun 21, 2010 IP