Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Can someone review my code , I need to know better solutions or any bug.

Okey ! , I made a PHP file to get search results by parameters and search by title , content and tag

PHP's head look like:

include('../../../../wp-config.php');
header('Content-type:text/html;charset=utf-8');
$keyword = $_GET['k'];

That I will bring search results by jQuery after.

My main search function to show result from database:

function search($keyword,$mark){
    while($data=mysql_fetch_array($keyword)){
        if($data['post_status']=='publish'){
            echo '<li>#'.$data['ID'].' '.htmlentities($data['post_title']).' ('.$mark.')</li>';
        }
    }
} 

Search by title function : Full keyword (ex : "jack daniels")

$sql_full_keyword       = "SELECT * from wp_posts WHERE post_title LIKE '$keyword%'";
$search_full_keyword    = mysql_query($sql_full_keyword) or die(mysql_error());
search($search_full_keyword,"Full keyword(s) = '$keyword'");

So i split keyword for next steps like :

$keywords_split         = explode(" ",$keyword);

Search by title function : multi keywords (ex : jack + daniels)

for($i=0;$i<count($keywords_split);$i++){
    $sql_split_keyword      = "SELECT * from wp_posts WHERE post_title LIKE '$keywords_split[$i]%'";
    $search_split_keyword   = mysql_query($sql_split_keyword) or die(mysql_error());
    search($search_split_keyword,"Split keyword = '$keywords_split[$i]'");
}

Search by content function : (same above, but search by content)

for($i=0;$i<count($keywords_split);$i++){
    $sql_split_keyword      = "SELECT * from wp_posts WHERE post_content LIKE '$keywords_split[$i]%'";
    $search_split_keyword   = mysql_query($sql_split_keyword) or die(mysql_error());
    search($search_split_keyword,"Split keyword = '$keywords_split[$i]' (Content)");
}

Than close mysql

mysql_close();

Search by tag function : (using normal wordpress query)

    $tags = str_replace(" ","-",$keyword).",".str_replace(" ",",",$keyword);
    query_posts(array('post_type'=>array('one','two','three') ,'tag' => $tags,'posts_per_page'=>-1));

    while (have_posts()):the_post();
    echo '<li>#'.get_the_ID().' '.get_the_title().'</li>';
    endwhile;

Everything work now and I need someone to review my code, tell me better things or tong ...

Full code here : http://snipt.org/zpz0

Thanks

share|improve this question
1  
I don't even know where to start with what's wrong with this code... –  kaiser Jan 3 '13 at 13:33
    
no, just ask to review @kaiser –  l2aelba Jan 3 '13 at 13:34
3  
Are you coming to WP with PHP background or followed vanilla PHP tutorial? There is massive amount of things here done in plain PHP, while they should be done via WordPress APIs when implemented in it. –  Rarst Jan 3 '13 at 13:35
3  
Never use query_posts, never include wp-config.php from a file, never call a php file to do AJAX use the provided API instead, always use the WPDB class instead of the deprecated php mysql extensions, use if(){} not if() : endif;, don't define html characterset etc with a header() call, always sanitise variables pulled from $_GET, always prefix your functions and classes with a unique identifier, or namespace them accordingly.. etc etc etc –  Tom J Nowell Jan 3 '13 at 15:01
1  
But aside from that, I'd give you a 9/10 for effort for someone fresh and brand new, but do be aware now we've seen your code and pointed out things we expect immediate improvement –  Tom J Nowell Jan 3 '13 at 15:03

1 Answer 1

up vote 3 down vote accepted

Search in WordPress is complicated matter because:

  1. WP doesn't do it very well out of the box
  2. People tend to have varied expectations of how precisely search should work
  3. Even light (from human perspective) requirements for search tend to translate in complicated and involved code

Building search from scratch is involved (I know because I did exactly that for a while for clients) and you should totally look for search plugins or other compromise (like search as service) first.

To briefly enumerate specific issues with your code:

share|improve this answer
    
Fixed gist.github.com/4450989 , thanks :D –  l2aelba Jan 4 '13 at 8:50

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.