Display meta data from one post on connected posts edit pages

Support MB Relationships Display meta data from one post on connected posts edit pages

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #12506
    Tamer KilincTamer Kilinc
    Participant

    Hi there!

    I am new to metabox relationships and I am really loving it. It makes everything a lot easier and faster 🙂

    I am currently having one problem. I have a custom post type to custom post type one to many relationship. The first custom post type is customer and the second is invoice. So one customer can have many invoices. The problem I have is that on the invoice edit page I can only see the connected customer, but I can't display any other data. I need some other data of the customer to display on the edit page (because on the invoice page you can create a PDF of the invoice on client-side and the invoice needs to display the customer data). I know I could create a lot more meta boxes like customer address, customer name and so on and fill them with customer data by retrieving the data after querying through connected posts and hooking into a hook like ,save_post'. But this would mean that all the data would be saved twice in the database, once in the table for customers and once in the table for invoices.

    I am asking myself if there is another and maybe easier option to show more data than the connected post name.

    Thank you for your time & help!

    #12519
    Anh TranAnh Tran
    Keymaster

    Hi Tamer,

    If all you want is just displaying customer info on the invoice edit page, then you can use a custom field custom_html to get their data and show. These data should be for customers, they are custom fields for customers. On the invoice edit page, you simply make a query and get the info.

    #12605
    Tamer KilincTamer Kilinc
    Participant

    Hi Anh,

    Thank you for your info. I have tried what you said and it works, thank you for this.

    Another question is:
    What is the best way if you want to save customer info on the invoice edit page.
    For example I am not only saving the customer of the invoice via metabox relationships, I am also saving the customer number as a seperate column in the invoice table. I do this because I need the customer number to build the invoice number.

    Right now I am doing it like this:

    I am hooking my function into save_post hook

    add_action( 'save_post', 'insert_kundennummer', 10, 3);

    I am looking if this saving is an auto-draft

    if ( $post->post_status == 'auto-draft' ) { return; }

    I am querying through the related posts, retrieve the post ID, get the custom meta field 'customer_number' and save it to the 'invoice_customernumber' field

    $wpdb->update( $table_name, array( 'invoice_customernumber' => $inv_cust_no ), array('ID' => $post_id) );

    Is this the best way to do this? Because I had some issues with WordPress auto-save feature I thought there is a better option to do this. Maybe you can help me. Thanks in advance!

Viewing 3 posts - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.