Support

Account

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

Solved

How to populate a select field with titles from another post type

  • Hi,

    I’m trying to populate a select field on a custom post type with the titles from another post type. I know there is a relationship field for this but for Elementor reasons I need to have it in a select field so I can use it in the editor.

    I have a post type called ‘players’ and each item in players is a player, and the title is the player’s name.

    In another post type called fixtures, I would like a select field that lists those players’ names to pick from. I will have multiple fields like this on this post type so the user can pick the players for the match.

    I’ve tried this, but it doesn’t seem to work, so I would love it if someone could help?

    function populate_players_select_fields_choices($field) {

    // Query the 'players' custom post type to retrieve post titles
    $players = get_posts(array(
    'post_type' => 'players',
    'numberposts' => -1, // Retrieve all posts
    ));

    // Initialize an empty choices array
    $choices = array();

    // Loop through the players and add their titles to the choices array
    foreach ($players as $player) {
    $choices[$player->ID] = $player->post_title;
    }

    // Set the choices for the select field
    $field['player'] = $choices;

    return $field;
    }
    add_filter('acf/load_field', 'populate_players_select_fields_choices');

  • 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:

    php

    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() ) {
    $players_query->the_post();
    $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.

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

You must be logged in to reply to this topic.