WordPress.org

Ready to get started?Download WordPress

Forums

Posts 2 Posts
[resolved] WP_Query() x get_posts() (3 posts)

  1. pavelevap
    Member
    Posted 3 years ago #

    These values are in database table:

    id,from,to
    2,2478,3910
    3,4075,3910

    I tried to connect posts to pages, so there are two posts (2478 and 4075) connected to page 3910.

    Then I wanted to list connected posts to my page:

    $sc_query = new WP_Query( array(
      'suppress_filters' => false,
      'post_type' => 'post',
      'connected_to' => $post->ID) );
    
    if($sc_query->have_posts()) :
    
    echo '<ul>';
    
    while($sc_query->have_posts()):$sc_query->the_post(); ?>
    <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    
    <?php
    endwhile;
    echo '</ul>';
    endif;
    wp_reset_query();

    $post->ID is correct 3910, but there are both connected posts with many others? I do not understand why, because in p2p table are only these 2 rows?

    And when I use get_posts(), everything works well:

    $connected = get_posts( array(
      'suppress_filters' => false,
      'post_type' => 'post',
      'connected_to' => $post->ID,
    ) );
    
    echo '<ul>';
    
    foreach( $connected as $post ) : setup_postdata($post); ?>
    <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    <?php endforeach;
    
    echo '</ul>';

    I am out of ideas... Same query, different results?

  2. pavelevap
    Member
    Posted 3 years ago #

    Oh, probably my mistake, not related to Posts 2 Posts plugin, but interesting...

    Problem was related to parametr caller_get_posts which was false by default, but I had there many sticky post and so they were passed automatically into WP_Query()...

    Adding 'caller_get_posts' => true solved my troubles...

  3. scribu
    Member
    Plugin Author

    Posted 3 years ago #

    Yeah, makes sense, since get_posts() automatically sets some parameters.

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic