Hello, if you want to get the value of a Types custom date field from each post in the query results, I suggest using the get_post_meta function in the foreach loop. Then use a conditional to test if there is a value, and if so, append it to the choices array. Something like this:
This assumes the slug of your custom date field is "tour-date-field". If not, replace that part of the code with the appropriate date field slug, including the wpcf- prefix.
This is so far working. Now a next step is, to get only specific dates out. As I have products with a reference post field to tour dates, I need only those dates, where I have the tour dates in it. My data structure is followed:
funtion get_my_tour_date_id () {
global $post;
$parent_post = $post->ID;
$field_slug = 'tourid';
$results = toolset_get_related_posts(
// get posts related to this one
$parent_post,
// Relationship between the posts
$field_slug,
// Additional arguments
[
// This is mandatory because we're passing just a single post as the first parameter.
'query_by_role' => 'child',
// pagination
'limit' => 9999,
'offset' => 0,
'args' => '',
'role_to_return' => 'other',
'return' => 'post_id'
]
);
return $results;
}
To get only those posts of tour dates out. What is the best way to merge with this function:
function populate_dates( $form ) {
global $post;
foreach ( $form['fields'] as &$field ) {
if ( $field->type != 'select' || strpos( $field->cssClass, 'populate-dates' ) === false ) {
continue;
}
$args = array(
'numberposts' => -1,
'post_type' => 'tour-date',
'post_status' => 'publish',
'suppress_filters' => false
);
$posts = get_posts( $args );
$choices = array();
foreach ( $posts as $post ) {
$tour_date_field = get_post_meta( $post->ID, 'wpcf-tour-date-field', true);
if( $tour_date_field )
{
$choices[] = array( 'text' => date( 'd. M Y', $tour_date_field), 'value' => $post->ID );
}
}
// update 'Select a Post' to whatever you'd like the instructive option to be
$field->placeholder = 'Select a date';
$field->choices = $choices;
}
return $form;
}
It looks like you need to replace your get_posts query with the toolset_get_related_posts query instead, and adjust the foreach loop to read those posts from $results:
function populate_dates( $form ) {
global $post;
$parent_post = $post->ID;
$field_slug = 'tourid';
foreach ( $form['fields'] as &$field ) {
if ( $field->type != 'select' || strpos( $field->cssClass, 'populate-dates' ) === false ) {
continue;
}
$choices = array();
$results = toolset_get_related_posts(
// get posts related to this one
$parent_post,
// Relationship between the posts
$field_slug,
// Additional arguments
[
// This is mandatory because we're passing just a single post as the first parameter.
'query_by_role' => 'child',
// pagination
'limit' => 9999,
'offset' => 0,
'args' => '',
'role_to_return' => 'other',
'return' => 'post_id'
]
);
foreach ( $results as $result ) {
$tour_date_field = get_post_meta( $result->ID, 'wpcf-tour-date-field', true);
if( $tour_date_field )
{
$choices[] = array( 'text' => date( 'd. M Y', $tour_date_field), 'value' => $result->ID );
}
}
// update 'Select a Post' to whatever you'd like the instructive option to be
$field->placeholder = 'Select a date';
$field->choices = $choices;
}
return $form;
}
function get_my_tour_date_id () {
global $post;
$parent_post = $post->ID;
$field_slug = 'tourid';
$results = toolset_get_related_posts(
// get posts related to this one
$parent_post,
// Relationship between the posts
$field_slug,
// Additional arguments
[
// This is mandatory because we're passing just a single post as the first parameter.
'query_by_role' => 'child',
// pagination
'limit' => 9999,
'offset' => 0,
'args' => '',
'role_to_return' => 'other',
'return' => 'post_id'
]
);
return $results;
}
add_shortcode('tour_date_id', 'get_my_tour_date_id');
I expect that I should get something out - instead I see "Array" (lien caché) just before the green banner in the bottom.
And in the error log I get this:
[09-Jun-2021 05:02:54 UTC] PHP Warning: array_merge(): Expected parameter 1 to be an array, string given in /mnt/disk1/home/chillicyclingksb/public_html/wp-content/plugins/wp-views/vendor/toolset/toolset-common/inc/autoloaded/interop/commands/RelatedPosts.php on line 316
[09-Jun-2021 05:02:54 UTC] PHP Warning: array_key_exists() expects parameter 2 to be array, null given in /mnt/disk1/home/chillicyclingksb/public_html/wp-content/plugins/wp-views/vendor/toolset/toolset-common/inc/autoloaded/interop/commands/RelatedPosts.php on line 470
I think I found out why it didn't worked. I changed the code to:
function populate_dates( $form ) {
global $post;
$origin_id = $post->ID;
$relationship_slug = 'tourid';
foreach ( $form['fields'] as &$field ) {
if ( $field->type != 'select' || strpos( $field->cssClass, 'populatetourdates' ) === false ) {
continue;
}
$choices = array();
$results = toolset_get_related_posts (
// get posts related to this one
$origin_id, // get posts connected to this one
$relationship_slug, // in this relationship
array(
'query_by_role' => 'parent', // origin post role
'role_to_return' => 'child', // role of posts to return
'return' => 'post_id', // return array of IDs (post_id) or post objects (post_object)
'limit' => 999, // max number of results
'offset' => 0, // starting from
'args' => null // for adding meta queries etc.
)
);
foreach ( $results as $result ) {
$tour_date_field = get_post_meta( $result, 'wpcf-tour-date-field', true);
if( $tour_date_field )
{
$choices[] = array( 'text' => date( 'd. M Y', $tour_date_field), 'value' => $result );
}
}
// update 'Select a Post' to whatever you'd like the instructive option to be
$field->placeholder = 'Select a date';
$field->choices = $choices;
}
return $form;
}
Or what are Your thoughts?
Yes, I think you are correct. The toolset_get_related_posts API returns a post ID, not a complete post, by default, so you don't need to use the ->ID to get the post ID. I think you are right about that.