WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Sorting Doesn't work after using %s on query (3 posts)

  1. andresgh
    Member
    Posted 1 year ago #

    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()?

  2. ClaytonJames
    Member
    Posted 1 year ago #

    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.

    http://make.wordpress.org/core/2012/12/12/php-warning-missing-argument-2-for-wpdb-prepare/

  3. andresgh
    Member
    Posted 1 year ago #

    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.]

Topic Closed

This topic has been closed to new replies.

About this Topic