Hi there @Hube2,
I too have required this functionality today, due to being at work I am unable to test this for a variety of taxonomies although I have used it for shopp-categories.
Please let me know how you get on with this.
https://github.com/WazzaJB/ACF-Taxonomy-Depth-Rule
Wazza
Hi @Wally
Your code suggests that the previous developer has a custom function to register custom fields. That or their are using another plugin to ACF as some of the parameters are missing / different.
I think some debugging is required from your end to see how the fields are registered with ACF
Hi @AmandaB
Thanks for the request. Your right, this would be a great feature and I’ll add it to the to-do
Cheers
Elliot
Hi @roger
My first question is to ask where the user is editing the post.
If the users are logging into the WP backend and editing posts via the normal wp-admin interface, then you could create a custom location rule!
A custom location rule will allow you to look at the current logged in user and compare it to the post’s author. If they match, return true, if not, return false.
This will show / hide the field group on a user basis!
You can read how to create custom location rules here:
http://www.advancedcustomfields.com/resources/tutorials/custom-location-rules/
Hi @ashkas
Thanks for the reply. I’m not sure why this would be causing any issues with ACF. It doesn’t seem to change any core functionality at all…
Is it possible that there is a Syntax error which is breaking the admin_enqueue_scripts action?
Aha, well in that case you were totally on the right path!
Filtering by “author” takes a user ID, while filtering by “author_name” will take a text string. Try this:
add_action( 'pre_get_posts', 'filtre');
function filtre($query) {
if( !is_admin() ) {
$query->set('author', get_current_user_id() );
}
return $query;
}
Or alternatively, you could get the string name first and filter by “author_name” instead:
$user = wp_get_current_user();
$query->set('author_name', $user->data->user_nicename );
So the linked post talks about limiting the results of post relationship custom field objects. This loop can be accessed and modified via AJAX hook path like so:
function my_post_object_query( $args, $field, $post )
{
// modify the order
$user_id = get_current_user_id();
$args['author'] = $user_id;
$args['post_type']="business";
// uncomment and test to see all set arguments:
// echo '<pre>'.print_r($args, true).'</pre>';
return $args;
}
// filter for every field
add_filter('acf/fields/post_object/query', 'my_post_object_query', 10, 3);
Since post relationship field results can be updated dynamically, this should let you restrict the results for every updated display. This particular example limits the results in two ways: by post type “business” and also by current user!
This is a pretty handy little snippet, I’m hanging on to this guy.
Check it out – just throw “old.” in front of the url to get to the old forums!
Not sure about your actual question though… will get back to you on that one.
And NOW I found the solution on the documentation.
Sorry. I’ll try to read first before I post again. 🙂
Sorry. Just had to use the same logic found on Customize the relationship field list query.
Here’s the code:
add_filter( 'acf/fields/post_object/query', 'change_posts_order' );
function change_posts_order( $args ) {
$args['orderby'] = 'date';
$args['order'] = 'DESC';
return $args;
}
Been xdebugging, and maybe this helps. core/fields/_functions.php:load_field( $field, $field_key, $post_id = false ) returns:
$f = array[18]
$f['name'] = (string) dato
$f['class'] = (string) date_time_picker
$f['time_format'] = (string) H:mm
$f['field_group'] = (int) 6583
$f['conditional_logic'] = array[3]
$f['conditional_logic']['allorany'] = (string) all
$f['conditional_logic']['rules'] = array[1]
$f['conditional_logic']['rules'][0] = array[2]
$f['conditional_logic']['rules'][0]['operator'] = (string) ==
$f['conditional_logic']['rules'][0]['field'] = (string) null
$f['conditional_logic']['status'] = (int) 0
$f['show_week_number'] = (string) false
$f['value'] = <string>
$f['required'] = (int) 0
$f['type'] = (string) date_time_picker
$f['id'] = (string) acf-field-dato
$f['key'] = (string) field_51d520b7091da
$f['label'] = (string) dato
$f['picker'] = (string) slider
$f['show_date'] = (string) true
$f['order_no'] = (int) 0
$f['instructions'] = <string>
$f['save_as_timestamp'] = (string) true
$f['date_format'] = (string) m/d/y
As you can see, the $f[‘value’] is empty
@wells5609 If you could help me a bit further, that would be great.
I’m trying to connect my field group to a ajax filter plugin. It should be able to just put the field key in the shortcode and it should work but it does not so I’m trying to edit the plugin a bit. I’ve been able to show the correct field list in the plugin, but it does not show the counts or use the filter function..
This is one part of the code (uag-wall-functions.php):
if (strstr($key,'metakey')) {
$matches = array();
if (preg_match_all('/{([^}]*)}/', $value, $matches)) {
$i = 0;
foreach($matches[1] as $array) {
$i++;
$all_arg = explode(',', $array);
foreach($all_arg as $arg) {
$str_arr = explode(':', $arg);
$metakey[$i][$str_arr[0]] = $str_arr[1];
}
/* ARGS */
$key = (isset($metakey[$i]['key'])) ? $metakey[$i]['key'] : null;
$field_key = (isset($metakey[$i]['field_key'])) ? $metakey[$i]['field_key'] : null;
$title = (isset($metakey[$i]['title'])) ? $metakey[$i]['title'] : __('Untitled','uag');
$reset_text = (isset($metakey[$i]['reset_text'])) ? $metakey[$i]['reset_text'] : __('Reset','uag');
$metavalues = get_field_object($field_key);
if( $metavalues )
{
$display .= '<div class="uag-filter" data-key="'.$field_key.'">
<div class="uag-filter-head"><span class="uag-toggle"></span>'.$title.'<div class="uag-helpers"><span class="uag-loading uag-hide"></span><a href="#" class="uag-reset">'.$reset_text.'</a></div></div>
<div class="uag-mainbody">
<div class="uag-filter-body">';
foreach($metavalues['choices'] as $metavalue) {
$display .= '<a href="#" class="uag-filter-option" data-metakey="'.$field_key.'" data-metavalue="'.$metavalue.'">'.$metavalue;
if ($args['count'] == 'show') {
$display .= '<span class="uag-count">'.uag_meta_count($field_key, $metavalue, $args).'</span>';
}
$display .= '</a>';
}
$display .= '</div>
</div>
</div>';
}
and the functions.php file:
/* Collect meta values */
function uag_meta_values($metakey) {
global $wpdb;
$prefix = $wpdb->prefix.'postmeta';
$posts = $wpdb->prefix.'posts';
$values = $wpdb->get_col("SELECT meta_value AS $metakey FROM $posts
JOIN $prefix ON ($prefix.post_id = $posts.ID)
WHERE post_status = 'publish'
AND meta_key = '$metakey'");
$values = array_unique($values);
return $values;
}
/* Count */
function uag_count_meta_values($metakey) {
global $wpdb;
$prefix = $wpdb->prefix.'postmeta';
$posts = $wpdb->prefix.'posts';
$values = $wpdb->get_col("SELECT meta_value AS $metakey FROM $posts
JOIN $prefix ON ($prefix.post_id = $posts.ID)
WHERE post_status = 'publish'
AND meta_key = '$metakey'");
$values = array_unique($values);
return count($values);
}
/* meta count by value */
function uag_meta_count($metakey, $metavalue, $args = null) {
$loop = array(
'post_type' => 'any',
'posts_per_page' => -1,
'meta_key' => $metakey,
'meta_value' => $metavalue,
);
Sorry, it’s a lot of code. But actually I’m not sure where to start in here. If you would point me in the good direction that would be enough. Thanks
Thanks for the response Elliot, unfortunately in our situation that did not solve the issue.
Did some further testing (switching plugins themes) and eventually narrowed it down to an action hook for admin_enqueue_scripts. We load css on the backend for some of the content editor forms, and with the issue happening with ACFs enqueue’s we were able to finally find the issue.
At the moment we’ve just commented out our own enqueue hook as we’re on the development server. Will provide the code of how we’re doing this, but we’ve never had an issue with this before (either with ACF nor other plugins).
function load_theme_admin_scripts($hook) {
// Admin CSS
wp_enqueue_style('theme_admin_css', get_template_directory_uri() . '/library/css/admin_css.css', false, '1.0', 'all');
}
add_action( 'admin_enqueue_scripts', 'load_theme_admin_scripts' );
Hi @Elliot,
url: "../wp-content/themes/twentytwelve/fields/test.php"
I’ve done it this way indeed, I’m not using the native wp_ajax functions. So that would be the problem. I’ll find out how to do it correctly, thanks.
Just to give anyone who could search for this some extra informaiton, here was the exact sql statement
“select * from wp_options where option_name like “%_category_%_start_date%”
where start_date is the name of my field.
Thanks again for your speedy answer.
Yep, you can jump over to the documentation page to view the actions / filters available.
Or just use the search above for acf/save_post
Hi @guiloviu
Looking at the code you have posted, there is clearly a problem with the way you have nested your arrays.
For example, you have
'relation' => 'AND',
and
'relation' => 'OR',
in the same array.
I think you should take another look at the WP_Query docs and also post this question on stack exchange as it does not involve any ACF specific functions.
Hi @jimmy.aat10
In general, yes, it is possible to filter the posts shown in an ACF field.
This said, I’m quite confused as to what fields you are using.
Is the field a post object or relationship?
Also, where would you want to add a column? TO what interface? Perhaps use a screenshot to demonstrate
My apologies, my head is wrapped around my goal. The access_pin is just a field that will be unique to a specific category. Do you have any examples of using the save_post action?
Thanks Elliot.
Yes, this would be quite an advanced feature, but something to think about.
Yes the subfields have different values.
I have managed to find a solution, I created a page with the repeater sub-fields laid out. I saved that page without a title and then use a plugin to duplicate it whenever I need a new page with those same repeater subfields already there.
I just think this would be a good feature as sometimes you have a lot of repeater subfields (like dropdown boxes with set values) that you use almost every time. A feature like this would ensure that they’re already setup when you create a new post.
Thanks Elliot, I was able to do that last night and it seems to work quite well
Hi @LeffDesign
In your above code, you have the AJAX call pointing to:
url: "../wp-content/themes/twentytwelve/fields/test.php",
Is this correct? If so, WP will not be loaded correctly, ACF will not be included and your PHP error will make sense.
In your first code however, you write that you are using the native wp_ajax functions.php AJAX solution.
Can you confirm which way you are coding as they dramatically will effect ACF’s usage
Hi @Steve Holland
This is not possible via the get_field function in ACF.
I would encourage you to look at writing a custom sql query to find all these values
Thanks
Elliot
Reading over your topic again, I may have miss understood the feedback.
I wonder if you could use the save_post action to first check that the values posted are unique. If not, you could then redirect the browser or find another way to abort the save.
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.