• Resolved Marco Panichi

    (@marcopanichi)


    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)
  • Plugin Support Saiful Islam

    (@saifislam01)

    Hi @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

    (@marcopanichi)

    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>";
    	}
    }
    Plugin Support Saiful Islam

    (@saifislam01)

    Hi @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.