query - What's wrong with my $wpdb prepare?

admin2025-06-02  2

I can't get my head around this. I get this error:

Parse error: syntax error, unexpected '"' in 224. The line is this between foreach $html:

$html = '';
   foreach ( $recent_across_network as $post ) {
      $html .= 'blog_id, $post->ID ) . '">' . $post->post_title . '';
   }
   $html .= '';

Also I get error on $wpdb-prepare:

$site_list = $wpdb->get_results( $wpdb->prepare('SELECT * FROM wp_blogs ORDER BY blog_id') );

prepare I believe would need to have to arguments? So I tried this:

$site_list = $wpdb->get_results( $wpdb->prepare('SELECT * FROM wp_blogs ORDER BY blog_id = %d', $wpdb->blogid) );

But that doesn't work either.

EDIT: The code is based on the function "wp_recent_across_network" on this article: /

I can't get my head around this. I get this error:

Parse error: syntax error, unexpected '"' in 224. The line is this between foreach $html:

$html = '';
   foreach ( $recent_across_network as $post ) {
      $html .= 'blog_id, $post->ID ) . '">' . $post->post_title . '';
   }
   $html .= '';

Also I get error on $wpdb-prepare:

$site_list = $wpdb->get_results( $wpdb->prepare('SELECT * FROM wp_blogs ORDER BY blog_id') );

prepare I believe would need to have to arguments? So I tried this:

$site_list = $wpdb->get_results( $wpdb->prepare('SELECT * FROM wp_blogs ORDER BY blog_id = %d', $wpdb->blogid) );

But that doesn't work either.

EDIT: The code is based on the function "wp_recent_across_network" on this article: http://www.smashingmagazine/2011/11/17/wordpress-multisite-practical-functions-methods/

Share Improve this question edited Feb 26, 2019 at 9:08 Dave 2003 silver badges15 bronze badges asked Aug 14, 2014 at 20:40 user995317user995317 1652 silver badges9 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 2

These look like 2 separate questions.

The first I think is a single vs double quotes issue:

Try this:

$html = '';
foreach ( $recent_across_network as $post ) {
    $html .= 'blog_id, '.$post->ID.' ) . '">' . $post->post_title . '';
}
$html .= '';

The line in the foreach is putting $post->ID in single quotes which won't evaluate it's value but rather put that exact string into $html.

For the prepare issues, try wrapping your query in double quotes and passing an actual value as the parameter.

Also you should access the table via the helper and not directly as is the table prefix is not wp_ your query will not work. Take a read of the WPDB class referencefor the details of prepare, table names etc

$site_list = $wpdb->get_results( $wpdb->prepare('SELECT * FROM wp_blogs ORDER BY blog_id = %d', $wpdb->blogid) );
//becomes
$site_list = $wpdb->get_results( $wpdb->prepare("SELECT * FROM $wpdb->blogs ORDER BY blog_id = %d", 1) );

Disclaimer - I haven't tested this!

Hope it helps

You don't need to invoke $wpdb->prepare() since your original SQL query doesn't contain any input from the user. Your 2nd query -- SELECT * FROM wp_blogs ORDER BY blog_id = %d -- has a syntax error (the ORDER BY clause won't accept the = %d), and so won't work.

(At least I'm pretty sure that's the case. I can't find anything in the MySQL docs to indicate that ORDER BY blog_id=%d would work, anyways.)

Also, if you're trying to get a list of sites in a Multisite installation, I recommend using wp_get_sites() instead of reinventing the wheel.

I am rewriting the entire function "wp_recent_across_network" mentioned.

1) With Using Transients API

function wp_recent_across_network( $size = 10, $expires = 7200 ) {
    if( !is_multisite() ) return false;

    // Cache the results with the WordPress Transients API
    // Get any existing copy of our transient data
    if ( ( $recent_across_network = get_site_transient( 'recent_across_network' ) ) === false ) {

        // No transient found, regenerate the data and save a new transient
        // Prepare the SQL query with $wpdb
        global $wpdb;

        // Because the get_blog_list() function is currently flagged as deprecated
        // due to the potential for high consumption of resources, we'll use
        // $wpdb to roll out our own SQL query instead. Because the query can be
        // memory-intensive, we'll store the results using the Transients API
        if ( false === ( $site_list = get_site_transient( 'multisite_site_list' ) ) ) {
            $site_list = $wpdb->get_results( 'SELECT * FROM ' . $wpdb->prefix . 'blogs ORDER BY blog_id' );
            set_site_transient( 'multisite_site_list', $site_list, $expires );
        }

        $limit = absint($size);

        // Merge the wp_posts results from all Multisite websites into a single result with MySQL "UNION"
        foreach ( $site_list as $site ) {
            if( $site == $site_list[0] ) {
                $posts_table = $wpdb->prefix . "posts";
            } else {
                $posts_table = $wpdb->prefix . $site->blog_id . "_posts";
            }

            $posts_table = esc_sql( $posts_table );
            $blogs_table = esc_sql( $wpdb->prefix . 'blogs' );

            $query .= "(SELECT $posts_table.ID, $posts_table.post_title, $posts_table.post_date, $blogs_table.blog_id FROM $posts_table, $blogs_table";
            $query .= " WHERE $posts_table.post_type = 'post'";
            $query .= " AND $posts_table.post_status = 'publish'";
            $query .= " AND $blogs_table.blog_id = {$site->blog_id})";

            if( $site !== end($site_list) )
                $query .= " UNION ";
                else
                    $query .= " ORDER BY post_date DESC LIMIT 0, $limit";
        }

        // Sanitize and run the query
        $recent_across_network = $wpdb->get_results( $query );

        // Set the Transients cache to expire every two hours
        set_site_transient( 'recent_across_network', $recent_across_network, 60*60*2 );
    }
    // Format the HTML output
    if($recent_across_network) {
        $html = '<ul>';
        foreach ( $recent_across_network as $post ) {
            $html .= '<li><a href="'.get_blog_permalink($post->blog_id, $post->ID).'">'.$post->post_title.'</a></li>';
        }
        $html .= '</ul>';
        echo $html;
    }
}

2) Without Using Transients API

function wp_recent_across_network1( $size = 10) {
    if( !is_multisite() ) return false;

    // Prepare the SQL query with $wpdb
    global $wpdb;
    $site_list = $wpdb->get_results( 'SELECT * FROM ' . $wpdb->prefix . 'blogs ORDER BY blog_id' );

    $limit = absint($size);

    // Merge the wp_posts results from all Multisite websites into a single result with MySQL "UNION"
    foreach ( $site_list as $site ) {
        if( $site == $site_list[0] ) {
            $posts_table = $wpdb->prefix . "posts";
        } else {
            $posts_table = $wpdb->prefix . $site->blog_id . "_posts";
        }

        $posts_table = esc_sql( $posts_table );
        $blogs_table = esc_sql( $wpdb->prefix . 'blogs' );

        $query .= "(SELECT $posts_table.ID, $posts_table.post_title, $posts_table.post_date, $blogs_table.blog_id FROM $posts_table, $blogs_table";
        $query .= " WHERE $posts_table.post_type = 'post'";
        $query .= " AND $posts_table.post_status = 'publish'";
        $query .= " AND $blogs_table.blog_id = {$site->blog_id})";

        if( $site !== end($site_list) )
            $query .= " UNION ";
            else
                $query .= " ORDER BY post_date DESC LIMIT 0, $limit";
    }

    // Sanitize and run the query
    $recent_across_network = $wpdb->get_results( $query );

    // Format the HTML output
    if($recent_across_network) {
        $html = '<ul>';
        foreach ( $recent_across_network as $post ) {
            $html .= '<li><a href="'.get_blog_permalink($post->blog_id, $post->ID).'">'.$post->post_title.'</a></li>';
        }
        $html .= '</ul>';
        echo $html;
    }
}

Changes done are listed below

  • no need to invoke $wpdb->prepare() because there is no variable passed in query
  • Corrected broken code in your reference website
  • Changed the prefix variable to $wpdb->prefix
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748810204a313909.html

最新回复(0)