This topic contains 6 replies, has 2 voices, and was last updated by  xolotl 2 years, 11 months ago.

  • Author
    Posts
  • #6171

    xolotl
    Participant

    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:
    cpt_relate_remove($postID, $relatedPostID);
    cpt_relate($postID, $relatedPostID);

  • #6176

    Steve
    Keymaster

    @xolotl– Welcome to the Piklist community!

    If you’re using Relationships, you probably want to use our latest beta which is a total rewrite and waaaaaaay more powerful. Download it here >

  • #6179

    xolotl
    Participant

    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?

  • #6187

    xolotl
    Participant

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

  • #6206

    xolotl
    Participant

    Update:

    I see in the latest Piklist beta that @sbruner guided me to that post-to-post relationships are no longer stored in a separate table, but in wp_postmeta. That makes it more obvious how to change relationship values via a custom function. I’ll experiment and post my results.

  • #6218

    Steve
    Keymaster

    Great. Looking forward to hearing what you think.

  • #6268

    xolotl
    Participant

    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.