I’m not 100% clear on what you’re looking for.
What to you mean by:
Normal WYSIWYG wordpress allows you when you first create the field to say whether or not you want it to put out HTML or just pure text.
I don’t have any knowledge of adding a wysiwyg field in WP other than using ACF to do it, so any documentation you can point me to would probably be helpful.
ACF4 uses a different editor than the main WP editor. ACF5 uses the same editor. There is a significant difference between these two versions of the plugin. The developer is working on releasing a free version of ACF to WP some time this year.
Are you trying to filter the posts that are returned by these fields or are you trying to somehow have the returned post array include the content of the fields?
The first is possible.
The second is not really possible. WP_Query does not return values from custom fields. To get these you must loop through the posts and get the values of the fields.
if ($featured_posts->have_posts()) {
while($featured_posts->have_posts()) {
$featured_posts->the_post();
$value = get_field('my_custom_field');
}
}
Is it an error in the post ID or in what is being returned by get_field that’s causing the issue?
The filter you have runs during an AJAX call, so it may be hard for you to see what’s going on. You can write values to your error log and then look in there.
function my_post_object_query( $args, $field, $post_id ) {
$class = get_field('class',$post_id);
error_log($post_id);
error_log($class);
// ...
Like I said, the only reason this should happen is if the ajax request fails, or never happens. Is the JavaScript running?
1) Is acf_form_head() before get_header() and run before any html is output?
2) Does your theme contain call to wp_head()
3) Does your theme contain call to wp_foot()
4) Are your deferring the loading of JavaScript or otherwise altering the way that JavaScript is loaded on the page?
I actually have WP_DEBUG on and I’m not getting any error in the browser or in the PHP error logs. As I mentioned I’m using acf_form
so this is a front-end form. Does that make any difference? What is being returned is a fully formed HTML response:
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US"><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width">
<meta name="robots" content="noindex,follow">
<title>Validation failed</title>
<style type="text/css">
html {
background: #f1f1f1;
}
body {
background: #fff;
color: #444;
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen-Sans, Ubuntu, Cantarell, "Helvetica Neue", sans-serif;
margin: 2em auto;
padding: 1em 2em;
max-width: 700px;
-webkit-box-shadow: 0 1px 3px rgba(0,0,0,0.13);
box-shadow: 0 1px 3px rgba(0,0,0,0.13);
}
h1 {
border-bottom: 1px solid #dadada;
clear: both;
color: #666;
font-size: 24px;
margin: 30px 0 0 0;
padding: 0;
padding-bottom: 7px;
}
#error-page {
margin-top: 50px;
}
#error-page p {
font-size: 14px;
line-height: 1.5;
margin: 25px 0 20px;
}
#error-page code {
font-family: Consolas, Monaco, monospace;
}
ul li {
margin-bottom: 10px;
font-size: 14px ;
}
a {
color: #0073aa;
}
a:hover,
a:active {
color: #00a0d2;
}
a:focus {
color: #124964;
-webkit-box-shadow:
0 0 0 1px #5b9dd9,
0 0 2px 1px rgba(30, 140, 190, .8);
box-shadow:
0 0 0 1px #5b9dd9,
0 0 2px 1px rgba(30, 140, 190, .8);
outline: none;
}
.button {
background: #f7f7f7;
border: 1px solid #ccc;
color: #555;
display: inline-block;
text-decoration: none;
font-size: 13px;
line-height: 26px;
height: 28px;
margin: 0;
padding: 0 10px 1px;
cursor: pointer;
-webkit-border-radius: 3px;
-webkit-appearance: none;
border-radius: 3px;
white-space: nowrap;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
-webkit-box-shadow: 0 1px 0 #ccc;
box-shadow: 0 1px 0 #ccc;
vertical-align: top;
}
.button.button-large {
height: 30px;
line-height: 28px;
padding: 0 12px 2px;
}
.button:hover,
.button:focus {
background: #fafafa;
border-color: #999;
color: #23282d;
}
.button:focus {
border-color: #5b9dd9;
-webkit-box-shadow: 0 0 3px rgba( 0, 115, 170, .8 );
box-shadow: 0 0 3px rgba( 0, 115, 170, .8 );
outline: none;
}
.button:active {
background: #eee;
border-color: #999;
-webkit-box-shadow: inset 0 2px 5px -3px rgba( 0, 0, 0, 0.5 );
box-shadow: inset 0 2px 5px -3px rgba( 0, 0, 0, 0.5 );
-webkit-transform: translateY(1px);
-ms-transform: translateY(1px);
transform: translateY(1px);
}
</style>
</head>
<body id="error-page">
<p></p><h2>Validation failed</h2><ul><li><div style="text-align: center"><h3>Unable to update your profile</h3>
<p>We were unable to update your profile because your "About Me" was longer than 515 characters.</p>
<p><a href="javascript:void(0);" onclick="window.location.reload(true);" class="button">Back to profile</a></p></div></li></ul><p></p>
</body></html>
Thanks for the response John.
I don’t think any of those points you made are an issue, because when I substitute a string from one of the choices in the class pulldown, the query works as expected.
I understand that the $post_id defaults to the current post, but I don’t know if something got changed with an update. It’s just not getting the $post_id from the current page for me.
basically, if you want to get all of the posts in a term then you should use WP_Query https://codex.wordpress.org/Class_Reference/WP_Query and the documentation will lead you there https://developer.wordpress.org/reference/functions/query_posts/
The important bit is the tax_query and the fact that your field is returning an term object. You could query by any of the fields of the returned object.
If you want to see what the object looks like
$department = get_field('department');
echo '<pre>'; print_r($depearment'); echo '</pre>';
Here is
<?php
$department = get_field('department');
$query = array(
'post_type' => 'staff',
'orderby' => 'menu_order',
'order' => 'ASC',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'departments',
'field' => 'term_id', // not needed, default value
'operator' => 'IN', // not needed, default value
'terms' => array($department->term_id)
)
)
);
$team_query = WP_Query($args);
if ($team_query->have_posts()) {
while($team_query->have_posts()) {
$team_query->the_post();
// output stuff about this post
}
wp_reset_postdata();
}
more information about querying based on different types of acf fields can be fount here https://www.advancedcustomfields.com/resources/query-posts-custom-fields/
Hello, I have the following problem:
I have 4 categories hierarchies:
Category
– Subcategory
— Mark
— Model
With your help I managed to make the subcategories display normally, however, from the mark I can no longer follow the hierarchy updating the query.
Do you have any idea how I can get it?
Attached is an example of the fields I need.
You could also do
if (!$valid || !$value) {
return $valid;
}
Most of the examples that the developer has given are supposed to be pretty basic, only including the necessary parts. There are many cases where if you use the code as given, for example many of the pre_get_post filter examples, that you’ll likely mess up other queries that need to be done because no checks are given to make sure the correct queries are altered.
As far as altering the code, no validation filter knows what the other filters are validating. ACF just does apply_filters(.....
. There isn’t even a guarantee that the internal ACF required field filter will actually run before your custom filter runs. It just runs through them all until one of them fails.
This part of your code is really unnecessary because the current post ID is passed to your filter
global $post;
setup_postdata( $post );
$post_id = $post->ID;
$ID = $post->ID;
There are several other things that may or may not be going on.
Can the field class have multiple selections? If it can then you’ll need to do a “LIKE” query on this field.
If this field was altered and “Select multiple values” was changed then some or your posts may contain single values and some may contain serialized arrays. If this is the case then you’re going to have to do an “OR” query checking for both == and LIKE.
What is the return format set to, Post Object or Post ID? If it’s returning a post object then you’ll need to extract the post ID from the object returned. This could also be effected by the multiple factor and could be returning an array of post object.
What type of field is playoff_game? I’m guessing a true false field. Does this field exist for every existing post or was it added. Posts that do not have this field at all could be throwing off the query, although I’m not sure about this.
I kept the field as “library:all”, but filtered the media that could be seen like so (fill in your own USERTYPE):
/**
* Limit media access to only their own media for certain users
*/
add_filter( 'ajax_query_attachments_args', 'mr_mp_filter_media' );
function mr_mp_filter_media( $query ) {
if ( current_user_can( 'USERTYPE' ) || current_user_can( 'USERTYPE2' ) ) {
$query['author'] = get_current_user_id();
}
return $query;
}
This does not work?
Page Type
is equal to
Parent Page (has children)
I already know that it’s not likely that custom location rules will be added for custom post types, at least not by the developer. So it the above does not work your only real option is to create a custom location rule.
And I do have an example that will do something similar here https://github.com/Hube2/acf-filters-and-functions/blob/master/page-nth-level-location-rule.php that I’m sure can be made to work with a few tweaks.
Thanks for the quick reply!
The first part is now solved with the filter you mentioned.
For the second part, the page parent will only return a list of my pages. If it also returned the ‘0’ value then i could use this. I will check the links you sent and see what i can manage.
Still it would be nice to also have a post parent in the location rules or also include the ‘0’ in page parent.
thank you.
Sorry, missed the point of your questions. You would need to set up an acf/load_field filter. In the filter check if (is_admin()) { return $field }
. Then you can set the correct field setting to limit the characters. I don’t know what that setting is, you’d need to look at the plugin I linked to and/or ask the developer.
@phylaxis when this happens it is usually because there is a PHP error generated during the AJAX request for the validation. Turn on debugging and error logging and then check in your error log to see what is causing the request to fail. https://codex.wordpress.org/WP_DEBUG
The first part can be done using this filter https://www.advancedcustomfields.com/resources/acf-fields-post_object-query/
The second part requires creating a custom location rule https://www.advancedcustomfields.com/resources/custom-location-rules/. I have some example custom location rules here https://github.com/Hube2/acf-filters-and-functions. There’s not one that is specifically for what you want. If you dig around in ACF and find the page type location rules you can pretty much copy most of what you need from ACF for use with your custom post type. Although, I just dug around in ACF and it seems that the Page Type location rule should work for parent/child no matter what post type you use it on (unless I’m missing something)
What do you have the image field set to return? It is probably set to “Image Object” or “Image Array” depending on what version of ACF you’re using. Either set it to return the Image URL or do
<img src="<?php $image = get_field( 'image-1', $page->ID );
echo $image['url']; ?>">
For anyone looking for something similar in the future, this is one way to do this
<?php
// displaying apartment(s)
while(have_posts()) {
the_post();
// display other information about the apartment
// get post ID of resort from apartment page
// assumes post object field does not allow multiple
$resort = get_field('resort', false, false);
// query specials post type for same resort
// again, assumes post object field does not allow multiple
$args = array(
'post_type' => 'special',
'posts_per_page' => -1,
'meta_query' => array(
'key' => 'resort',
'value' => $resort
)
);
$resort_query = WP_Query($args);
if ($resort_query->have_posts()) {
while($resort->have_posts()) {
$resort_query->the_post();
// display information about special
} // end while
wp_reset_post_data();
} // end if have_posts
// possibly show more info about apartment after special query
} // while have posts
?>
Hi @chrisdavies71,
You can use this filter to add your custom script file:
http://www.advancedcustomfields.com/resources/acfinputadmin_enqueue_scripts/
You can add the Jquery as follows
$( "#datepicker" ).datepicker({
defaultDate: null
});
That didn’t work. It returned the post id of the last post in the query of the get_away_record_update function and used that as the $home_team parameter of the get_home_record_update function. Thanks though!
Just a quick comment; if anyone tries the code above by @acf-support there’s a period missing before “acf-label”:
.acf-field-1234567890 > acf-label {display: none;}
should be
.acf-field-1234567890 > .acf-label {display: none;}
Hi @blaasvaer
Thanks for the follow up.
That is quite tricky as you would need to implement some custom action to dynamically add items to the custom taxonomy by clicking on the add new link.
I’m afraid that is currently not possible from the plugins API.
I know it’s been a year but, in case this can help someone, this seems to do it, at least for me:
add_action('acf/render_field_settings', 'hidelabel_render_field_settings');
function hidelabel_render_field_settings( $field ) {
acf_render_field_setting( $field, array(
'label' => __('Hide Label?'),
'instructions' => '',
'name' => 'hide_label',
'type' => 'true_false',
'ui' => 1,
), true);
}
add_filter('acf/prepare_field', 'hidelabel_prepare_field');
function hidelabel_prepare_field( $field )
{
if ($field['hide_label'])
echo '
<style type="text/css">
.acf-field-', substr($field['key'],6),' > .acf-label {display: none;}
</style>';
return $field;
}
Indeed, it’s not possible with get_terms (get_categories) args, cause final query contains “INNER JOIN wp_termmeta” but acf doesn’t use this table
So loop through it with usort()
the shortest variant for now is
usort($terms, function($a. $b) use ($taxonomy) {
return get_field('sort_order', $taxonomy.'_'.$a->term_id) - get_field('sort_order', $taxonomy.'_'.$b->term_id);
});
I got this sorted via email support, thank you very much. This is what the result looks like:
<?php
// if 'file' repeater has rows
if( have_rows('file') ): ?>
<!-- files select button -->
<button onclick="download()" type="button">Download</button>
<div id="download">
<!-- files select list -->
<?php
// loop through the 'file' rows of data
while ( have_rows('file') ) : the_row();
$files = get_sub_field('files');
$file_name = get_sub_field('file_name'); ?>
<!-- display file name with URL in download list -->
<a href="<?php echo $files['url']; ?>" target="_blank"><?php echo $file_name; ?></a>
<?php
// endwhile loop for 'file' repeater
endwhile; ?>
</div>
<?php
// else if 'file' repeater has no rows
else :
// no rows found
endif;
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.