Sauter la navigation

[Résolu] How to get dates into a dropdown with a given parent ID (product id)

This support ticket is created Il y a 3 années et 6 mois. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

Ce sujet contient 9 réponses, a 3 voix.

Dernière mise à jour par fred-r.M Il y a 3 années et 6 mois.

Assisté par: Christian Cox.

Auteur
Publications
#2079123

Tell us what you are trying to do? In Gravity Forms I can pre populate a dropdown field like this:

/* prepopulate date dropdown */
add_filter( 'gform_pre_render_17', 'populate_dates' );
add_filter( 'gform_pre_validation_17', 'populate_dates' );
add_filter( 'gform_pre_submission_filter_17', 'populate_dates' );
add_filter( 'gform_admin_pre_render_17', 'populate_dates' );
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 ) {
            $choices[] = array( 'text' => date( 'd. M Y', $post->wpcf-tour-date-field), 'value' => $post->ID );
        }
 
        // update 'Select a Post' to whatever you'd like the instructive option to be
        $field->placeholder = 'Select a Post';
        $field->choices = $choices;
 
    }
 
    return $form;
}

I have as "parent" the product (where I might need to get the ID, and this is referenced to tourid in the post-type tour-date.

In the dropdown field I like to see the tour-date-field - ideally everything from now until the future, the value should be the ID from tour-date.

I get the ID from tour-dates, but the dates aren't showing correct - respectively I get basically "zero".

Is there any documentation that you are following? I not found a documentation

Is there a similar example that we can see? In the lower part - lien caché the dropdown is the "Untiled field".

What is the link to your site? lien caché

#2079577

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:

...
  foreach ( $posts as $post ) {
    $tour_date = get_post_meta( $post->ID, 'wpcf-tour-date-field', true);
    if( $tour_date )
    {
      $choices[] = array( 'text' => date( 'd. M Y', $tour_date), 'value' => $post->ID );
    }
  }
...

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.

#2079957

Dear Christian Cox

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:

CPT: Tour Dates
tourid (Post Reference)
tour-date-field (date field)
tour-price (number field)
supplement (number field)

This is a function might working:

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;
}
#2080503

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;
}
#2080975

Dear Christian Cox

It seems not to working. lien caché

I don't get any dates now.

A question, would it help to have a relationship instead of only a post reference?

#2081089

Dear Christian Cox

I created a short code for a test:

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

It seems as I don't get anything.

#2081285

Dear Christian Cox

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;
}

Before:

$tour_date_field = get_post_meta( $result->ID, 'wpcf-tour-date-field', true);

Now:

$tour_date_field = get_post_meta( $result, 'wpcf-tour-date-field', true);

without ID

And for the part:

$choices[] = array( 'text' => date( 'd. M Y', $tour_date_field), 'value' => $result );

the value should be $result only, not $result->ID

Or what are Your thoughts?

#2082037

Shane
Supporter

Les langues: Anglais (English )

Fuseau horaire: America/Jamaica (GMT-05:00)

Hi Fred,

Christian is currently out sick today but should be back tomorrow to continue assisting.

You should get a response from him or another supporter tomorrow should he be out again tomorrow sick.

Thank you for your continued patience on this one.

Thanks,
Shane

#2083357

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.

#2084365

My issue is resolved now. Thank you!