I think I already got it
$brand = get_field_object("brand");
$term = get_term($brand["value"], "pa_brand");
echo $brand["value"];
Thanks again – I’ve implemented the code successfully, and also added an additional line of code so that empty fields do not display. Here’s the final code.
<ul>
<?php
$fields = get_field_objects();
if( $fields ) {
foreach ($fields as $field_name => $field) {
if ($field['parent'] == '303') {
if ($field['value'] == '') {
continue;
}
echo '<li><b>';
echo $field['label'] . ": </b>";
echo $field['value'];
echo '</li>';
}
}
}
?>
</ul>
John,
Just wanted to let you know that your code did end up working. It wasn’t working when I was building the site locally on MAMP, but once I moved to a remote web server, it started working. I have no idea why it works now and not locally, but I’m just happy it’s working so not going to question it.
Thanks again for all your help, man!
John, thanks so much for helping out.
I’ve run the code you suggested, and you can see the results here (under the heading Optional Extras – the other two columns are coded line by line – not ideal): https://bushlapa.storiesandscience.co.za/caravans/goggas/rooimier/
Just to take a step back – what I’d like to achieve is the following:
– Display the field names and values from one field group at a time.
– Exclude any fields that do not have a value.
Here is a snippet of the printed result:
Array
(
[dry_weight] => Array
(
[ID] => 439
[key] => field_5937f37f96261
[label] => Dry weight
[name] => dry_weight
[prefix] => acf
[type] => text
[value] => 620
[menu_order] => 0
[instructions] =>
[required] => 1
[id] =>
[class] =>
[conditional_logic] => 0
[parent] => 303
[wrapper] => Array
(
[width] => 33
[class] =>
[id] =>
)
[_name] => dry_weight
[_prepare] => 0
[_valid] => 1
[default_value] =>
[placeholder] =>
[prepend] =>
[append] => kg
[maxlength] =>
)
[total_length] => Array
(
[ID] => 440
[key] => field_5937f3b496262
[label] => Total length
[name] => total_length
[prefix] => acf
[type] => text
[value] => 3360
[menu_order] => 1
[instructions] =>
[required] => 0
[id] =>
[class] =>
[conditional_logic] => 0
[parent] => 303
[wrapper] => Array
(
[width] => 33
[class] =>
[id] =>
)
[_name] => total_length
[_prepare] => 0
[_valid] => 1
[default_value] =>
[placeholder] =>
[prepend] =>
[append] => mm
[maxlength] =>
)
Your filter is basically altering every front end query for all logged in users that can upload files. You need to be more specific about what queries to alter.
This will get you started
add_action('pre_get_posts','allow_pending_posts_wpse_103938');
function allow_pending_posts_wpse_103938($qry) {
if (!is_admin() && current_user_can('upload_files') &&
$qry->is_main_query()) {
$qry->set('post_status', array('publish','pending','draft'));
}
}
I just checked my theme and this function is breaking the gallery functionality
add_action('pre_get_posts','allow_pending_posts_wpse_103938');
function allow_pending_posts_wpse_103938($qry) {
if (!is_admin() && current_user_can('upload_files')) {
$qry->set('post_status', array('publish','pending','draft'));
}
}
It basically adds post that are pending to the post archive.
Does anyone now why it is breaking the ACF gallery?
Thanks
Hey John,
Thanks for helping out here! I’ve taken your code and tried to get it working. On my side the code is not running – the resulting html just leaves me with the opening and closing
What am I doing wrong?
<ul>
<?php
$fields = get_field_objects();
if( $fields ) {
foreach ($fields as $field_name => $field) {
if ($field['parent'] != 'group_5937f81f79c5b') {
// not in first group
continue;
}
echo '<li>';
echo $field['label'] . ": ";
echo $field['value'];
echo '</li>';
}
}
?>
</ul>
Well, If I needed I’d go directly to the database, only because it would mean a lot less queries to do and would be a lot faster. This is quick to illustrate the queries. On the other hand, I don’t think that prepare can handle the ‘IN’ clause, at least I have never found anything that gives any adequate solution.
global $wpdb;
// 1. get all the post ID's of doctors
$query = 'SELECT ID
FROM '.$wpdb->posts.'
WHERE post_type = "doctor"
AND post_status = "publish"';
$ids = $wpdb->get_col($query);
// 2. get the relationship field for all of the above posts
$query = 'SELECT meta_value
FROM '.$wpdb->postmeta.'
WHERE post_id IN ("'.implode('", "', $ids).'")
AND meta_key = "my_relationship"'; // or whatever the field name is
$list = $wpdb->get_col($query);
// 3. get list of unique locations
$location_ids = array()
for ($i=0; $<count($list); $i++) {
$value = maybe_unserialize($list[$i]);
if (is_array($value)) {
for ($j=0; $j<count($value); $j++) {
if (!in_array($value[$j], $location_ids)) {
$location_ids = intval($value[$j]);
}
} // end for j
} // end if array
} // end for i
// 4. query locations
$args = array(
'post_type' => 'location',
'posts_per_page' => -1,
'post__in' => $location_ids
);
$locations = new WP_Query($args);
Thanks John. That’s kinda what I thought. I looked into doing it directly via the database but since the relationship field is serialized data it’s a bit tricky (though I’m sure not impossible).
For anyone stumbling upon this, I was able to solve it by looping through and building an array of the info I need.
I’m using “posts” and “authors” in place of “doctors” and “locations” but the idea is the same.
$posts = get_posts(array(
'post_type' => 'post',
'posts_per_page' => -1,
'meta_key' => 'author',
'meta_value' => ' ',
'meta_compare' => '!='
));
$authors_list = array();
foreach ($posts as $post) {
$related_authors = get_field('author', $post->ID);
foreach ($related_authors as $author_id) {
if (!in_array($author_id, $authors_list)) {
$authors_list[] = $author_id;
}
}
}
function author_sort($a, $b) {
return strcmp($a->post_title, $b->post_title);
}
usort($authors_list, "author_sort");
foreach ($authors_list as $author) {
echo '<a href="' . get_the_permalink($author_id) . '">' . get_the_title($author->ID) . '</a><br />';
}
No, not using a single WP_Query. This would not be possible even if the relationship field was on locations instead of doctors.
Using a standard WP_Query you would need to first get ALL doctors and loop through the posts and collect a list of all of the locations from them and then do another query to get the locations.
Even using standard WP custom fields this would be difficult due to what it is you’re looking to get.
There might be some ways to reduce the number of queries, or speed things up by using $wpdb https://codex.wordpress.org/Class_Reference/wpdb to directly query the database.
Giving the information I have I would say that this is more than likely due to the theme or another plugin. You need to deactivate other plugins to rule them out and switch to a basic theme to rule it out.
Look for errors.
1) Are there any JavaScript errors
2) Enable debugging and turn on error logging and see if there are any PHP errors reported during the AJAX request. https://codex.wordpress.org/WP_DEBUG
“Post Type is equal to Listing” is the rule. ‘Listing’ is the name of the custom post type
Just wanted to mentioned that mag07’s technique on page two of this thread works.
Setup fields in ACF
Create one record and populate each ACF field
Export posts/CPT using a plugin, I used WP Ultimate CSV Importer
Then open your CSV and copy+paste your fields from your source CSV to the appropriate column in the exported file.
Import new file using WP Ultimate CSV Importer
ACF fields will appear under “Custom Fields” and will not be restricted.
This approach is basically a more labor-intensive, but *free* alternative to simply paying for a premium plugin that will do it in one-swoop.
That did the job! The format is just good it’s just some font names and thats it!
I made from the echo:
<?php foreach ($values as $value) {
echo '<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=' . $value .':100,300,400,500,700,900">';
}
?>
And every font load in so that is working nice now!
Big thank you John!
There are several ways that you can do this, the most straight forward is to loop through all of your values outside the loop and gather all of the values into an array, checking to make sure there are not duplicates
$values = array();
// your loop starts
// somewhere inside your loops where you're getting the value
$value = get_sub_field('field_name');
if (!in_array($value, $values)) {
$values[] = $value;
}
// after the end of the loop loop through the
// collectionof $values and echo what is needed
The second method would be to create an acf/update_value filter https://www.advancedcustomfields.com/resources/acf-update_value/. In this filter you can possibly look at the value and store some other post meta value that has the list so you can just get this value instead of doing the loop. This would be pretty complex and would essentially just move the code from the front end into the admin and would probably require additional coding.
Another method would be to use an output buffer http://php.net/manual/en/book.outcontrol.php
ob_start();
// your current loop for outputting content here
// with additions as shown in the first option for gathering values
// after the loop, output the link and font values
// the echo the output buffer wherever it is the content needs to be displayed
echo ob_get_clean();
I just ran the same test on with a text field and I’m not getting any extra content stored. So the question is, where is the extra content coming from? ACF is not adding it.
The first step is to see what my be causing it by deactivating plugins and switching themes.
Hi John,
It’s so that a user can select a font (Google Font) and i can load it in the Googlefonts url:
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=here the value">
But a user can do it with all different elements so thats why i don’t need double values in that loop.
Hi John,
I am not a advanced php guy but i need to duplicate the loop again (first option) to make it work you say? I get the value now, thats good :-). But i was thinking maybe it was easier with way less PHP code.
Other question: i get the values now… But how can i check if their is a duplicate value in that loop and that all the duplicated will be removed/not showing.
What i now get:
Cat, Cat, Dog, Rat, Mouse, Rat, Cat, Dog, Mouse
But what i want to see:
Cat, Dog, Rat, Mouse
This can also happen because of at JavaScript error or a PHP error during the AJAX request.
What are the location rules for the field groups that are not appearing?
I could use this example as improvement request. Despite ACF block is forced after Title give it some margin-bottom, as other meatboxes have.
#acf_after_title-sortables .acf-postbox {
margin: 20px 0 0;
}
to:
#acf_after_title-sortables .acf-postbox {
margin: 20px 0;
}
I cloned completely Post Type generator from https://generatewp.com/post-type/.
Something as my own helper and for my own use, not public. Those guys have very educative website and generators.
77 ACF fields just for this part.
Layout can be done much simpler then on their webiste, but decided for now not to use one row flexible content or repeaters. Maybe later when I get use to all this. Those two fields make preview template very long and complicate to visually follow, edit, add.
And one custom code Metabox to preview template. Just called preview template with “require_once”, can be placed everywhere. Best solution, because it is set it ad forget it. Any code change in preview template is directly reflected in preview Metabox.
It can be done easy, just need much time. I finished it for one day. Others will probably take much less time. I can reuse and adapt preview code template.
Not “insert in editor content area” option, but not needed for this project.
Without knowing the exact index of each nested repeater it will be impossible to get a value from a specific nested sub field with such a complex setup. But in order to loop through this structure outside of the loop is to provide the post ID for the first
if (have_rows('page', $post_id)) {
while (have_rows('page', $post_id)) {
the_row();
// nested loops here
}
}
If you do know the exact index at each lever you can use get_post_meta() as suggested by @willh, this is how the meta key can be constructed. nested field indexes start at 0 and not 1.
$meta_key = "page_{$index}_row_{$index}_columns_{$index}_elements_{$index}_field_name";
$value = get_post_meta($post_id, $meta_key, true);
I think you misunderstood the second part. When you put HTML into the Field Label box, this HTML gets rendered in the field listing of the Field Group. For example, if for the Field Label I put:
<span style="display: block; font-size: 26px; font-weight: bold; line-height: 1.3em; color: #ccc; padding-bottom: 20px; border-bottom: 1px solid #333;">My Custom Header</span>
Then in the Field Group, it will display like this:
What I’m saying is that it should strip all HTML and CSS only in the field listing for the field group. Obviously on whichever page or post this field group is assigned to the HTML would render normally.
It is deeper that i thought!
In case that you can’t find anything else… make url-field to text-field and will work 100%
<a href="<?php echo get_post_meta($post->ID, 'Url', true); ?>"><?php echo get_post_meta($post->ID, 'Url', true); ?></a>
Well, that’s because I forgot something important
while ($query->have_rows('artikel', $post_id)) {
the_row(); // I forgot this
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.