Support Forum
Support › MB Custom Post Type › get the value of a custom field from a cpt when not using "the loop"Resolved
Anh, hi!
My apologies for asking about something that is probably something very elementary.
But I'm a little confused and would like to know the most efficient way of doing the following:
I've created a cpt called "Customer File".
The title of each post is the customer #.
Each post has 3 custom fields:
- name,
- address,
- phone.
So, I'm working on an archive or Search Results type of page.
The page is called Monthly Invoices.
The invoices are not posts or custom posts that are retrieved the normal way (that is, with wp_query & displayed by going through the loop.)
Instead, my code makes an api call to a remote server that returns an array of invoices for the requested month.
So, my code loops through the array of invoices (using foreach) & outputs the invoice data for each invoice.
Each element of the array is an invoice that contains invoice data, but the only customer info is the customer #. So, I need to get the post from the "Customer File" cpt where the title of the post matches the "customer #" of the invoice (that I got from the api.) & then display the values for the "name", "address", "phone" custom fields of that customer post.
So, as I'm looping through the "Invoice" array, what's the right way to take the $customer_number field in the array, retrieve the corresponding post in the "Customer File" cpt & display the values of the 3 custom fields?
I hope I've explained this clearly.
Thanks, James
Hi James,
Could you please share the structure of the Invoices array? Assume that the invoices return the array like this and you can use the for loop to get the name of the customer
$invoices = array(
array(
'name' => 'John',
'price' => 10
),
array(
'name' => 'Marry',
'price' => 20
),
array(
'name' => 'Doe',
'price' => 30
)
);
foreach ( $invoices as $number => $invoice ) {
if( $invoice['name'] == $your_query->post->post_title /*John*/ ) {
echo 'the price is: ' . $invoice['price'];
// echo rwmb_meta('your_field_id');
}
}
Long, thank you for the quick response.
I hope this makes things clearer.
$invoices = array(
array(
'customernumber' => '101',
'invoicenumber' => 1,
'invoiceamount' => 100
),
array(
'customernumber' => '213',
'invoicenumber' => 2,
'invoiceamount' => 95
),
array(
'customernumber' => '107',
'invoicenumber' => 3,
'invoiceamount' => 107
)
);
Custom Post Type: Customer File
Title: 101
Name: John Doe
Address: 123 Main Street
Phone: 901-123-4567
Title: 107
Name: Jane Smith
Address: 787 Oak Street
Phone: 212-356-4512
Title: 213
Name: Mary Jones
Address: 6642 Hawthorne Road
Phone: 749-878-3674
I need to display:
customer number: 101
invoice number: 1
invoice amount: $100
Name: John Doe
Address: 123 Main Street
Phone: 901-123-4567
customer number: 213
invoice number: 2
invoice amount: $95
Name: Mary Jones
Address: 6642 Hawthorne Road
Phone: 749-878-367
Thanks, James
I have a CPT name Customer File and create three text fields (number, address, phone) as the code below
add_filter( 'rwmb_meta_boxes', 'your_prefix_register_meta_boxes' );
function your_prefix_register_meta_boxes( $meta_boxes ) {
$prefix = '';
$meta_boxes[] = array (
'title' => esc_html__( 'Customer info', 'text-domain' ),
'id' => 'customer-info',
'post_types' => array(
0 => 'customer-file',
),
'context' => 'normal',
'priority' => 'high',
'fields' => array(
array (
'id' => $prefix . 'name',
'type' => 'text',
'name' => esc_html__( 'Name', 'text-domain' ),
),
array (
'id' => $prefix . 'address',
'type' => 'text',
'name' => esc_html__( 'Address', 'text-domain' ),
),
array (
'id' => $prefix . 'phone',
'type' => 'text',
'name' => esc_html__( 'Phone', 'text-domain' ),
),
),
);
return $meta_boxes;
}
then use this code to show the list invoices and the customers name
$invoices = array(
array(
'customernumber' => '101',
'invoicenumber' => 1,
'invoiceamount' => 100
),
array(
'customernumber' => '213',
'invoicenumber' => 2,
'invoiceamount' => 95
),
array(
'customernumber' => '107',
'invoicenumber' => 3,
'invoiceamount' => 107
)
);
$args = array(
'post_type' => 'customer-file',
'post_status' => 'publish',
'posts_per_page' => -1,
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
foreach ( $invoices as $number => $invoice ) {
if( $invoice['customernumber'] == $loop->post->post_title ) {
echo 'Customer Number: ' . $invoice['customernumber'] . '<br>';
echo 'Invoice Number: ' . $invoice['invoicenumber'] . '<br>';
echo 'Invoice Amount: ' . $invoice['invoiceamount'] . '<br>';
echo 'Name: ' . rwmb_meta( 'name' ) . '<br>';
echo 'Address: ' . rwmb_meta( 'address' ) . '<br>';
echo 'Phone: ' . rwmb_meta( 'phone' ) . '<br>';
}
}
endwhile;
wp_reset_postdata();
Please see my screen record to make sense https://cl.ly/3fd58b9a803b