Loop cpt titles in select bricks form field

Hi. Is it possible to loop through and display CPT titles as options in a select bricks form field?
Thank you.

Hey Solarian,

since Bricks 1.9 this is possible. Just create a small helper function to fetch the post titles (separated by a line break) and then call this function in the form:

function get_cpt_titles() {
    $posts = get_posts( [
        'post_type' => 'YOUR_CPT_SLUG_HERE',
        'posts_per_page' => -1
    ] );

    $post_titles = array_map( function( $post ) {
        return $post->post_title;
    }, $posts );

    return implode( "\n", $post_titles );
}

Let me know if that helps.

Best,

André

3 Likes

thank you, André, it’s working. More that that, this option is also possbile:
{echo:get_cpt_titles}
other option that is not a cpt title
another static option

Hi @aslotta

ive been trying a few variations of this for taxonomy of a cpt. Any chance you can help with this?

function get_item_category() {
    $term_query = get_terms( [
        'post_type' => 'buysell',
        'posts_per_page' => -1
    ] );

    $term_titles = array_map( function( $term_query ) {
        return $term->term_title;
    }, $terms );

    return implode( "\n", $term_titles );

}

Hey @MattBlack,

what exactly are you trying to query?

Best,

André

@aslotta

the form is to create a CPT.
i want to display the list of categories in the form

Hey @MattBlack,

and buysell is the taxonomy? If so try this:

function get_taxonomy_terms() {
    $term_names = get_terms( [
        'taxonomy'   => 'buysell',
        'hide_empty' => false,
        'fields' => 'names'
    ] );

    return implode( "\n", $term_names );
}

Best,

André

1 Like

worked perfectly. thank you and appreciate your help @aslotta

Hi, I have CPT field, with City, In every post I have different city. Please how I can show the cities from all posts in form? Thank you.