admin
1 month ago
ajax
3 months ago
api
2 weeks ago
fields
4 weeks ago
forms
4 weeks ago
legacy
5 months ago
locations
1 month ago
post-types
5 months ago
rest-api
4 weeks ago
walkers
5 months ago
acf-bidirectional-functions.php
5 months ago
acf-field-functions.php
5 months ago
acf-field-group-functions.php
5 months ago
acf-form-functions.php
5 months ago
acf-helper-functions.php
5 months ago
acf-hook-functions.php
5 months ago
acf-input-functions.php
5 months ago
acf-internal-post-type-functions.php
5 months ago
acf-meta-functions.php
2 months ago
acf-post-functions.php
5 months ago
acf-post-type-functions.php
5 months ago
acf-taxonomy-functions.php
5 months ago
acf-user-functions.php
5 months ago
acf-utility-functions.php
5 months ago
acf-value-functions.php
5 months ago
acf-wp-functions.php
5 months ago
assets.php
5 months ago
class-acf-data.php
5 months ago
class-acf-internal-post-type.php
5 months ago
compatibility.php
5 months ago
deprecated.php
5 months ago
fields.php
5 months ago
index.php
2 years ago
l10n.php
5 months ago
local-fields.php
5 months ago
local-json.php
3 months ago
local-meta.php
5 months ago
locations.php
5 months ago
loop.php
5 months ago
media.php
5 months ago
rest-api.php
5 months ago
revisions.php
3 months ago
third-party.php
5 months ago
upgrades.php
2 months ago
validation.php
5 months ago
wpml.php
5 months ago
acf-post-type-functions.php
305 lines
| 1 | <?php |
| 2 | /** |
| 3 | * @package ACF |
| 4 | * @author WP Engine |
| 5 | * |
| 6 | * © 2026 Advanced Custom Fields (ACF®). All rights reserved. |
| 7 | * "ACF" is a trademark of WP Engine. |
| 8 | * Licensed under the GNU General Public License v2 or later. |
| 9 | * https://www.gnu.org/licenses/gpl-2.0.html |
| 10 | */ |
| 11 | |
| 12 | /** |
| 13 | * Get an ACF CPT as an array |
| 14 | * |
| 15 | * @param integer|string $id The post ID being queried. |
| 16 | * @return array|false The post type object. |
| 17 | */ |
| 18 | function acf_get_post_type( $id ) { |
| 19 | return acf_get_internal_post_type( $id, 'acf-post-type' ); |
| 20 | } |
| 21 | |
| 22 | /** |
| 23 | * Retrieves a raw ACF CPT. |
| 24 | * |
| 25 | * @since 6.1 |
| 26 | * |
| 27 | * @param integer|string $id The post ID. |
| 28 | * @return array|false The internal post type array. |
| 29 | */ |
| 30 | function acf_get_raw_post_type( $id ) { |
| 31 | return acf_get_raw_internal_post_type( $id, 'acf-post-type' ); |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * Gets a post object for an ACF CPT. |
| 36 | * |
| 37 | * @since 6.1 |
| 38 | * |
| 39 | * @param integer|string $id The post ID, key, or name. |
| 40 | * @return object|boolean The post object, or false on failure. |
| 41 | */ |
| 42 | function acf_get_post_type_post( $id ) { |
| 43 | return acf_get_internal_post_type_post( $id, 'acf-post-type' ); |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Returns true if the given identifier is an ACF CPT key. |
| 48 | * |
| 49 | * @since 6.1 |
| 50 | * |
| 51 | * @param string $id The identifier. |
| 52 | * @return boolean |
| 53 | */ |
| 54 | function acf_is_post_type_key( $id ) { |
| 55 | return acf_is_internal_post_type_key( $id, 'acf-post-type' ); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Validates an ACF CPT. |
| 60 | * |
| 61 | * @since 6.1 |
| 62 | * |
| 63 | * @param array $post_type The ACF post type array. |
| 64 | * @return array|boolean |
| 65 | */ |
| 66 | function acf_validate_post_type( array $post_type = array() ) { |
| 67 | return acf_validate_internal_post_type( $post_type, 'acf-post-type' ); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Translates the settings for an ACF internal post type. |
| 72 | * |
| 73 | * @since 6.1 |
| 74 | * |
| 75 | * @param array $post_type The ACF post type array. |
| 76 | * @return array |
| 77 | */ |
| 78 | function acf_translate_post_type( array $post_type ) { |
| 79 | return acf_translate_internal_post_type( $post_type, 'acf-post-type' ); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Returns and array of ACF post types for the given $filter. |
| 84 | * |
| 85 | * @since 6.1 |
| 86 | * |
| 87 | * @param array $filter An array of args to filter results by. |
| 88 | * @return array |
| 89 | */ |
| 90 | function acf_get_acf_post_types( array $filter = array() ) { |
| 91 | return acf_get_internal_post_type_posts( 'acf-post-type', $filter ); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Returns an array of raw ACF post types. |
| 96 | * |
| 97 | * @since 6.1 |
| 98 | * |
| 99 | * @return array |
| 100 | */ |
| 101 | function acf_get_raw_post_types() { |
| 102 | return acf_get_raw_internal_post_type_posts( 'acf-post-type' ); |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Returns a filtered array of ACF post types based on the given $args. |
| 107 | * |
| 108 | * @since 6.1 |
| 109 | * |
| 110 | * @param array $post_types An array of ACF posts. |
| 111 | * @param array $args An array of args to filter by. |
| 112 | * @return array |
| 113 | */ |
| 114 | function acf_filter_post_types( array $post_types, array $args = array() ) { |
| 115 | return acf_filter_internal_post_type_posts( $post_types, $args, 'acf-post-type' ); |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Updates an ACF post type in the database. |
| 120 | * |
| 121 | * @since 6.1 |
| 122 | * |
| 123 | * @param array $post_type The main ACF post type array. |
| 124 | * @return array |
| 125 | */ |
| 126 | function acf_update_post_type( array $post_type ) { |
| 127 | return acf_update_internal_post_type( $post_type, 'acf-post-type' ); |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Deletes all caches for the provided ACF post type. |
| 132 | * |
| 133 | * @since 6.1 |
| 134 | * |
| 135 | * @param array $post_type The ACF post type array. |
| 136 | * @return void |
| 137 | */ |
| 138 | function acf_flush_post_type_cache( array $post_type ) { |
| 139 | acf_flush_internal_post_type_cache( $post_type, 'acf-post-type' ); |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Deletes an ACF post type from the database. |
| 144 | * |
| 145 | * @since 6.1 |
| 146 | * |
| 147 | * @param integer|string $id The ACF post type ID, key or name. |
| 148 | * @return boolean True if post type was deleted. |
| 149 | */ |
| 150 | function acf_delete_post_type( $id = 0 ) { |
| 151 | return acf_delete_internal_post_type( $id, 'acf-post-type' ); |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Trashes an ACF post type. |
| 156 | * |
| 157 | * @since 6.1 |
| 158 | * |
| 159 | * @param integer|string $id The post type ID, key, or name. |
| 160 | * @return boolean True if post was trashed. |
| 161 | */ |
| 162 | function acf_trash_post_type( $id = 0 ) { |
| 163 | return acf_trash_internal_post_type( $id, 'acf-post-type' ); |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Restores an ACF post type from the trash. |
| 168 | * |
| 169 | * @since 6.1 |
| 170 | * |
| 171 | * @param integer|string $id The post type ID, key, or name. |
| 172 | * @return boolean True if post was untrashed. |
| 173 | */ |
| 174 | function acf_untrash_post_type( $id = 0 ) { |
| 175 | return acf_untrash_internal_post_type( $id, 'acf-post-type' ); |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * Returns true if the given params match an ACF post type. |
| 180 | * |
| 181 | * @since 6.1 |
| 182 | * |
| 183 | * @param array $post_type The ACF post type array. |
| 184 | * @return boolean |
| 185 | */ |
| 186 | function acf_is_post_type( $post_type ) { |
| 187 | return acf_is_internal_post_type( $post_type, 'acf-post-type' ); |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * Duplicates an ACF post type. |
| 192 | * |
| 193 | * @since 6.1 |
| 194 | * |
| 195 | * @param integer|string $id The ACF post type ID, key or name. |
| 196 | * @param integer $new_post_id Optional ID to override. |
| 197 | * @return array|boolean The new ACF post type, or false on failure. |
| 198 | */ |
| 199 | function acf_duplicate_post_type( $id = 0, $new_post_id = 0 ) { |
| 200 | return acf_duplicate_internal_post_type( $id, $new_post_id, 'acf-post-type' ); |
| 201 | } |
| 202 | |
| 203 | /** |
| 204 | * Activates or deactivates an ACF post type. |
| 205 | * |
| 206 | * @param integer|string $id The ACF post type ID, key or name. |
| 207 | * @param boolean $activate True if the post type should be activated. |
| 208 | * @return boolean |
| 209 | */ |
| 210 | function acf_update_post_type_active_status( $id, $activate = true ) { |
| 211 | return acf_update_internal_post_type_active_status( $id, $activate, 'acf-post-type' ); |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * Checks if the current user can edit the post type and returns the edit url. |
| 216 | * |
| 217 | * @since 6.1 |
| 218 | * |
| 219 | * @param integer $post_id The ACF post type ID. |
| 220 | * @return string |
| 221 | */ |
| 222 | function acf_get_post_type_edit_link( $post_id ) { |
| 223 | return acf_get_internal_post_type_edit_link( $post_id, 'acf-post-type' ); |
| 224 | } |
| 225 | |
| 226 | /** |
| 227 | * Returns a modified ACF post type ready for export. |
| 228 | * |
| 229 | * @since 6.1 |
| 230 | * |
| 231 | * @param array $post_type The ACF post type array. |
| 232 | * @return array |
| 233 | */ |
| 234 | function acf_prepare_post_type_for_export( array $post_type = array() ) { |
| 235 | return acf_prepare_internal_post_type_for_export( $post_type, 'acf-post-type' ); |
| 236 | } |
| 237 | |
| 238 | /** |
| 239 | * Exports an ACF post type as PHP. |
| 240 | * |
| 241 | * @since 6.1 |
| 242 | * |
| 243 | * @param array $post_type The ACF post type array. |
| 244 | * @return string|boolean |
| 245 | */ |
| 246 | function acf_export_post_type_as_php( array $post_type ) { |
| 247 | return acf_export_internal_post_type_as_php( $post_type, 'acf-post-type' ); |
| 248 | } |
| 249 | |
| 250 | /** |
| 251 | * Prepares an ACF post type for the import process. |
| 252 | * |
| 253 | * @since 6.1 |
| 254 | * |
| 255 | * @param array $post_type The ACF post type array. |
| 256 | * @return array |
| 257 | */ |
| 258 | function acf_prepare_post_type_for_import( array $post_type = array() ) { |
| 259 | return acf_prepare_internal_post_type_for_import( $post_type, 'acf-post-type' ); |
| 260 | } |
| 261 | |
| 262 | /** |
| 263 | * Imports an ACF post type into the database. |
| 264 | * |
| 265 | * @since 6.1 |
| 266 | * |
| 267 | * @param array $post_type The ACF post type array. |
| 268 | * @return array The imported post type. |
| 269 | */ |
| 270 | function acf_import_post_type( array $post_type ) { |
| 271 | return acf_import_internal_post_type( $post_type, 'acf-post-type' ); |
| 272 | } |
| 273 | |
| 274 | /** |
| 275 | * Exports the "Enter Title Here" text for the provided ACF post types. |
| 276 | * |
| 277 | * @since 6.2.1 |
| 278 | * |
| 279 | * @param array $post_types The post types being exported. |
| 280 | * @return string |
| 281 | */ |
| 282 | function acf_export_enter_title_here( array $post_types ) { |
| 283 | $to_export = array(); |
| 284 | $export = ''; |
| 285 | |
| 286 | foreach ( $post_types as $post_type ) { |
| 287 | if ( ! empty( $post_type['enter_title_here'] ) ) { |
| 288 | $to_export[ $post_type['post_type'] ] = $post_type['enter_title_here']; |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | if ( ! empty( $to_export ) ) { |
| 293 | $export .= "\r\nadd_filter( 'enter_title_here', function( \$default, \$post ) {\r\n"; |
| 294 | $export .= "\tswitch ( \$post->post_type ) {\r\n"; |
| 295 | |
| 296 | foreach ( $to_export as $post_type => $enter_title_here ) { |
| 297 | $export .= "\t\tcase '$post_type':\r\n\t\t\treturn '$enter_title_here';\r\n"; |
| 298 | } |
| 299 | |
| 300 | $export .= "\t}\r\n\r\n\treturn \$default;\r\n}, 10, 2 );\r\n\r\n"; |
| 301 | } |
| 302 | |
| 303 | return esc_textarea( $export ); |
| 304 | } |
| 305 |