Tagged: post-relate
- This topic has 10 replies, 4 voices, and was last updated 6 years, 1 month ago by
Jason.
-
AuthorPosts
-
-
February 17, 2014 at 6:16 pm #1417
JasonKeymasterGreetings!
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!
-
February 18, 2014 at 12:12 am #1418
KevinKeymasterJason-
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
-
February 18, 2014 at 7:08 am #1422
SteveKeymaster -
February 18, 2014 at 10:47 am #1424
JasonKeymasterNice. 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! 🙂
-
February 18, 2014 at 2:45 pm #1425
SteveKeymaster@jason– Docs updated > Thanks.
-
February 18, 2014 at 2:51 pm #1426
JasonKeymasterThanks, Steve! Updated doc is perfect.
-
January 8, 2016 at 5:52 pm #5527
AnthonyMemberIs it possible to use the
new WP Queryto achieve the same as theget_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 -
January 8, 2016 at 9:43 pm #5529
AnthonyMemberI ask cause I wanted to paginate the results.
-
January 9, 2016 at 12:39 pm #5533
JasonKeymaster@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. 🙂
-
January 9, 2016 at 12:41 pm #5534
-
January 9, 2016 at 12:49 pm #5535
JasonKeymasterYour 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?
-
-
-
AuthorPosts
- You must be logged in to reply to this topic.