Title: [Plugin: Posts 2 Posts] SQL error
Last modified: August 20, 2016

---

# [Plugin: Posts 2 Posts] SQL error

 *  Resolved [salocined](https://wordpress.org/support/users/salocined/)
 * (@salocined)
 * [14 years, 1 month ago](https://wordpress.org/support/topic/plugin-posts-2-posts-sql-error/)
 * Sorry S, me again
 * Trying to update all my connections with new update/updated code.
 * 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 ‘) GROUP BY tp_posts.
   ID ORDER BY tp_posts.post_date DESC’ at line 1]
 * SELECT tp_posts.*, tp_p2p.* FROM tp_posts INNER JOIN tp_term_relationships ON(
   tp_posts.ID = tp_term_relationships.object_id) INNER JOIN tp_p2p WHERE 1=1 AND(
   tp_term_relationships.term_taxonomy_id IN (1082) ) AND tp_posts.post_type = ‘
   post’ AND (tp_posts.post_status = ‘publish’ OR tp_posts.post_status = ‘private’)
   AND () GROUP BY tp_posts.ID ORDER BY tp_posts.post_date DESC
 * from
 *     ```
       $connected = new WP_Query( array(
       'post_type' => 'post',
       'posts_per_page' => '5',
       'orderby' => 'rand',
       'connected_type' => 'posts_to_posts',
       'connected_items' => get_queried_object_id(),
       'nopaging' => true
       ) );
       ```
   
 * [http://wordpress.org/extend/plugins/posts-to-posts/](http://wordpress.org/extend/plugins/posts-to-posts/)

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

 *  Plugin Author [scribu](https://wordpress.org/support/users/scribu/)
 * (@scribu)
 * [14 years, 1 month ago](https://wordpress.org/support/topic/plugin-posts-2-posts-sql-error/#post-2657844)
 * Ok, and how does the ‘posts_to_posts’ connection type look like?
 *  Thread Starter [salocined](https://wordpress.org/support/users/salocined/)
 * (@salocined)
 * [14 years, 1 month ago](https://wordpress.org/support/topic/plugin-posts-2-posts-sql-error/#post-2657845)
 *     ```
       p2p_register_connection_type( array(
       'name' => 'posts_to_posts',
       'from' => 'post',
       'to' => 'post',
       'reciprocal' => true,
       'prevent_duplicates' => false
       ) );
       ```
   
 *  Thread Starter [salocined](https://wordpress.org/support/users/salocined/)
 * (@salocined)
 * [14 years, 1 month ago](https://wordpress.org/support/topic/plugin-posts-2-posts-sql-error/#post-2657847)
 * I actually do
    `$connected = new WP_Query( array( ‘post_type’ => array(‘post’,’
   vessel’), ‘cat’ => ‘-933’, ‘posts_per_page’ => ‘5’, ‘orderby’ => ‘rand’, ‘connected_type’
   => array(‘posts_to_posts’,’posts_to_vessels’), ‘connected_items’ => get_queried_object_id(),‘
   nopaging’ => true ) ); `
 * and my vessel connection is
    `p2p_register_connection_type( array( ‘name’ => ‘
   posts_to_vessels’, ‘from’ => ‘post’, ‘to’ => ‘vessel’, ‘reciprocal’ => true, ‘
   prevent_duplicates’ => false ) ); `
 * but even trying to strip it down, getting same error
 *  Plugin Author [scribu](https://wordpress.org/support/users/scribu/)
 * (@scribu)
 * [14 years, 1 month ago](https://wordpress.org/support/topic/plugin-posts-2-posts-sql-error/#post-2657849)
 * Also, what do you get when you do `var_dump( get_queried_object() );` ?
 *  Thread Starter [salocined](https://wordpress.org/support/users/salocined/)
 * (@salocined)
 * [14 years, 1 month ago](https://wordpress.org/support/topic/plugin-posts-2-posts-sql-error/#post-2657851)
 * object(stdClass)#717 (25) { [“ID”]=> int(1356) [“post_author”]=> string(1) “3”[“
   post_date”]=> string(19) “2011-05-26 09:32:40” [“post_date_gmt”]=> string(19)“
   2011-05-26 09:32:40” [“post_content”]=> string(2579) ” [caption id="attachment_9993"
   align="alignleft" width="240" caption="Title-II"][“post_title”]=> string(10) “
   Title II” [“post_excerpt”]=> string(0) “” [“post_status”]=> string(7) “publish”[“
   comment_status”]=> string(4) “open” [“ping_status”]=> string(6) “closed” [“post_password”]
   => string(0) “” [“post_name”]=> string(10) “Title-ii” [“to_ping”]=> string(0)“”[“
   pinged”]=> string(0) “” [“post_modified”]=> string(19) “2011-11-30 13:44:37” [“
   post_modified_gmt”]=> string(19) “2011-11-30 13:44:37” [“post_content_filtered”]
   => string(0) “” [“post_parent”]=> int(0) [“guid”]=> string(77) “[http://URL&p=1356&#8221](http://URL&p=1356&#8221);[“
   menu_order”]=> int(0) [“post_type”]=> string(6) “vessel” [“post_mime_type”]=>
   string(0) “” [“comment_count”]=> string(1) “0” [“ancestors”]=> array(0) { } [“
   filter”]=> string(3) “raw” }
 *  Plugin Author [scribu](https://wordpress.org/support/users/scribu/)
 * (@scribu)
 * [14 years, 1 month ago](https://wordpress.org/support/topic/plugin-posts-2-posts-sql-error/#post-2657861)
 * Thanks, I was able to reproduce the error. Will have a fix soon.
 *  Thread Starter [salocined](https://wordpress.org/support/users/salocined/)
 * (@salocined)
 * [14 years, 1 month ago](https://wordpress.org/support/topic/plugin-posts-2-posts-sql-error/#post-2657863)
 * Thx.
    Can you also look at the posts_per_page, as it doesnt seem to work, but
   was before. I am trying, as the code above to display only 5 related, at random.
   But getting the full list.
 * Sorry for the trouble
 *  Plugin Author [scribu](https://wordpress.org/support/users/scribu/)
 * (@scribu)
 * [14 years, 1 month ago](https://wordpress.org/support/topic/plugin-posts-2-posts-sql-error/#post-2657867)
 * It doesn’t work because you also pass `'nopaging' => true`.
 *  Plugin Author [scribu](https://wordpress.org/support/users/scribu/)
 * (@scribu)
 * [14 years, 1 month ago](https://wordpress.org/support/topic/plugin-posts-2-posts-sql-error/#post-2657877)
 * The SQL error should be fixed in the [development version](http://downloads.wordpress.org/plugin/posts-to-posts.zip)(
   1.2-alpha2).
 *  Thread Starter [salocined](https://wordpress.org/support/users/salocined/)
 * (@salocined)
 * [14 years, 1 month ago](https://wordpress.org/support/topic/plugin-posts-2-posts-sql-error/#post-2657920)
 * nopaging = silly me.
    will dig into alpha2 in the morning. thx again
 *  Thread Starter [salocined](https://wordpress.org/support/users/salocined/)
 * (@salocined)
 * [14 years, 1 month ago](https://wordpress.org/support/topic/plugin-posts-2-posts-sql-error/#post-2658010)
 * grr…
    ok it’s working some places, but not in other…
 * I am listing a post type, fine, and for each, inside the loop, I need to find
   if other articles are connected to the one in the loop.
    So I execute the query
   below. If an article connected if found, i display an icon (ok)…
 *     ```
       $the_query = new WP_Query( array(
       'post_type' => array('post', 'destination'),
       'connected_type' => array('posts_to_posts','posts_to_destinations'),
       'connected_items' => get_queried_object_id()
       ) );
                                             var_dump(get_queried_object_id());
   
       if($the_query->have_posts()) :
       echo "ok";
       else:
       echo "none";
       endif;
       wp_reset_postdata();
       ```
   
 * The dump, returns, int(0).
    if I try to use $post->ID, not working, but $post-
   >ID is correct in the loop, ie returns the proper id for the current record in
   the loop.
 *  Thread Starter [salocined](https://wordpress.org/support/users/salocined/)
 * (@salocined)
 * [14 years, 1 month ago](https://wordpress.org/support/topic/plugin-posts-2-posts-sql-error/#post-2658014)
 * Ok, I made 2 mistake in my query above.
 * 1/ Inside the loop I should use
    [https://github.com/scribu/wp-posts-to-posts/wiki/Looping-The-Loop](https://github.com/scribu/wp-posts-to-posts/wiki/Looping-The-Loop)
 * 2/ I was trying to find a connection that didnt exist.
 * Now, I actually ant to use this to only display an icon if a connection exist,
   and if try to do
 *     ```
       $connectedarticle = p2p_type('posts_to_destinations' )->get_connected( $post->ID );
       if($connectedarticle){
        echo display_article_icon($post->ID);
       }
       ```
   
 * it will display the icon, but on all item in loop and not only the one which 
   have the connection
 * if using
    `p2p_list_posts( $connectedarticle );`
 * it will only display articles where they are connected…
 * Can i do,
 *     ```
       if(p2p_list_posts){
        echo display_article_icon($post->ID);
       }
       ```
   
 * ?
 *  Thread Starter [salocined](https://wordpress.org/support/users/salocined/)
 * (@salocined)
 * [14 years, 1 month ago](https://wordpress.org/support/topic/plugin-posts-2-posts-sql-error/#post-2658017)
 * ok, what I needed to do was
 * Before my While…
    `p2p_type( 'posts_to_destinations' )->each_connected( $wp_query,
   array(), 'articles' );`
 * and in my loop
 *     ```
       if($post->articles){
       //do my thing
       }
       ```
   

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

The topic ‘[Plugin: Posts 2 Posts] SQL error’ is closed to new replies.

 * ![](https://s.w.org/plugins/geopattern-icon/posts-to-posts_7a8e9d.svg)
 * [Posts 2 Posts](https://wordpress.org/plugins/posts-to-posts/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/posts-to-posts/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/posts-to-posts/)
 * [Active Topics](https://wordpress.org/support/plugin/posts-to-posts/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/posts-to-posts/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/posts-to-posts/reviews/)

 * 13 replies
 * 2 participants
 * Last reply from: [salocined](https://wordpress.org/support/users/salocined/)
 * Last activity: [14 years, 1 month ago](https://wordpress.org/support/topic/plugin-posts-2-posts-sql-error/#post-2658017)
 * Status: resolved