• Hello,

    I encountered a SQL syntax error in CoAuthorsPlus plugin when running a WP-CLI command wp co-authors-plus assign-user-to-coauthor. The error message is as follows:

    [11-Jul-2024 04:19:32 UTC] WordPress database error You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '','page)' at line 1 for query SELECT ID FROM wp_posts WHERE post_author=270 AND post_type IN (post','page) made by...

    The problem lies in the SQL query construction in co-authors-plus/php/class-wp-cli.php at line 238. The code currently is:

    $post_types = implode( "','", $coauthors_plus->supported_post_types() );

    $posts = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_author=%d AND post_type IN ({$post_types})", $user->ID ) );

    This results in an SQL syntax error because the post_type values are not properly enclosed in single quotes in the IN clause.

    Proposed Fix:

    // add single quotes
    $posts = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_author=%d AND post_type IN ('{$post_types}')", $user->ID ) );

    I hope this issue can be resolved quickly.

    Thank you!

  • You must be logged in to reply to this topic.