Title: Populating select with posts objects
Last modified: October 21, 2023

---

# Populating select with posts objects

 *  Resolved [Marco Panichi](https://wordpress.org/support/users/marcopanichi/)
 * (@marcopanichi)
 * [2 years, 7 months ago](https://wordpress.org/support/topic/populating-select-with-posts-objects/)
 *     ```
       I have a custom post type "Auto"
   
       Then I have a custom post type "Driver"
   
       When I create or edit a "Driver" post, I would like to be able to select the Car.
   
       In other words, I need a select field populated with posts of type Car.
   
       It's possible?
       ```
   

Viewing 3 replies - 1 through 3 (of 3 total)

 *  [Saiful Islam](https://wordpress.org/support/users/saifislam01/)
 * (@saifislam01)
 * [2 years, 7 months ago](https://wordpress.org/support/topic/populating-select-with-posts-objects/#post-17141470)
 * Hi [@marcopanichi](https://wordpress.org/support/users/marcopanichi/),
 * To create a relationship between your “Driver” and “Auto” custom post types, 
   allowing you to select a car when creating or editing a driver post, you would
   typically need to do some custom coding.
   Hope this information will help.
 * Regards!
 *  Thread Starter [Marco Panichi](https://wordpress.org/support/users/marcopanichi/)
 * (@marcopanichi)
 * [2 years, 7 months ago](https://wordpress.org/support/topic/populating-select-with-posts-objects/#post-17142882)
 * I solved, client side, using jQuery.
 * This is the structure of the code I used, I hope it helps someone:
 *     ```
       add_action('wp_footer', 'my_custom_select');
       function my_custom_select()
       {
       	$posts = new WP_Query(array(
       		...
       	));
   
       	$options = '';
       	if( $posts->have_posts() )
       	{
       		while( $posts->have_posts() )
       		{
       			$posts->the_post();
       			$options .= "field.append($('<option>').attr('value', '".get_the_ID()."').text(\"".get_the_title()."\"))\r\n";
       		}
       	}
       	wp_reset_postdata();
   
       	echo "<script type='text/javascript'>
       		jQuery(document).ready(function($) {
       			var field = $('#my_custom_select');
       			field.empty();
       			field.append($('<option>').attr('value', '-1').text(\"- select -\"))
       			$options
       		});
       	</script>";
       	}
       }
       ```
   
 *  [Saiful Islam](https://wordpress.org/support/users/saifislam01/)
 * (@saifislam01)
 * [2 years, 7 months ago](https://wordpress.org/support/topic/populating-select-with-posts-objects/#post-17142948)
 * Hi [@marcopanichi](https://wordpress.org/support/users/marcopanichi/),
 * That’s great to hear that you were able to solve the issue on the client side
   using jQuery! Sharing the code structure can be incredibly helpful for others
   who might encounter a similar problem. Thank you for being considerate and sharing
   your solution – it’s a fantastic way to contribute to the community and help 
   others learn and grow.

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Populating select with posts objects’ is closed to new replies.

 * ![](https://ps.w.org/wp-user-frontend/assets/icon-256x256.gif?rev=2818776)
 * [User Frontend: AI Powered Frontend Posting, User Directory, Profile, Membership & User Registration](https://wordpress.org/plugins/wp-user-frontend/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/wp-user-frontend/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/wp-user-frontend/)
 * [Active Topics](https://wordpress.org/support/plugin/wp-user-frontend/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/wp-user-frontend/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/wp-user-frontend/reviews/)

## Tags

 * [custom fields](https://wordpress.org/support/topic-tag/custom-fields/)
 * [custom post type](https://wordpress.org/support/topic-tag/custom-post-type/)

 * 3 replies
 * 2 participants
 * Last reply from: [Saiful Islam](https://wordpress.org/support/users/saifislam01/)
 * Last activity: [2 years, 7 months ago](https://wordpress.org/support/topic/populating-select-with-posts-objects/#post-17142948)
 * Status: resolved