I can’t process feature requests, you might want to contact the developers https://www.advancedcustomfields.com/contact/
@hube2 Thanks for taking some brain time to parse and process my response in an intelligent and mindful way 🙂
That makes sense. It would be really nice if ACF could re-initialize when switching to blog, so that it can fetch the fields from whatever site is being used on a multi-site at runtime. I understand this is not possible now.
I’ll try the field definition approach, but I’d like to ask for the above as a feature request, who knows, perhaps it might be easy to re-init after switching to blog? I wouldn’t know.
But thank you once again for your attention!
your need to see if orderby is in the url query. This cannot be done by looking at $query->get( 'orderby');
because by the time of pre_get_posts WP has already set it to the default of date in the admin.
if (empty($_GET['orderby'])) {
// set default orderby
}
This gives me, as expected, the date
as the query input. However, I thought that the rest of the snippet provides different query parameters, doesn’t it?
@hube2 That makes sense, thank you.
I understand that the value formatting (eg: return Both
) comes from the ACF Store “Values”, therefore the function acf_switch_stores
being called on switch_blog
should update ACF Stores to point to the multi-site instance after the switch.
So, this is what I think is happening:
1. Request starts at main site
2. ACF initializes naturally, which triggers acf_register_store('values')
3. I run switch_to_blog(2)
on the context of filtering a field value, which fires the hook acf_switch_stores
4. ACF switches to that multisite instance store, but the store isn’t initialized in this site instance, so the values
store is empty
It works at plugins_loaded
because:
1. Request starts at main site
2. On plugins_loaded, I call switch_to_blog
and then get_field
, which internally calls ACF->initialize()
on the context of the switched multi-site instance, therefore initializing the stores on the context of that site instance
Would it make sense for ACF to register the stores again on the acf_switch_stores
function, if they are not registered yet?
The only way to do that is to inform people that the ACF plugin is required. There are things that you can add to your plugin that will automatically install other plugins if they are not installed so that they can be updated as other plugins.
What you want to do for loading the custom fields can be done by adding an acf-json folder to your plugin, move the field groups for your plugin into this folder and then adding a load point https://www.advancedcustomfields.com/resources/local-json/
You can hide your field groups so they cannot be syned by editing the json file and setting them to private
"modified": 1503334184,
"private": true
Check the headers that are being sent to the browser. When a PDF is downloaded instead of displayed it is an indication the the headers or not correct, for example Content-Type: application/octet-stream.
will cause a PDF to download instead of display. https://stackoverflow.com/questions/6293893/how-do-i-force-files-to-open-in-the-browser-instead-of-downloading-pdf
Sorry, I probably missed it.
Check the values you are passing to the query
$orderby = $query->get( 'orderby');
echo $orderby; die;
this will give tell you what the value is and exit so that you can see it.
As I said, the ACF JS API cannot be used for displaying data. The input field must be on the page, which it is not on the front of the site unless this is an ACF form.
I don’t know how else to explain this. ACF JS functions are for making changes in the admin or when editing content, not for displaying content.
Where you are doing this var field = acf.getField('fork_model');
you need to make an AJAX request to the server to get the data.
so, i have fields in the products i want to show on the site.
The problem is that I have to use jQurry because of the dynamic display.
I tried to test in the console like this first, but I can’t get the values
function action_wc_before_single_variation() {
?>
<script type="text/javascript">
(function($){
$('form.variations_form').on('show_variation', function(event, data){
console.log( data.variation_id ); // The variation Id <=== <===
console.log( data.variation_description ); // The variation description <=== <===
var field = acf.getField('fork_model');
console.log( $field );
});
})(jQuery);
</script>
<?php
}
Am I wrong somewhere?
After a couple of seconds, the image should change to another.
In addition to the above, changing to another random image would requires some type of slider application built in JavaScript. This is not something that can be done with PHP.
The ACF JS files (API) are only loaded on the admin and will only work for editing. The ACF JS does not run on the front end and cannot be used to get values of fields.
If I am somehow misunderstanding where you are attempting to use this you need to use acf.getField() and this function requires a field key, not field name.
https://www.advancedcustomfields.com/resources/javascript-api/#acf.field
The problem with random queries is that they are highly memory intensive.
Your code above will load the image, but will your server allow rand()? that would be the first question. Many hosting providers will not allow it. If it WILL allow, then i’ll try to help you with the actual code.
I solved it.
<?php if( have_rows('affiliatepartner') ): ?>
<div class="external-links">
<?php while( have_rows('affiliatepartner') ): the_row(); ?>
<?php
$produkturl = get_sub_field('produkturl');
$buttontext = get_sub_field('buttontext');
?>
<a href="<?php echo esc_url( $produkturl ); ?>" class="button product_type_external" aria-label="<?php echo $buttontext; ?>" rel="nofollow" target="_blank"><?php echo $buttontext; ?></a>
<?php endwhile; ?>
</div>
<?php endif; ?>
Maybe it will help you, I solved it. The individual vars must be called within the repeater. In my case:
<?php if( have_rows('affiliatepartner') ): ?>
<div class="external-links">
<?php while( have_rows('affiliatepartner') ): the_row(); ?>
<?php
$produkturl = get_sub_field('produkturl');
$buttontext = get_sub_field('buttontext');
?>
<a href="<?php echo esc_url( $produkturl ); ?>" class="button product_type_external" aria-label="<?php echo $buttontext; ?>" rel="nofollow" target="_blank"><?php echo $buttontext; ?></a>
<?php endwhile; ?>
</div>
<?php endif; ?>
Hello, does anyone read the inquiries?
This code got what I needed. Make sure to add the #hide_this_div to the whatever you want to hide and change the ‘ACF-field-name’ in this code to your field name. You can change the ‘post’ to any CPT.
add_action('template_redirect', 'hide_disabled_blocks');
function hide_disabled_blocks() {
global $post;
// get dynamic project id
$post_id = $post->ID;
// get value of true-false field in each post
$hidediv = get_post_meta($post_id, 'ACF-field-name', true);
if is_singular( 'post' ) :?>
<style type="text/css">
<?php
// if "ACF true-false command" is false, hide the Div ID
if ( ($hidediv == '0') || (empty($hidediv)) ): ?>
#hide_this_div { display: none; }
<?php endif;
?>
</style>
Try this code. Make sure to add the #hide_this_div to the whatever you want to hide:
add_action('template_redirect', 'hide_disabled_blocks');
function hide_disabled_blocks() {
global $post;
// get dynamic project id
$post_id = $post->ID;
// get value of true-false field in each post
$hidediv = get_post_meta($post_id, 'ACF-field-name', true);
if is_singular( 'post' ) :?>
<style type="text/css">
<?php
// if "ACF true-false command" is false, hide the Div ID
if ( ($hidediv == '0') || (empty($hidediv)) ): ?>
#hide_this_div { display: none; }
<?php endif;
?>
</style>
add_row() just like update_field() when a field does not already exist in the DB then you must use field keys instead of field names to add values from PHP
$row = array(
'fiield_XXXXX' => esc_sql($_POST['title']),
'fiield_YYYYY' => esc_sql($_POST['description']),
'fiield_ZZZZZ' => $attachment_id,
);
add_sub_row(array('tabs', $tab_counter, 'downloads'), $row, $machine_id);
WPForms provides the ability to upload files to drive for form submissions. From the search results I just looked at other premium form plugins also do this. This is not something you are likely to be able to do using ACF. It would require an API connection with Google that I assume that the premium form plugins have.
I have spend multiple hours trying to figure a way around this and to my likings I haven’t found an ideal solution. The problem is Innerblocks isn’t really an ACF function but a WordPress function.
Although I don’t really understand your question the closest I’ve come to a workable pagebuilder with container+columns is this way:
This is a content container block
$allowed_blocks = [ 'acf/contentpartial' ];
<div id="<?php echo esc_attr($id); ?>" class="<?php echo esc_attr($className); ?>">
<?php if($container == 'container-fluid'): echo '<div class="container">'; endif; ?>
<div class="row">
<InnerBlocks
allowedBlocks="<?php echo esc_attr( wp_json_encode( $allowed_blocks ) ); ?>"
/>
</div>
<?php if($container == 'container-fluid'): echo '</div>'; endif; ?>
</div>
In which you load the partial:
$allowed_blocks = [ 'core/heading', 'core/paragraph','core/list', 'core/button', 'core/image', 'core/shortcode', 'gravityforms/form'];
$col = get_field('col_grootte');
?>
<div id="<?php echo esc_attr($id); ?>" class="<?php echo esc_attr($className); ?> <?php echo $col; ?> pt-<?php the_field('ruimte_boven'); ?> pb-<?php the_field('ruimte_onder'); ?> editor-reset">
<div class="content-block">
<InnerBlocks allowedBlocks="<?php echo esc_attr( wp_json_encode( $allowed_blocks ) ); ?>" />
</div>
</div>
This only problem is that you have to manually select the column size. It can’t be col-6 whenever there are 2 partials automatically.
Hi @Flaschenzug,
Thank you for your answer ! The issue came from the SSL certificate that expired on the 1st of october, a few hours after I experimented with that. It’s now fixed and this code is working again.
I asked myself the same questions but didn’t get much debug information, with devtools the src in image tag wasn’t filled with the image link and no error would come up. I did check if all the links in medias were in https and they were.
I don’t have much knowledge in the server side but I guess it wouldn’t let wordpress process https links when its SSL certificate is expired.
Yes, I can confirm that ACF is having a hard time loading formatted values in a multi-site field when running during the context of a acf/load_field/name=
.
1. Request starts at main site
2. I have a filter such as: add_filter('acf/load_field/name=schools', function($field) {});
3. Inside the closure of this filter, I run switch_to_blog(3)
and then get_field('state', 'options');
Where state
is a field of type “Select” such as:
ny : New York
fl : Florida
And is configured to return Both (Array)
. But due to a probable issue with ACF, likely at the values
data store, it returns the unformatted value ny
only.
What I had to do was a dirty workaround:
// Pre-load the school state value in the "plugins_loaded" context
add_action('plugins_loaded', function() {
if (!is_not_editing_schools()) {
return;
}
switch_to_blog(3);
global $school_state;
$school_state = get_field('state', 'options'); // ['ny' => 'New York'], as expected
});
add_filter('acf/load_field/name=schools', function($field) {
global $school_state;
// Now I have ['ny' => 'New York'] here, while in this context I was only able to get 'ny' out of get_field after switch_to_blog.
});
Hi John,
many thanks. This works fine.
But how can I add a “read more” Link after each answer?
Example: <p>Read more about <strong><a href="<?php the_permalink(); ?>"><?= the_title(); ?></a></strong></p>
I would try using htmlspecialchars().
On the other hand if I was doing this I would probably built and array in PHP and then json_encode that array for output allowing PHP to deal with special characters.
<?php
if ($faq) {
$ld = array(
'@context' => 'https://schema.org',
'@type' => 'FAQPage',
'mainEntity' => array()
);
if (have_rows('faq')) {
while (have_rows('faq')) {
the_row();
$ld['mainEntity'][] = array(
'@type' => 'Question',
'name' => get_sub_field('question'),
'acceptedAnswer' => array(
'@type' => 'Answer',
'text' => get_sub_field('answer')
)
);
}
}
?>
<script type="application/ld+json"><?php echo json_encode($ld); ?></script>
<?php
}
?>
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.