March 23, 2016 at 11:46 pm #6171
I’m trying to figure out the most elegant way to change the posts related to another post via a function. I can see the relations stored in the DB in wp_post_relationships and I see evidence that there are programmatic ways to change the relations, but I don’t see “the piklist way” to change related posts.
For example, if my post type were named “cpt”, I’m thinking there might be a functions like:
March 24, 2016 at 11:11 am #6176
March 24, 2016 at 12:30 pm #6179
Thanks @sbruner! I got the latest beta and it definitely appears to be a quantum leap!
Maybe I’m blind, but still not finding an obvious way to change related posts other than the metabox.
My use case is to send a user from an instance of one CPT (let’s call it the parent) to a very large collection of posts of another CPT (let’s call them the children), one of which the user would select to relate to the original parent CPT. The user might also need to make a new child to relate.
It seems like too much to try to handle that all via the parent form, so I’m using sessions to enable the user to leave the parent, explore the children, and choose a child to relate to the parent.
I’m looking for a way to have a form button (or link) on the child that fires a function that relates that child back to the parent (which is known in the session). I just can’t seem to find the right call to make in my function to establish the relation to the parent. Do I have to update the wp_post_relationships table myself, or is there function that already does that?
March 24, 2016 at 7:35 pm #6187
@gabzon & @sajonara: It seems like you both were looking to do something similar to what I’m attempting. I was hoping to use the CPT-CPT relation standard in Piklist rather than storing the relation in a separate meta field as in @sajonara’s solution.
Have either of you discovered a way to make a standard Piklist relation via a function?
March 29, 2016 at 2:32 pm #6206
April 1, 2016 at 8:38 pm #6218SteveKeymaster
Great. Looking forward to hearing what you think.
April 10, 2016 at 2:05 pm #6268
I’m now able to effectively manage post-to-post (PTP) relationships programmatically via my own custom function.
As things stand in Piklist 0.9.4.29, posts are related by setting postmeta values on the __relate_post key. So for example, if post ID 1 were related to post ID 2, there would be these two rows in the wp_postmeta table:
post_id, meta_key, meta_value --- 1, __relate_post, 2 2, __relate_post, 1
And one can set/unset such postmeta values using standard WP functions like:
// add a specific PTP relationship update_post_meta($postID, '__relate_post', $postIDrelated); // remove a specific PTP relationship delete_post_meta($postID, '__relate_post', $postIDrelated);
Note that each relationship sets two postmeta values, one for each related post relating it to the other. Obviously there may be many relationships for any given post to any number of other posts, so to change more/all relationships, one would have to do some querying/looping to make broader changes. Read up on on how update_post_meta and delete_post_meta work to understand how to change one or more relationships.
@sbruner: Thanks for the help! I consider this inquiry resolved.
- You must be logged in to reply to this topic.