• Resolved andresgh

    (@andresgh)


    Hello,
    I am currently building a plugin that allows to add images to a comment. I have created a table for comments with images in the admin panel with sorting. Since adding %s and on prepare it no longer sorts.

    WP 3.4
    SELECT * FROM $wpdb->comments, $wpdb->commentmeta WHERE $wpdb->comments.comment_ID = $wpdb->commentmeta.comment_ID ORDER BY $orderby $order

    would result in this query:
    SELECT * FROM sanap_comments, sanap_commentmeta WHERE sanap_comments.comment_ID = sanap_commentmeta.comment_ID ORDER BY comment_post_ID desc

    WP 3.5
    SELECT * FROM $wpdb->comments, $wpdb->commentmeta WHERE $wpdb->comments.comment_ID = $wpdb->commentmeta.comment_ID ORDER BY %s %s

    Results in:
    SELECT * FROM sanap_comments, sanap_commentmeta WHERE sanap_comments.comment_ID = sanap_commentmeta.comment_ID ORDER BY ‘comment_post_ID’ ‘asc’

    Anyone know what I need to change to make it work with the $wpdb->prepare()?

Viewing 2 replies - 1 through 2 (of 2 total)
  • This is just a guess mind you, but take a look at this article and see if there’s anything there that might be of help in finding a solution. It looks pretty similar in subject.

    PHP Warning: Missing argument 2 for wpdb::prepare()

    Thread Starter andresgh

    (@andresgh)

    Hello ClaytonJames,
    Thanks for the help, I didn’t find anything to help me (well maybe some ideas).

    What I did was add:

    if( empty($orderby) && empty($order)){
    /*
    * I know that initial load of the table will not be control by the user
    * that its save to assume they wont be able to inject anything
    */
    $orderby = !empty( $_GET["orderby"] ) ? mysql_real_escape_string( $_GET["orderby"] ) : " comment_date ";
    $order = !empty( $_GET["order"] ) ? mysql_real_escape_string( $_GET["order"] ): "desc";
    
    $query .= " ORDER BY $orderby $order "; 	
    
    }else{
    /*
    * But when they sort the user can see the variables in the URL
    * that's when we stick with WordPress
    */
    $query .= " ORDER BY %s %s "; 
    
    }

    [Moderator Note: Please post code or markup snippets between backticks or use the code button. As it stands, your posted code may now have been permanently damaged/corrupted by the forum’s parser.]

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Sorting Doesn't work after using %s on query’ is closed to new replies.