Howdy, Stranger!
It looks like you're new here. If you want to get involved, click one of these buttons!
ACF for custom post type
-
So I'm trying to get ACF's working with custom post types, I'm fairly new to wordpress so stop me if I'm doing something stupid here.
I have a bunch of custom post types registered in my functions php and working fine in my custom page templates (I'm building my own themes), the problem is I can't get my ACF's working with the custom post types.
The only options in location under 'post type' is either post or page.
There must be a way to get them working for custom post types... -
Hi @andy999
Depending on your ACF version, you may need to make your custom post types have a public setting of true.
In the new beta, this public setting is not needed -
Yeah, thanks, I actually worked that out just after I posted but I thought I'd leave this up just in case it was a quirk of my own code and not the way ACF's work.
It would be nice not to need to have the setting of public for ACF's to work, will updating the plugin when you release the next version break any ACF's we have set up already or will we just be able to go in and change the public variable in the functions? -
Hi @andy999
No, nothing will break, you just won't have to include the public param in the future
Thanks
Elliot -
I just came to report the same issue. In the future, will ACF be looking at some other post type parameter instead (like show_ui)?
-
-
I'm realizing that this may be a different issue and I was confused, but, I see that 3.5.2 came out. I had expected that non-public post types would now be able to be referenced in the Relationship field but it still seems that this is not the case. I'd love to get that allowed.
-
Hi @mrwweb
I will have a think about how I can add this in without annoying other users just wanting all public post types.
Cheers
Elliot -
Totally understood. In hopes of being constructive, some UI and/or logic possibilities:
1. Check 'show_ui' in addition to 'public' for post types. If either is true, show the post type. This would resolve all of my issues (and it makes sense on a lot of levels that a post type with a UI, even if private, might want ACF), but I may be overlooking some reason why this wouldn't work.
2. Add a textbox for entering custom post type name. Presumably, people encountering this issue are mostly developers that wouldn't mind doing this.
3. Hide non-public post types by default but add a "Show all post types" toggle field. This could be a setting too, but that would really only be required if there was an important reason to persistently show the post types. In my mind, once you've selected it, you probably won't need it again.
4. Create a new register_post_type() argument like 'acf' or support 'acf' as a value in the 'supports' argument
I've even tried making a post type public, selecting it, and then making it non-public again. However, the field seems to lose it, presumably because of some type of sanitization on the field. However, I wonder if even allowing this "hack" (turn on public, select, turn off) would at least be a step in the right direction. Then again, that just feels wrong. -
I have the same problem with the 'public' argument. By default WordPress sets 'public' to false. Good that you are fixing this in the beta.
Where can I find the beta? -
-
Hi Elliot,
I have seen that you solved it for locations, only for other items the problem still exists.
For example when you add a Post Object field. The options there only show 'public' post types.
I would suggest adding a utility function to your acf class, which you can the use to retrieve the posttypes.
I have a sample code here which works. Take a look a this:
acf.php, add the following utility method:function get_post_types()
{
$exclude = array( 'acf' );
$include = array( 'page', 'post' );
$post_types = get_post_types( array(
'_builtin' => false,
));
foreach ( $include as $post_type ) {
if( post_type_exists( $post_type ) ) {
$post_types[$post_type] = $post_type;
}
}
return apply_filters( 'acf-post-types', array_diff( $post_types, $exclude ) );
};
In post_object.php for example, you can now use:$field['post_type'] = $this->parent->get_post_types();
Instead of:$field['post_type'] = get_post_types( array('public' => true) );
I would love to see something like this in a future release. This prevents me for having to set everything to public.
Cheers, Tobias
-
Hi @tschutter,
The code looks perfect to me. I'll add it into the to-do list and hopefully will be out soon.
Cheers
Elliot