Hi
This question is related to the support ticket I submit a few days ago.
https://toolset.com/forums/topic/how-to-automatic-get-data-from-other-cpt-for-custom-checkbox-field/
add_filter( 'wpt_field_options', 'tssupp_mod_options', 10, 2 );
function tssupp_mod_options( $current_options, $title ){
if ( $title == "Skus" ) { // Edit for the title of your select custom field
// Get published products
$products = get_posts(
array(
'post_type' => 'product',
'post_status' => 'publish',
'meta_key' => '_sku',
'nopaging' => true
)
);
// Get SKU of each product
$skus = array();
foreach ($products as $product) {
$skus[] = get_post_meta( $product->ID, '_sku', true );
}
// Customise current options
$current_options = array();
foreach ($skus as $sku) {
$current_options[] = array(
'#value' => $sku,
'#title' => $sku
);
}
}
return $current_options;
}
The code above successful get the product SKU, but now I also need to get the product name.
How can I do it?
Hello, are you saying you want to use the Product title as the text instead of the SKU? Or are you saying there is another separate select field for choosing a Product? In either case, you can access the product title from the $product variable after calling get_posts, like this:
$products = get_posts(
array(
'post_type' => 'product',
'post_status' => 'publish',
'nopaging' => true
)
);
// Get title of each product
$product_titles = array();
foreach ($products as $product) {
$product_titles[] = $product->post_title;
}
Now you have an array of product titles in $product_titles.