Skip to content

SQL Errors from O2O_Connection_Taxonomy::get_connected_[*]_objects() #11

@emrikol

Description

@emrikol

Under the right conditions (or wrong conditions, depending on how you look at it), get_connected_[*]_objects() returns a WP_Error object that results in a SQL error:

Warning: implode(): Invalid arguments passed in /wp-includes/query.php on line 2658

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 ') AND wp_posts.post_type = 'post' AND (wp_posts.post_status = ' at line 1 for query SELECT wp_posts.ID FROM wp_posts WHERE 1=1 AND wp_posts.ID IN () AND wp_posts.post_type = 'post' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private') ORDER BY wp_posts.post_date DESC LIMIT 0, 3 made by WP_Query::get_posts/

An example of how this can happen is if you want to set up a "related posts" section for the post post type in single.php:

...

add_action('init', 'o2o_related_posts');
function o2o_related_posts() {
    O2O::Register_Connection('o2o_related_posts', 'post', 'post', array(
        'reciprocal' => true,
        'to'         => array(

            'sortable' => true,
            'labels'   => array(

                'name'          => 'Related Posts',
                'singular_name' => 'Related Post'
            )
        ),
        'from'       => array(
            'labels' => array(
                'name'          => 'Posts',
                'singular_name' => 'Post'
            )
        )
    ) );
}

$o2o_query = new WP_Query( array(
    'o2o_query' => array(
        'connection' => 'o2o_related_posts',
        'id' => get_queried_object_id()
) ) );

...

If you don't check that 'post' !== get_post_type() then you will get the WP_Error object returned on all custom post type single posts.

One fix for this would be to just return an empty array() instead of the WP_Error object, but that doesn't address the need to notify the developer that they may be using the wrong post type (perhaps throw a PHP Warning or Notice?).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions