• Resolved carlfost

    (@carlfost)


    I am looking for a programmatic solution to managing relationships from php ($wpdb) or javascript (ajax), but I cannot find online any references or examples of how to do so.
    From the admin page, I was able to add a custom post_type called Registration and another one called Events. I set up a relationship field in the Registration post_type called “registered_event”. From the admin screen I can drag and add or remove an event from the Registration post_type, but I would like to do the same programmatically. I cannot find a solution online how to manage relationships programmatically.
    When I programmatically create a registration record in the wp_posts table and also create a record in the wp_postmeta table: metakey=”registered_event” and metavalue=”51” (which is the ID for the Event record), the WordPress get_field(“registered_event”) command does not return an array of the Event record. If I manually edit any field of the registration record from the admin screen and save it, then the get_field(“registered_event”) returns the correct data.
    How can I manage WordPress relationships programmatically in php ($wpdb) or javascripts (ajax)?

    The page I need help with: [log in to see the link]

Viewing 4 replies - 1 through 4 (of 4 total)
  • There is no built-in WordPress thing called relationship between two post types or two posts.
    If you are using a plugin to create the post types, you can ask your question at the plugin’s support forum.
    Otherwise, can you use a different term to describe what you want?

    Thread Starter carlfost

    (@carlfost)

    OK. It must be created by the Plugin Custom Design. I thought it was native to WordPress.
    This picture shows the difference between when I created the registration record from the admin screen, and when I programatically created it.
    Relationship Field

    For the difference that you’re seeing in the database, you’ve stored an integer valur, while which ever theme/plugin is doing it for you it storing the value as a serialised array. All you have to do is store the value as a serialised array too and you’ll see the same thing.

    Thread Starter carlfost

    (@carlfost)

    I found out that the Advance Custom Field (ACF) plugin setup the relationship field, and to update it, I use update_field(‘custom field name’,’related post ID, ‘wp_posts ID’);

    • This reply was modified 6 years, 8 months ago by carlfost.
Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Managing Relationships Programmatically’ is closed to new replies.