Home › Forums › ACF PRO › Created Fields Programmatically › Reply To: Created Fields Programmatically
Hi All,
The forum wasn’t displaying my post for a while and this pushed me to keep trying, and I managed to sort the issue. It was actually just a simple ->ID on constructing the rewrites that was missing, and then I added a higher priority on the init, and all seems to be working nicely.
The code is below. If anyone can see any issues or ways I could make the code more efficient please let me know.
/***
*
* Creates Option Page and ACF Fields for CPT Archive Pages
*
***/
function add_archive_option_fields() {
$post_types = get_post_types( array ( 'public' => true, '_builtin' => FALSE ), 'objects' );
if ( $post_types ) {
// Creates Archive Pages Option Page
if ( function_exists( 'acf_add_options_page' ) ) {
acf_add_options_page( array (
'page_title' => 'Archive Pages',
'menu_title' => 'Archive Pages',
'menu_slug' => 'archive-pages',
'icon_url' => 'dashicons-admin-page',
'position' => 20
));
}
// Adds main Archive Page Custom Fields for all CPTs that are set as has_archive => true
if( function_exists('acf_add_local_field_group') ) {
acf_add_local_field_group( array (
'key' => 'group_archive_pages',
'title' => 'Archive Pages',
'fields' => array (),
'location' => array (
array (
array(
'param' => 'options_page',
'operator' => '==',
'value' => 'archive-pages',
),
),
),
));
foreach ( $post_types as $post_type => $properties ) {
$post_type_object = get_post_type_object( $post_type );
$post_type_label = $post_type_object->labels->name;
$post_type_singular_label = $post_type_object->labels->singular_name;
if ( $properties->has_archive ) {
acf_add_local_field(
array(
'key' => ''.$post_type.'_archive_page',
'label' => ''.$post_type_singular_label.' Archive Page',
'name' => ''.$post_type.'_archive_page',
'type' => 'post_object',
'instructions' => 'Select your archive page for the '.$post_type_label.'',
'required' => 0,
'post_type' => array( 0 => 'page' ),
'allow_null' => 1,
'multiple' => 0,
'return_format' => 'object',
'parent' => 'group_archive_pages'
)
);
acf_add_local_field(
array(
'key' => ''.$post_type.'_per_page',
'label' => ''.$post_type_singular_label.' Per Page',
'name' => ''.$post_type.'_per_page',
'type' => 'range',
'instructions' => 'Select the amount of posts that are shown before pagination for the '.$post_type_label.'',
'required' => 0,
'default_value' => '',
'min' => 1,
'max' => 99,
'step' => 1,
'append' => 'posts per page',
'parent' => 'group_archive_pages'
)
);
}
}
}
}
}
add_action( 'init', 'add_archive_option_fields', 99 );
/***
*
* Rewrite post type slug via ACF Options page
*
***/
function change_post_types_slug( $args, $post_type ) {
$site_url = get_site_url();
$full_site_url = $site_url . '/';
if ( $post_type === $post_type ) {
// Checks to see if CPT has_archive
$post_type_object = get_post_type_object( $post_type );
$has_archive = $post_type_object->has_archive;
if ( $has_archive ) {
$post_type_archive_page = get_field( ''.$post_type.'_archive_page', 'option' );
if ( $post_type_archive_page ) {
$post_type_archive_page_link = get_the_permalink( $post_type_archive_page );
$post_type_archive_page_url = str_replace( $full_site_url, '', $post_type_archive_page_link);
$post_type_archive_page_url = rtrim( $post_type_archive_page_url,'/' );
$args['rewrite']['slug'] = $post_type_archive_page_url;
}
}
}
return $args;
}
add_filter( 'register_post_type_args', 'change_post_types_slug', 10, 2 );
/***
*
* Posts Per Page via ACF Options page
*
***/
function my_post_queries( $query ) {
if ( !is_admin() && $query->is_main_query() ){
$post_types = get_post_types( array ( 'public' => true, '_builtin' => FALSE ) );
foreach ( $post_types as $post_type ) {
if ( is_post_type_archive( $post_type )){
$post_type_per_page = get_field( ''.$post_type.'_per_page', 'option' );
$query->set( 'posts_per_page', $post_type_per_page );
}
}
}
}
add_action( 'pre_get_posts', 'my_post_queries' );
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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.