@yueng actually, wp_enqueue_scripts() should load on the frontend & backend for your blocks when within the enqueue_assets attribute in acf_register_block(), here’s my code that is loading the JS file in the frontend and backend:
'enqueue_assets' => function(){
//Stack the Assets in the Order we Need Them
wp_enqueue_style( 'general-block-style', get_template_directory_uri() . '/template-parts/block/css/all-blocks.css', array('foundation') ); //Requires Foundation
wp_enqueue_style( 'slideshow-block-style', get_template_directory_uri() . '/template-parts/block/css/slideshow.css', array('general-block-style') ); //Requires all-blocks.css
wp_enqueue_style( 'slick-main', get_template_directory_uri() . '/template-parts/block/css/slick/slick.css' );
wp_enqueue_style( 'slick-theme', get_template_directory_uri() . '/template-parts/block/css/slick/slick-theme.css' );
wp_enqueue_script( 'slideshow-block-library', get_template_directory_uri() . '/template-parts/block/js/slick.min.js', array('jquery'), '', true );
wp_enqueue_script( 'slideshow-block-script', get_template_directory_uri() . '/template-parts/block/js/slideshow.js', array('jquery'), '', true );
},
I took a second look at my slideshow JS file and the only other difference I’m seeing is the each jQuery function (that iterates through every slideshow block on the frontend/backend) like so:
$(document).ready(function(){
$('.banners').each(function(){
init_banner( $(this) );
});
});
@yueng actually, the wp_enqueue_script, when included in the enqueue_assets part of acf_register_block should include it in the frontend and backend for your block, here’s what mine looks like:
...
'enqueue_assets' => function(){
//Stack the Assets in the Order we Need Them
wp_enqueue_style( 'slideshow-block-style', get_template_directory_uri() . '/template-parts/block/css/slideshow.css', array('general-block-style') ); //Requires all-blocks.css
wp_enqueue_style( 'slick-main', get_template_directory_uri() . '/template-parts/block/css/slick/slick.css' );
wp_enqueue_style( 'slick-theme', get_template_directory_uri() . '/template-parts/block/css/slick/slick-theme.css' );
wp_enqueue_script( 'slideshow-block-library', get_template_directory_uri() . '/template-parts/block/js/slick.min.js', array('jquery'), '', true );
wp_enqueue_script( 'slideshow-block-script', get_template_directory_uri() . '/template-parts/block/js/slideshow.js', array('jquery'), '', true );
},
...
Also, I took a second look at my slideshow javascript and the only real difference I have is an each jQuery function (which might help):
$(document).ready(function(){
$('.banners').each(function(){
init_banner( $(this) );
});
});
Let me know if that helps.
I actually had exactly the same issue after creating a slideshow block with slick, as well as all of my other blocks that use javascript.
The problem was, I copied and pasted the wp_enqueue_script some documentation at some point and I had (notice the lack of a version #):
wp_enqueue_script( 'slideshow-block-library', get_template_directory_uri() . '/template-parts/block/js/slick.min.js', array('jquery'), true );
When I should have had:
wp_enqueue_script( 'slideshow-block-library', get_template_directory_uri() . '/template-parts/block/js/slick.min.js', array('jquery'), '1.0.0', true );
For more info, see the WordPress Docs here.
You can force an ACF custom block to show each new Page that is created by setting up a Gutenberg template that includes the block you’d like, with this code:
function pages_guten_template() {
$post_type_object = get_post_type_object( 'page' );
$post_type_object->template = array(
array( 'acf/custom-block-name' ),
);
}
add_action( 'init', 'pages_guten_template' );
More details on templates can be found here (including how to lock it in place, if you so desire).
This feature would be very handy!
I needed to include a common CSS file for all of my blocks and I was able to do it with this:
function enqueue_foundation_in_admin() {
$current_screen = get_current_screen();
if ( method_exists( $current_screen, 'is_block_editor' ) && $current_screen->is_block_editor() ) {//Check if we're on a Gutenberg Page
wp_enqueue_style( 'foundation', get_template_directory_uri() . '/scripts/foundation-6.5.1.css' );
}
}
add_action( 'admin_enqueue_scripts', 'enqueue_foundation_in_admin' );
Note: this could effect other items in the WordPress admin (text, divs etc.), so be cognizant of that. Otherwise the alternative is using “enqueue_assets” and wp_enqueue_style and just declaring the CSS file for each block.
I figured it out. Basically, in the AJAX response I iterated through each of the post ID’s of the Questions post type and added them to the select field of the post object ACF field as an <option>, which successfully populates the post object <ul>
visible to admins after the Exam post is published (in theory, you could also add a <li>
to the <ul>
for each question if you needed to display it to the admin, but I didn’t).
$.post(ajaxurl, examData, function(response) {
var questionSelect = $('select#acf-field_5c9b994d45a1a');
for(i=0; i<response.length; i++) {
var question_item = '<option value="'+response[i]+'" selected="selected" data-i="' + i + '">'+response[i]+'</option>';
questionSelect.append(question_item);
}
$('#publish').click(); //Save the Exam Post
console.log(response);
}, "json");
Well now, that was almost too easy!
I added that action and used the exact same function as my acf/save_post filter; now I’m running an Import updating the content (which is unused) and all of the “_wp” meta entries are getting added. Thanks for your help John! Also, WPAI is great.
Yes, I’m using WP All Import
Shot in the dark, as this is pretty much what you had, but give something like this a try:
//Perhaps use the field_key here
if( have_rows('content_type', $post_id) ) :
while( have_rows('content_type', $post_id) ) : the_row();
if(get_row_layout() == 'text_field'): //The Name of our Flexible Content Row
//Perhaps use the field_key here
if( have_rows('label') ) :
while( have_rows('label') ) : the_row();
//Perhaps use the field_key here
update_sub_field('subtitle', "This is a new Subtitle!");
endwhile;
endif;
endif;
endwhile;
endif;
Can you post a screenshot of structure of your ACF Flexible Content field group page (i.e. the interface where you created your Flexible Content field)?
I’m not sure it’ll help you exactly, but figured I’d chime in with something I spent a while troubleshooting for update_sub_field(), for a custom admin interface I was working on. I wanted to update multiple fields within a Group, within a Repeater. Here’s my code:
$values = array(
//field1 => value
'field_5aac179f96382' => 'emailed',
//field2 => value
'field_5aac164496381' => 'TEST5678!'
);
//update_sub_field( array(repeater_name, row_id, group_name), $values, post_id )
update_sub_field( array('field_5a71dfd39fac7', 1, 'field_5a7209ec9facd'), $values, 116 )
What I did to troubleshoot in my situation, was create a new WordPress page and outputted the above code on that page. Then, I know when I visit that page in the frontend, that my field “should” be updated on my post and if it doesn’t then there’s something wrong with my code.
Okay, so if you have multiple datepicker fields on a Course, try something like this on your single-course.php to only show dates that are in the future:
$today = date( 'Y-m-d' );
if(get_field('date_1') > $today):
echo '<p>' . get_field('date_1') . '</p>';
endif;
if(get_field('date_2') > $today):
echo '<p>' . get_field('date_2') . '</p>';
endif;
etc...
Again, just make sure that the date format for $today matches what you specified in your datepicker field in the ACF settings. If that doesn’t work, you may need to do a php strtotime() with the dates.
Querying Courses by multiple ACF field dates would be a little trickery.
$today = date( 'Y-m-d' );
$args = array(
'post_type' => 'course',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'date_1',
'value' => $today,
'compare' => '>',
),
array(
'key' => 'date_2',
'value' => $today,
'compare' => '>',
),
),
);
$query = new WP_Query( $args );
I just don’t think your can order a WP Query by multiple fields.
Does each Course have a repeater field with multiple dates in it or have you created multiple courses with the same name that have a different date assigned to them?
You could do something like this to get all courses after today:
$today = date( 'Y-m-d' );
$args = array(
'post_type' => 'course',
'meta_query' => array(
array(
'key' => 'course_date',
'value' => $today,
'compare' => '>=',
'type' => 'DATE'
)
)
):
$query = new WP_Query( $args );
Note: the ‘key’ would be your ACF field name that contains the date. You may need to adjust the format of the date.
Solved it!
I found this post which lead me to an answer.
Here’s my code:
$course_id = $data['course']->ID; //280
$coursePoint = get_field('field_56646f51fa48b', $course_id);
//Get the ID of the Point that was was assign to the Course
if( $coursePoint ):
$post = $coursePoint;
setup_postdata( $post );
$selectedCourseID = $post->ID;
wp_reset_postdata();
endif;
$value = get_field('field_56644f11d62c3', $user_ID);
$value[] = array("field_566459158d2ab" => $selectedCourseID, "field_5664598f8d2ad" => "20151206");
Ok, so I figured out how to properly get the ID of the course that was completed:
$course_id = $data['course']->ID
So, how can I then take my course and insert it into a repeater (with a Post Object sub field)? Here’s what I have so far, which isn’t working:
$course_id = $data['course']->ID;
$coursePoint = get_field('field_56646f51fa48b', $course_id);
$value = get_field('field_56644f11d62c3', $user_ID);
$value[] = array("field_566459158d2ab" => $coursePoint, "field_5664598f8d2ad" => "20151206"); //The second field in the Repeat (Date) gets into a new row fine
update_field( $field_key, $value, $user_ID );
For reference, the multidimensional array that is output by $data is structured like this:
Array
(
[user] => WP_User Object
(
[data] => stdClass Object
(
[ID] => 30
[user_login] => testing
[user_pass] => $P$BQqHM2nDR2C3WB87v2gd83R0ZCtRug.
[user_nicename] => testing
[user_email] => [email protected]
[user_url] =>
[user_registered] => 2015-11-23 15:59:37
[user_activation_key] =>
[user_status] => 0
[display_name] => James Test
)
[ID] => 30
[caps] => Array
(
[s2member_level4] => 1
[access_s2member_ccap_member] => 1
)
[cap_key] => wp_lxkfgb_capabilities
[roles] => Array
(
[0] => s2member_level4
)
[allcaps] => Array
(
[read] => 1
[level_0] => 1
[access_s2member_level0] => 1
[access_s2member_level1] => 1
[access_s2member_level2] => 1
[access_s2member_level3] => 1
[access_s2member_level4] => 1
[s2member_level4] => 1
[access_s2member_ccap_member] => 1
)
[filter] =>
)
[course] => WP_Post Object
(
[ID] => 280
[post_author] => 1
[post_date] => 2015-12-05 11:21:55
[post_date_gmt] => 2015-12-05 16:21:55
[post_content] => This is another course!
[post_title] => Another Course
[post_excerpt] =>
[post_status] => publish
[comment_status] => closed
[ping_status] => closed
[post_password] =>
[post_name] => another-course
[to_ping] =>
[pinged] =>
[post_modified] => 2015-12-06 12:27:39
[post_modified_gmt] => 2015-12-06 17:27:39
[post_content_filtered] =>
[post_parent] => 0
[guid] => http://testsite.com/vra/?post_type=sfwd-courses&p=280
[menu_order] => 0
[post_type] => sfwd-courses
[post_mime_type] =>
[comment_count] => 0
[filter] => raw
)
[progress] => Array
(
[280] => Array
(
[lessons] => Array
(
[316] => 1
)
[topics] => Array
(
[316] => Array
(
[327] => 1
)
)
[completed] => 1
[total] => 1
)
)
)
I was able to fix my error with the checkboxes of field options by changing the field name to the field key like so (for both custom fields):
$field = get_field_object('field_54d4ec8875f85');
$field2 = get_field_object('field_54d4e73975f79');
I referenced this: http://www.advancedcustomfields.com/resources/get_field_object/
Now I just need to sort out how to change the javascript to apply both custom filters. I’ll report back if I figure it out.
I have the same issue, however, I was able to figure out how to get the url to work by modifying the meta_query in functions.php:
$meta_query[] = array(
relation => 'AND',
array(
'key' => 'community',
'value' => $community,
'compare' => 'IN',
),
array(
'key' => 'listing_status',
'value' => $status,
'compare' => 'IN',
),
);
So, when I visit http://www.website.com/listings/?listing_status=sold&community=cityname, if I have a Listing that is both sold and in cityname, the listing is displayed and my checkboxes show in the sidebar. However, as soon as my URL changes to something like http://www.website.com/listings/?listing_status=sold&community=newcityname (where “newcityname” is not marked as “sold”) I get the following error when where the checkboxes normally show:
Warning: Invalid argument supplied for foreach() in /serverpath/wp-content/themes/themename/archive-listings.php on line 27
(where line 27 is my foreach loop)
Here’s my code:
<?php
$field = get_field_object('community');
$values = isset($_GET['community']) ? explode(',', $_GET['community']) : array();
$field2 = get_field_object('listing_status');
$values2 = isset($_GET['listing_status']) ? explode(',', $_GET['listing_status']) : array();
?>
<ul class="communities">
<?php foreach( $field['choices'] as $choice_value => $choice_label ): ?>
<li>
<input type="checkbox" value="<?php echo $choice_value; ?>" <?php if( in_array($choice_value, $values) ): ?>checked="checked"<?php endif; ?> /> <?php echo $choice_label; ?></li>
</li>
<?php endforeach; ?>
</ul>
<ul class="status">
<?php foreach( $field2['choices'] as $choice_value2 => $choice_label2 ): ?>
<li>
<input type="checkbox" value="<?php echo $choice_value2; ?>" <?php if( in_array($choice_value2, $values2) ): ?>checked="checked"<?php endif; ?> /> <?php echo $choice_label2; ?></li>
</li>
<?php endforeach; ?>
</ul>
How can I fix this?
Also, how can I do the URL replacement when someone clicks on the checkboxes? I have the below setup which works for one custom field parameter at a time but not both:
<script type="text/javascript">
//Listing URL Query Change
(function($) {
$('.communities').on('change', 'input[type="checkbox"]', function(){
// vars
var $ul = $(this).closest('ul'),
vals = [];
$ul.find('input:checked').each(function(){
vals.push( $(this).val() );
});
vals = vals.join(",");
window.location.replace('<?php echo home_url('listings'); ?>?community=' + vals);
});
$('.status').on('change', 'input[type="checkbox"]', function(){
// vars
var $ul = $(this).closest('ul'),
vals = [];
$ul.find('input:checked').each(function(){
vals.push( $(this).val() );
});
vals = vals.join(",");
window.location.replace('<?php echo home_url('listings'); ?>?listing-status=' + vals);
});
})(jQuery);
</script>
Thanks in advance for any help 🙂
I’m am noticing this also within a Flexible content field. It looks like select WYSIWYG fields’ content are not displaying when I try to output them to the page:
<?php echo get_sub_field('section_content'); ?>
Nevermind I figured it out,
I looked at the name of the field in the front end and added it to my code like this and my Custom Post Types now have their custom title:
'post_title' => $_POST["fields"]['field_52b2ffdfdcad1']
James
Hi Elliot,
That did it. I have two rows in my wp_options table (as I was testing with two fields). I deleted both rows and my form now submits correctly!
Also, I’m not sure if I should start a new topic or not, but is there a way to change the post_title to pull the value from a field that has been setup? I tried this, but it did not work:
'post_title' => $_POST["fields"]['page_title']
Where page_title is the name of the field I setup in ACF.
Thanks very much,
James
Thanks for your reply @elliot
It is happening in the backend, the file upload field is within a repeater, which is part of a flexible content field.
I have not got any WordPress upload issues with the other forms like the media upload button on a page.
Very strange. If the error persists, I’ll get the error from my PHP log and report it here.
I normally have no issues uploading on this server with ACF (I use it all the time). The only thing new that I’ve done is update to the newest version.
That’s interesting, however mine is even occurring with my default admin user.
This is occurring on a fresh install of WordPress, with no plugins installed yet (other than the ACF plugins of course).
I tried doing it with another admin user and that didn’t work either.
I turned of debug mode and fixed two depreciated functions I was using, but that didn’t seem to be related.
I checked my HTTP Error log on my server and found the following error:
[Wed Sep 18 06:19:13 2013] [error] [client] Premature end of
script headers: async-upload.php, referer:
/wp-admin/post.php?post=12&action=edit
But that doesn’t help us much, so as per my host’s recommendation, I’ve enabled a PHP error log and am now trying to reproduce the error (which doesn’t seem to want to reproduce at the moment, of course) so maybe I can get some more details on what is going on.
I did notice two errors in my new PHP log which related to ACF, but I can’t confirm if they are related to my HTTP Upload Error yet:
[18-Sep-2013 20:55:55 UTC] PHP Fatal error: Maximum execution time of 300 seconds exceeded in /wp-content/plugins/advanced-custom-fields/acf.php on line 311
and
[18-Sep-2013 20:16:16 UTC] PHP Fatal error: Maximum execution time of 300 seconds exceeded in /wp-content/plugins/advanced-custom-fields/acf.php on line 283
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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.