• Hi guys. I have a 3.5 problem with a custom made theme. It’s this line:

    Can any of you help me with what can cause my problem:
    Warning: Missing argument 2 for wpdb::prepare(), called in….

    this line:

    $request = $wpdb->prepare("SELECT ID, post_title, post_date, post_excerpt,LEFT(post_content,$sqllimit) AS short_post_content FROM $wpdb->posts WHERE post_status = 'publish' ");

Viewing 5 replies - 1 through 5 (of 5 total)
  • The prepare function is used to securely substitute placeholders in a SQL query with the corresponding values.

    You can read on how to use it properly and why this error started occurring in WordPress 3.5 here: http://make.wordpress.org/core/2012/12/12/php-warning-missing-argument-2-for-wpdb-prepare/

    Assuming $sqllimit is an integer, you should replace it with %d and pass the variable in as the second parameter to the prepare function.

    Thread Starter karsteng

    (@karsteng)

    Oh I guess I am not that clever when it comes to code ;-). I tried changing it to %d but then my site was really weird. Can anyone guide me?

    Andrew Nevins

    (@anevins)

    WCLDN 2018 Contributor | Volunteer support

    Try this

    $post_status = 'publish';
    $request = $wpdb->prepare("SELECT ID, post_title, post_date, post_excerpt,LEFT(post_content,$sqllimit) AS short_post_content FROM $wpdb->posts WHERE post_status = %d ", $post_status);

    Andrew – If you use %d as a placeholder, then it will expect an integer, not a string.

    Something like this should work (not tested):
    $request = $wpdb->prepare("SELECT ID, post_title, post_date, post_excerpt,LEFT(post_content, %d) AS short_post_content FROM $wpdb->posts WHERE post_status = 'publish' ", $sqllimit);

    Let me know if that resolves the issue. It’s also worth reading the Codex on the WPDB class to get a better understanding of it: http://codex.wordpress.org/Class_Reference/wpdb

    Thread Starter karsteng

    (@karsteng)

    Damian… You are a hero 🙂 It worked perfect!! Thanks a lot!

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘Problem with template’ is closed to new replies.