WordPress.org

Ready to get started?Download WordPress

Forums

Posts 2 Posts
[resolved] Empty list of connections in version 1.6.2 (3 posts)

  1. AngryGerman
    Member
    Posted 6 months ago #

    I'm having a problem where connections that I create between instances of two custom post types (abc_person and abc_animal) show up fine in the admin interface, but not when I try to retrieve and render them on the archive pages for the custom post types.

    I am registering as follows:

    p2p_register_connection_type(array(
        'name' => 'persons_to_animals',
        'from' => 'abc_person',
        'to' => 'abc_animal',
        'cardinality' => 'one-to-many',
        'sortable' => 'any'
    ));

    Then I create connections, which show up as expected in the WP admin interface.

    But then, on the archive-abc_person template, regardless of whether I use

    $connected = get_posts(array(
        'connected_items' => $wp_query->posts,
        'connected_type' => 'persons_to_animals',
        'connected_direction' => 'from',
        'nopaging' => true,
        'suppress_filters' => false
    ));
    p2p_distribute_connected($wp_query->posts, $connected, 'connected');
    
    if (have_posts()) : while (have_posts()) : the_post();
    ...etc...

    or

    p2p_type('persons_to_animals')->each_connected($wp_query);
    
    if (have_posts()) : while (have_posts()) : the_post();
    ...etc...

    I always receive an empty array back as $post->connected

    The archive template is otherwise plain and doesn't have any other relevant code in it that would affect the loop.

    What am I doing wrong?

    http://wordpress.org/plugins/posts-to-posts/

  2. AngryGerman
    Member
    Posted 6 months ago #

    Tracked this down a little further.

    Looks like on the single custom post page, inside the call to each_connected(), P2P_Directed_Connection_Type->abstract_query() runs the following SQL query (which completes and returns the correct connected abc_animals):

    SELECT SQL_CALC_FOUND_ROWS  wp_posts.*, wp_p2p.* FROM wp_posts  INNER JOIN wp_p2p
    				LEFT JOIN wp_p2pmeta AS p2pm_order ON (
    					wp_p2p.p2p_id = p2pm_order.p2p_id AND p2pm_order.meta_key = '_order_from'
    				)
    			 WHERE 1=1  AND wp_posts.post_type = 'abc_animal' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private') AND (wp_p2p.p2p_type = 'persons_to_animals' AND wp_posts.ID = wp_p2p.p2p_to AND wp_p2p.p2p_from IN (SELECT   wp_posts.ID FROM wp_posts  WHERE 1=1  AND wp_posts.ID IN (2706) AND wp_posts.post_type = 'abc_person' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private')  ORDER BY wp_posts.post_date DESC ))  ORDER BY p2pm_order.meta_value+0 ASC, wp_posts.post_date DESC LIMIT 0, 10

    However, on the custom post archive page, the SQL query that gets run internally by P2P is as follows:

    SELECT   wp_posts.*, wp_p2p.* FROM wp_posts  INNER JOIN wp_p2p
    				LEFT JOIN wp_p2pmeta AS p2pm_order ON (
    					wp_p2p.p2p_id = p2pm_order.p2p_id AND p2pm_order.meta_key = '_order_from'
    				)
    			 WHERE 1=1  AND wp_posts.post_type = 'abc_animal' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private') AND (wp_p2p.p2p_type = 'persons_to_animals' AND wp_posts.ID = wp_p2p.p2p_to AND wp_p2p.p2p_from IN (SELECT SQL_CALC_FOUND_ROWS  wp_posts.ID FROM wp_posts  WHERE 1=1  AND wp_posts.ID IN (2706) AND wp_posts.post_type = 'abc_person' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private')  ORDER BY wp_posts.post_date DESC LIMIT 0, 30))  ORDER BY p2pm_order.meta_value+0 ASC, wp_posts.post_date DESC

    This fails based on the SQL_CALC_FOUND_ROWS statement inside the sub-query, as well as based on the LIMIT 0, 30 in that same sub-query. Which seems to be why the archive page returns an empty array.

    Is this a bug, or does P2P just not work on archive pages?

  3. AngryGerman
    Member
    Posted 6 months ago #

    Based on the fact that even the new WP_Query() call in P2P's do_query() returns a new WP_Query with posts_per_page set to 30, the issue seemed to lie outside of P2P.

    Turns out the rest of the code was overriding pre_get_posts elsewhere and set posts_per_page to 30 for archive pages.

    Filter adjusted, problem fixed.

Reply

You must log in to post.

About this Plugin

About this Topic