Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
emrikol opened this issue May 20, 2015 · 0 comments
Open

Comments

@emrikol
Copy link

emrikol commented May 20, 2015

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant