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.

I have made a function of ajax, which returns all the posts starting with a particular alphabet, suppose a user clicks on letter A, then all the posts starting with letter A will be displayed, any how I am successful in making this function run, but only if I use normal mysql, but I want to use wp-mysql,

My code shall speak for me ;)

echo $_GET['letter'];
$getLetter = $_GET['letter'];
global $wpdb;
global $post;

$result = $wpdb->get_results('SELECT * FROM wp_posts where post_title LIKE "'.$getLetter.'%" AND post_status = "publish" AND post_type="post"');
while($row = $result)
{
  echo '<a href="' . $row["guid"] . '">' . $row['post_title'] . '</a><br>';
}

I am using this code which is getting a letter through URL and searching it for in database, but if I use the above code, it takes too long to search for a post(it takes more then a minute), but if I use the below code

echo $_GET['letter'];
$getLetter = $_GET['letter'];
$con = mysql_connect('localhost', 'root', '');
mysql_select_db("wordpress", $con);
$result = mysql_query('SELECT * FROM wp_posts where post_title LIKE "'.$getLetter.'%" AND post_status = "publish" AND post_type="post"');
while($row = mysql_fetch_array($result))
{
  echo '<a href="' . $row["guid"] . '">' . $row['post_title'] . '</a><br>';
}

then everything works quickly,

Can anyone explain me this? I don't know wp-db that much, but I want this to be worked in wpdb

Am I doing something wrong?

EDITED AJAX CODE:

<script type="text/javascript">

jQuery(function($) {
    $(document).ready(function() {
    $("#Loading").hide();
        $(".letter").bind('click', function(){
        $("#Loading").fadeIn(); //show when submitting
        var val = $(".letter").val;
        $.ajax({
             url:'<?php bloginfo('template_directory'); ?>/newContent.php?letter='+$(this).html(), 

             success:function(data){
                $("#Loading").fadeOut('fast'); //hide when data's ready
                $("#content1").html(data);
             }
        });
        });
    });
});
</script>
share|improve this question
1  
Are you using WordPress AJAX API? –  Ashfame Aug 13 '11 at 7:55
    
@Ashfame no, I am using different ajax, but that works perfect, cause when I use normal mysql the ajax works and also when I use the wp-mysql the ajax works but the database takes too long to call –  ntechi Aug 13 '11 at 9:41
    
Try doing it with WordPress AJAX API to figure out if its an issue with your approach or something with WordPress at the moment. Also test it without any plugins, and for once, also check the queries & time taken to generate the page. –  Ashfame Aug 13 '11 at 12:24
    
@Ashfame I didn't get you, with wordpress AJAX API? Just check my question I have updated it with my ajax code, can you explain me? –  ntechi Aug 13 '11 at 13:35

1 Answer 1

up vote 2 down vote accepted

When you call the database directly rather then using the $wpdb object its faster for a simple reason which is you only call the database, and the when you use $wpdb it loads extra functions and basically most of WordPress before you can use the $wpdb object.

What you should try is to use the WordPress Ajax api (not really an api but the right way to use ajax with WordPress). Basically you need to wrap your code in a function and make sure you die at the end:

function my_ajax_callback(){
    $getLetter = $_GET['letter'];
    global $wpdb;
    global $post;

    $result = $wpdb->get_results('SELECT * FROM wp_posts where post_title LIKE "'.$getLetter.'%" AND post_status = "publish" AND post_type="post"');
    while($row = $result)
    {
      echo '<a href="' . $row["guid"] . '">' . $row['post_title'] . '</a><br>';
    }
    die();
}

then you need add an action hook:

//if you want only logged in users to access this function use this hook
add_action('wp_ajax_ACTION_NAME', 'my_AJAX_processing_function');

//if you want none logged in users to access this function use this hook
add_action('wp_ajax_nopriv_ACTION_NAME', 'my_AJAX_processing_function');

*if you want logged in users and guests to access your function by ajax the add both hooks. *ACTION_NAME must match the action value in your ajax POST.

then just direct the ajax call to admin-ajax.php and add the ACTION_NAME:

<script type="text/javascript">
jQuery(function($) {
    $(document).ready(function() {
    $("#Loading").hide();
        $(".letter").bind('click', function(){
            $("#Loading").fadeIn(); //show when submitting
            var val = $(".letter").val;
            var data = {action: 'ACTION_NAME', letter: val};
            $.ajax({
                 url:'url to wp-admin/admin-ajax.php',
                 success:function(data){
                    $("#Loading").fadeOut('fast'); //hide when data's ready
                    $("#content1").html(data);
                 }
            });
        });
    });
});
</script>

this way it won't load all of WordPress just for that ajax call.

share|improve this answer
    
thanks I think this will work –  ntechi Aug 13 '11 at 14:37
    
Glad i could help. –  Bainternet Aug 13 '11 at 15:55
    
But still loading WordPress in a php file doesn't justify the 1 minute time to serve that ajax call. I agree it will be slower but not this much :P –  Ashfame Aug 13 '11 at 16:57
    
1 minute load time simply means that something is badly wrong. –  Bainternet Aug 13 '11 at 18:05
    
@Bainternet You're starting to emerge to a AJAX prof. Maybe you could add a new Q that explains it in detail and we make a community wiki out of it? –  kaiser Aug 15 '11 at 23:32

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.