Home › Forums › Backend Issues (wp-admin) › Getting ACF Select[dropdown], Selected CPT's Title
I have a Custom Post Type that has ACF fields. So, for example, I can create a new Custom Post Type for Los Angeles and then fill out the different fields available for that post type. Then I can create a new Custom Post Type for New York and fill out the different fields.
When I create a page, I choose a select option(Los Angeles or New York) to output that post types values in the page. This is a required field.
Then I have a plugin to show the published status and the page template assigned of each page.
What I am wanting to do is also display the title of the Post Type assigned to each page. How can I get the title of each pages assigned Custom Post Type?
My Custom Post Type is named “City Templates”, here is the code:
// Add the City Templates Custom Post Type
function cptui_register_my_cpts_city_template() {
/* Post Type: City Templates.*/
$sitedomain = get_site_url();
$labels = array(
"name" => __( "City Templates", "Theme-Child" ),
"singular_name" => __( "City Template", "Theme-Child" ),
);
$args = array(
"label" => __( "City Templates", "Theme-Child" ),
"labels" => $labels,
"description" => "",
"public" => false,
"publicly_queryable" => false,
"show_ui" => true,
"delete_with_user" => false,
"show_in_rest" => false,
"rest_base" => "",
"rest_controller_class" => "WP_REST_Posts_Controller",
"has_archive" => false,
"show_in_menu" => true,
"show_in_nav_menus" => true,
"exclude_from_search" => true,
"capability_type" => "post",
"map_meta_cap" => true,
"hierarchical" => false,
"rewrite" => array( "slug" => "cities_template", "with_front" => true ),
"query_var" => true,
"menu_position" => 63,
"menu_icon" => "$sitedomain/wp-content/themes/Theme/images/image.png",
"supports" => array( "title", "editor", "thumbnail" ),
);
register_post_type( "cities_template", $args );
}
add_action( 'init', 'cptui_register_my_cpts_city_template' );
My ACF group is called “City Template Page Setting”, here is the code:
// Add the ACF choose city template select box
if( function_exists('acf_add_local_field_group') ):
acf_add_local_field_group(array(
'key' => 'group_5babd7e887844',
'title' => 'City Template Page Setting',
'fields' => array(
array(
'key' => 'field_59304f7e23d16',
'label' => 'Cities Template',
'name' => 'cities_template',
'type' => 'post_object',
'instructions' => '',
'required' => 1,
'conditional_logic' => 0,
'wrapper' => array(
'width' => '',
'class' => '',
'id' => '',
),
'post_type' => array(
0 => 'cities_template',
),
'taxonomy' => array(
),
'allow_null' => 0,
'multiple' => 0,
'return_format' => 'object',
'ui' => 1,
),
),
'location' => array(
array(
array(
'param' => 'page_template',
'operator' => '==',
'value' => 'page-city-template.php',
),
),
),
'menu_order' => 1,
'position' => 'side',
'style' => 'default',
'label_placement' => 'top',
'instruction_placement' => 'label',
'hide_on_screen' => '',
'active' => true,
'description' => '',
));
endif;
Here is what I have for my admin page output:
function display_ews_metas_ids(){
$page_ids=get_all_page_ids();
echo '<h3>Pages :</h3><div style="display:flex;flex-direction:row;flex-wrap:wrap;">';
foreach($page_ids as $page){
//Check if $current_lang_page_id is original page (not a tranlated page) and status publish
if (get_post_status($page) == 'auto-draft') {echo '';} else {
echo '<div class="desc-bulk-box" style="width:90%;padding:0 1%;margin:5px 0;"><a href="'. get_edit_post_link( $page ) .'" target="_blank"><strong style="color: #016392;">'.get_the_title($page).'</strong>';// retrieve the _metadescs current value
if ('draft' === get_post_status( $page )) {echo '<em style="color:#e29404"> (in draft status)</em>';}
if ('pending' === get_post_status( $page )) {echo '<em style="color:#bfa800"> (in pending status)</em>';}
if ('future' === get_post_status( $page )) {echo '<em style="color:#bfa800"> (in future status)</em>';}
if ('private' === get_post_status( $page )) {echo '<em style="color:#bfa800"> (in private status)</em>';}
if ('trash' === get_post_status( $page )) {echo '<em style="color:#c12222"> (in trash)</em>';}
echo '</a>';
}
$template = get_post_meta( $page, '_wp_page_template', true );
if ($template == 'page-city-template.php') {
echo ' -- <b><i>CITY BASED PAGE TEMPLATE</i></b> --';
$field = get_field_object('group_5babd7e887844');
$value = get_field('cities_template');
$label = $field['choices'][ $value ];?>
<p>Assigned Template: <?php echo $label; ?></p>
<?php echo $template; ?>
</div>
<?php }
}
}
I have piecemealed together many things for the output so if that is way off base, I am not surprised.
The topic ‘Getting ACF Select[dropdown], Selected CPT's Title’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.