Both Types,
I have a country dropdown that needs to be populated by a “country” post_type and a “specialisation” dropdown that needs to be populated from a meta_value.
I’m currently using jQuery but would be happier if there was a built in filter, function or hook.
This is my current jquery and php for the country dropdown.
<?php
// query for your post type
$post_type_query = new WP_Query(
array (
'post_type' => 'country',
'posts_per_page' => -1 ,
'orderby' => 'title',
'order' => 'ASC',
)
);
// we need the array of posts
$posts_array = $post_type_query->posts;
// create a list with needed information
// the key equals the ID, the value is the post_title
$post_title_array = wp_list_pluck( $posts_array, 'post_title', 'post_name' );
//echo '<pre>' . var_export(json_encode($post_title_array), true) . '</pre>';
?>
<script>
(function($) {
var selectValues = <?php echo json_encode($post_title_array); ?>;
var selectMenu = $('#country-select');
$.each(selectValues, function(key, value) {
selectMenu
.append($('<option>', { value : key })
.text(value));
});
})(jQuery);
</script>