Tagged: 

Viewing 8 reply threads
  • Author
    Posts
    • #1417
      Jason
      Keymaster

      Greetings!

      I have three post types that each have a relationship with a fourth, people. I have a metabox in all three that allows the user to select the people involved. It appears to work well, but I’m having issues retrieving the related posts. Here’s my query:

      public function recent_posts($count = 4, $offset = 0) {
      	$args = array(
      		'post_type'				=>	array('media', 'stories', 'projects'), 
      		'posts_per_page'		=> $count,
      		'offset'					=> $offset,
      		'post_belongs'			=> $this->id
      	);
      
      	return get_posts($args);
      }

      $this->id refers to the id of the current people id. If I understand correctly, this should retrieve all the media, stories, and project post types that are related to the current id. Unfortunately, it currently returns every post.

      Am I misunderstanding something?

      Thanks!

    • #1418
      Kevin
      Keymaster

      Jason-

      I actually laughed a little when I read this for no other reason than I got stuck on this myself a few months back. By default get_posts has suppress_filters set to true, so any filters designed to modify the query are not executed. Set suppress_filters to false and things should work as expected.

      Thanks,

      Kevin

    • #1422
      Steve
      Keymaster
    • #1424
      Jason
      Keymaster

      Nice. That’s definitely one of those argument parameters I’ve never paid attention to. Makes sense!

      Unfortunately, now I’m not getting any posts, but I believe I understand why. The people are being tagged from the other post types, which means the post_id in the db is, for example, projects, and the has_post_id is the person. I assume you’re doing the query in direction of joining the id to wp_posts.id to wp_post_relationships.post_id. This means the relationship is one-sided.

      Do you happen to have an argument that joins to the has_post_id column? Otherwise I’ll need to write my own query.

      Thanks!

      EDIT: Just found post_has, please add this to the docs! I didn’t see it in there and it’s exactly what I was talking about! 🙂

    • #1425
      Steve
      Keymaster
    • #1426
      Jason
      Keymaster

      Thanks, Steve! Updated doc is perfect.

    • #5527
      Anthony
      Member

      Is it possible to use the new WP Query to achieve the same as the get_posts() function? Like:

       $paged = ( get_query_var('page') ) ? get_query_var('page') : 1;
        $query_args = array(
          'post_type' => 'post',
          'paged' => $paged,
          'post_has' => $post->ID,
          'suppress_filters' => false     
        );
        // create a new instance of WP_Query
        $the_query = new WP_Query( $query_args );
      
      if ( $the_query->have_posts() ) :
      while ( $the_query->have_posts() ) : $the_query->the_post();
      

      Would prefer to use this kind of loop instead of a for_each

    • #5529
      Anthony
      Member

      I ask cause I wanted to paginate the results.

    • #5533
      Jason
      Keymaster

      @anthonyabraira: Yup! You can go ahead and use wp_query like you normally would. The relate code just adds to the query SQL; it doesn’t mess up any existing functionality. 🙂

      • #5534
        Anthony
        Member

        @Jason: I will have to test again, it didn’t work for me. Do you have a quick snippet to build off from? Perhaps link to another thread where this has been demonstrated?

      • #5535
        Jason
        Keymaster

        Your code above looks fine. I wouldn’t write a snippet any differently. Let’s debug a bit:

        What version of Piklist are you using?

        When you say it “didn’t work”, what exactly is happening?

        Does it work if you remove the past_has?

        Are you getting the results expected if you remove pagination? Is the only issue that they’re not paginating?

Viewing 8 reply threads
  • You must be logged in to reply to this topic.