WordPress.org

Ready to get started?Download WordPress

Forums

Posts 2 Posts
Can't get connections with custom query (6 posts)

  1. sccr410
    Member
    Posted 2 years ago #

    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/

  2. scribu
    Member
    Plugin Author

    Posted 2 years ago #

    get_queried_object() always refers to the initial post.

    So, you have to use $post instead.

  3. sccr410
    Member
    Posted 2 years ago #

    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>',
    	) );
    ?>
  4. sccr410
    Member
    Posted 2 years ago #

    Line 26 is

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

  5. scribu
    Member
    Plugin Author

    Posted 2 years ago #

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

  6. sccr410
    Member
    Posted 2 years ago #

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

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic

Tags

No tags yet.