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:

$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){
            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)

    $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)

    $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


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>';

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

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


  • 1
    I don't even know where to start with what's wrong with this code...
    – kaiser
    Jan 3, 2013 at 13:33
  • no, just ask to review @kaiser
    – l2aelba
    Jan 3, 2013 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, 2013 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, 2013 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, 2013 at 15:03

1 Answer 1


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:


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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