Home Forums General Issues How to populate a select field with titles from another post type Reply To: How to populate a select field with titles from another post type

  • To populate a select field on a custom post type with the titles from another post type, you can use a custom function and a WordPress query. Here’s an example of how you can achieve this:


    function populate_players_select_field( $field ) {
    // Query the ‘players’ post type to get the player names
    $players_query = new WP_Query( array(
    ‘post_type’ => ‘players’,
    ‘posts_per_page’ => -1, // Retrieve all players
    ) );

    // Populate the select field options
    $options = array();

    if ( $players_query->have_posts() ) {
    while ( $players_query->have_posts() ) {
    $options[get_the_ID()] = get_the_title();

    // Set the select field options
    $field[‘choices’] = $options;

    return $field;
    add_filter( ‘acf/load_field/name=select_field_name’, ‘populate_players_select_field’ );

    Replace ‘select_field_name’ in the add_filter line with the actual name of your select field.

    Make sure to place this code in your theme’s functions.php file or in a custom plugin file. This code uses the Advanced Custom Fields (ACF) plugin to modify the field, so make sure you have ACF installed and activated.

    What this code does is it hooks into the acf/load_field filter, specifically targeting the field with the specified name, and modifies its choices by querying the ‘players’ post type and retrieving the titles. The titles are then used as options for the select field.

    Remember to adjust the post type names (‘players’ and ‘fixtures’) to match your actual custom post types.

    Once you save this code and load the post editor, the select field on the ‘fixtures’ post type should be populated with the player names from the ‘players’ post type, allowing you to select the players for a match.