Support » Plugin: Posts 2 Posts » [Plugin: Posts 2 Posts] Can't get connections with custom query

  • I am using a custom query on a page template to grab a random custom post type. Then within the loop of that custom query trying to grab the related posts. None of the related posts are showing up.

    Here is my entire page template:

    <?php get_header(); ?>
    
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    
    	<div id="page-header">
    		<h1><?php the_title(); ?></h1>
    	</div>
    
    	<div id="main">
    		<section role="main">
    			<?php the_content(''); ?>
    
    			<?php endwhile; endif; ?>	
    
    			<?php
    			$the_query = new WP_Query( 'post_type=installations&posts_per_page=1' );
    			while ( $the_query->have_posts() ) : $the_query->the_post();
    			?>
    				<div class="installation-content">
    					<?php the_content(); ?>
    
    						<?php
    						// Find connected pages
    						$connected = new WP_Query( array(
    						  'connected_type' => 'gallery_to_products',
    						  'connected_items' => get_queried_object(),
    						  'nopaging' => true
    						) );
    
    						// Display connected pages
    						if ( $connected->have_posts() ) :
    							p2p_list_posts( $connected, array(
    								'before_list' => '<h2>Products Used</h2><ul>',
    								'after_list'  => '</ul>',
    								'before_item' => '<li>',
    								'after_item'  => '</li>',
    							) );
    
    						endif;
    						?>
    				</div>
    
    			<?php endwhile; wp_reset_postdata(); ?>
    		</section>
    	</div>
    
    <?php get_footer(); ?>

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

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author scribu

    (@scribu)

    get_queried_object() always refers to the initial post.

    So, you have to use $post instead.

    Didn’t seem to work, maybe I am using it wrong. I get the following error:

    Fatal error: Call to undefined method stdClass::get_queried_object() in /home/ashweb/public_html/oasisstone/wp-content/themes/oasis/page-installations.php on line 26

    My new code:

    <?php
    // Find connected pages
    $connected = new WP_Query( array(
      'connected_type' => 'gallery_to_products',
      'connected_items' => $post->get_queried_object(),
      'nopaging' => true
    ) );
    
    	p2p_list_posts( $connected, array(
    		'before_list' => '<h2>Products Used</h2><ul>',
    		'after_list'  => '</ul>',
    		'before_item' => '<li>',
    		'after_item'  => '</li>',
    	) );
    ?>

    Line 26 is

    'connected_items' => $post->get_queried_object(),

    Plugin Author scribu

    (@scribu)

    No, not $post->get_queried_object(). Just $post.

    <?php
    // Find connected pages
    $connected = new WP_Query( array(
      'connected_type' => 'gallery_to_products',
      'connected_items' => $post,
      'nopaging' => true
    ) );
    
    	p2p_list_posts( $connected, array(
    		'before_list' => '<h2>Products Used</h2><ul>',
    		'after_list'  => '</ul>',
    		'before_item' => '<li>',
    		'after_item'  => '</li>',
    	) );
    ?>

    Still nothing showing, but this exact same code works perfectly on the single template file.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘[Plugin: Posts 2 Posts] Can't get connections with custom query’ is closed to new replies.