I was missing the _acfnonce. It might be worth updating the development documents to specify this as it’s not anywhere on the net that I could find easily!
for reference my code was
<?php
$nonce = wp_create_nonce( 'acf_form' );
$args = wp_parse_args( $args, array(
'id' => 'acf-form',
'post_id' => false,
'new_post' => false,
'field_groups' => false,
'fields' => false,
'post_title' => false,
'post_content' => false,
'form' => true,
'form_attributes' => array(),
'return' => add_query_arg( 'updated', 'true', $url ),
'html_before_fields' => '',
'html_after_fields' => '',
'submit_value' => __("Update", 'acf'),
'updated_message' => __("Post updated", 'acf'),
'label_placement' => 'top',
'instruction_placement' => 'label',
'field_el' => 'div'
));
?>
<form id="post" class="acf-form" action="" method="post" autocomplete="off" enctype="">
<input type="hidden" name="_acfnonce" value="<?php echo $nonce; ?>">
<? acf_hidden_input(array( 'name' => '_acf_form', 'value' => base64_encode(json_encode('$args')) )); ?>
...rest of form ...
I don’t know if there is a shorter way, but this works for now!
This is a feature I’d also like to request.
ok. i know the problem. could you try if this works for you
<!-- model specifications -->
<?php if( have_rows('model_specifications') ):
$firstrow = "1";
while( have_rows('model_specifications') ): the_row();
$ms_model = get_sub_field('model');
$ms_gpm = get_sub_field('gpm');
$ms_input = get_sub_field('input');
$ms_psi = get_sub_field('psi');
$ms_drive = get_sub_field('drive');
$ms_rpm = get_sub_field('rpm');
$ms_drive_rpm = get_sub_field('drive_rpm');
$ms_hp = get_sub_field('hp');
$ms_volt = get_sub_field('volt');
$ms_hp_volt = get_sub_field('hp_volt');
$ms_power = get_sub_field('power');
$ms_engine = get_sub_field('engine');
$ms_burner = get_sub_field('burner');
$ms_electric_start = get_sub_field('electric_start');
$ms_size = get_sub_field('size');
if($firstrow == "1") :
echo '<div id="model-specifications">';
echo '<table class="rwd-table">';
echo '<thead>';
echo '<tr>';
if ($ms_model) {echo '<th>Model</th>';}
if ($ms_gpm) {echo '<th>GPM</th>';}
if ($ms_input) {echo '<th>Input</th>';}
if ($ms_psi) {echo '<th>PSI</th>';}
if ($ms_drive) {echo '<th>Drive</th>';}
if ($ms_rpm) {echo '<th>RPM</th>';}
if ($ms_drive_rpm) {echo '<th>Drive RPM</th>';}
if ($ms_hp) {echo '<th>HP</th>';}
if ($ms_volt) {echo '<th>Volt</th>';}
if ($ms_hp_volt) {echo '<th>HP / Volt</th>';}
if ($ms_power) {echo '<th>Power</th>';}
if ($ms_engine) {echo '<th>Engine</th>';}
if ($ms_burner) {echo '<th>Burner</th>';}
if ($ms_electric_start) {echo '<th>Electric Start</th>';}
if ($ms_size) {echo '<th>Size</th>';}
echo '<th>Quote</th>';
echo '</tr>';
echo '</thead>';
echo '<tbody>';
endif;
$firstrow++;
echo '<tr>';
if ($ms_model) {echo '<td data-th="Model">'.$ms_model.'</td>';}
if ($ms_gpm) {echo '<td data-th="GPM">'.$ms_gpm.'</td>';}
if ($ms_input) {echo '<td data-th="Input">'.$ms_input.'</td>';}
if ($ms_psi) {echo '<td data-th="PSI">'.$ms_psi.'</td>';}
if ($ms_drive) {echo '<td data-th="Drive">'.$ms_drive.'</td>';}
if ($ms_rpm) {echo '<td data-th="RPM">'.$ms_rpm.'</td>';}
if ($ms_drive_rpm) {echo '<td data-th="Drive RPM">'.$ms_drive_rpm.'</td>';}
if ($ms_hp) {echo '<td data-th="HP">'.$ms_hp.'</td>';}
if ($ms_volt) {echo '<td data-th="Volt">'.$ms_volt.'</td>';}
if ($ms_hp_volt) {echo '<td data-th="HP / Volt">'.$ms_hp_volt.'</td>';}
if ($ms_power) {echo '<td data-th="Power">'.$ms_power.'</td>';}
if ($ms_engine) {echo '<td data-th="Engine">'.$ms_engine.'</td>';}
if ($ms_burner) {echo '<td data-th="Burner">'.$ms_burner.'</td>';}
if ($ms_electric_start) {echo '<td data-th="Electric Start">'.$ms_electric_start.'</td>';}
if ($ms_size) {echo '<td data-th="Size">'.$ms_size.'</td>';}
echo '<td data-th="Quote"><a href="/request-a-quote/?">Request a Quote</a></td>';
echo '</tr>';
endwhile;
echo '</tbody>';
echo '</table>';
echo '</div>';
?>
<?php endif; ?>
<!-- end model specifications -->
I have forked this repo and created a pull request for it. I’ve also contacted the author of this plugin.
I’m not that much of a PHP hero, but willing to spend some time to make this working. If either you or the original author can point me in the right direction, then hopefully people can use this again 🙂
This solves it.
http://www.advancedcustomfields.com/resources/acf-fields-relationship-query/
function my_relationship_query( $args, $field, $post ) {
// get posts for current logged in user
$args['author'] = get_current_user_id();
return $args;
}
add_filter('acf/fields/relationship/query/key=<field_key>', 'my_relationship_query', 10, 3);
I had my repeater running through another file, to get it to work I had to run it through the repeater again on the page I wanted the repeater to run in the file. Like this.
<?php if( have_rows('layout_section') ) { } else { } ?>
<?php wp_reset_query(); ?>
<?php get_template_part( 'layout-strips' ); ?>
No idea why this works but it does.
I would try emailing support as they have been really helpful to me.
Hi @tsmulugeta
Wow, I do not remember when I last used this function as I resorted to using the repeater fields extension. However, If my memory serves me right, my goal was to create a new post of a custom type and attach an image to it, and not the page that the front end form was generated on. To save the post, this is the code I “think” i placed in my functions.php file to save the custom post. If it helps, I was using this code for a simple online classifieds website to post ads:
function my_pre_save_post( $post_id )
{
// check if this is to be a new post
if( $post_id != 'new' )
{
return $post_id
}
// Create a new post
$ad = array(
'post_status' => 'published',
'post_content' => $_POST['description'],
'post_title' => $_POST['title'],
'post_type' => 'ads',
'post_author' => get_current_user_id(),
'comment_status' => 'closed', // if you prefer
'ping_status' => 'closed' // if you prefer
);
// insert the post
$post_id = wp_insert_post( $ad );
// set post (ad) terms
$category = intval( $data['category'] );
wp_set_object_terms( $post_id, array( $category ), 'ad-categories' );
wp_set_object_terms( $post_id, explode( ',', $data['tags'] ), 'ad-tags' );
// now this is where I retrieved the submitted image id
// from the form and attached it to the post
attachment_change_parent( $_POST['ad_image'], $post_id );
// return the new ID
return $post_id;
}
add_filter('acf/pre_save_post' , 'my_pre_save_post', 10, 1 );
// this function changes the parent of the attached image to the post
function attachment_change_parent($aid,$pid) {
global $wpdb;
$query = "UPDATE {$wpdb->prefix}posts
SET post_parent = $pid
WHERE ID = $aid";
return ($wpdb->query( $wpdb->prepare($query) )) ? true : false;
}
Basically, from the front end form, as soon as the image is uploaded (via javascript), it’s attached to the page on which the form resides, but what I did was during the “pre_save_post” hook, I get the submitted image id and change the id of the page it’s attached to to the post as soon as the post is created.
I hope this makes some sense and I sure hope it points you into finding a solution. I am here in case you need any more explanation with anything.
Thank you!
Ok… so I’ve tried it a few different way and am still missing it.
I checked my database and here is what my Hello World! post has in it for directory_news:
a:2:{i:0;s:2:"65";i:1;s:2:"23";}
So that’s showing me that profiles with the ID of 65 and 23 are linked to that post…
So then I thought I followed the example that you provided with this
$mine = get_posts(array(
'post_type' => 'post', // "post" because I'm calling regular blog posts?
'meta_query' => array(
array(
'key' => 'directory_news', // slug of custom field
'value' => '65', // hardcoded it just to test
'compare' => 'LIKE'
)
)
));
You’ll see that instead of using the ID I just hardcoded 65 in there just to see if I could only pull that one…
However this still spits out ALL posts.. and not the ones that have David in the relationship field.
As mentioned above you’ll need to do a reverse query.
Understanding the query
The above “get_posts” query finds all posts that are of the type “doctor”. It then finds the “location” custom field for each doctor and runs a LIKE query on the value.
The relationship field saves it’s data as a serialized array. If you are not familiar with this format, please look up the stored value in your database. It will look something like this:
a:2:{i:0;s:2:”35″;i:1;s:2:”33″;}
This serialized array has 2 values, the first is “35”, the second is “33”. As this is the “location” field for a “doctor”, these 2 numbers must be the ID’s of location post types.In our query, the LIKE value was ‘”‘ . get_the_ID() . ‘”‘, if the location ID was 35 then the LIKE value would become ‘”35″‘. In this case, the query would match against the doctor’s location value (the serialized array shown above) and the doctor will be returned.
If i understand correctly you need to do a reverse query: http://www.advancedcustomfields.com/resources/querying-relationship-fields/
it is also possible that shortcode unfortunately get formatted, than this should work:
<?php $shortcode = get_field('request_demo_shortcode', false, false);
do_shortcode($shortcode);?>
(3 parameter should be false)
I solved the problem. The solution:
Create a file ‘nextli.js’:
jQuery(document).ready(function(){
jQuery('.acf-taxonomy-field .acf-checkbox-list li:nth-child(3)').nextAll().css('display','none');
});
In function.php
function admin_inline_js(){
wp_enqueue_script('custom_admin_script', get_stylesheet_directory_uri().'/lib/nextall.js', array('jquery'));
}
add_action( 'admin_enqueue_scripts', 'admin_inline_js' );
this should work:
<?php $images = get_field('gallery');
if( $images ): ?>
<div class="gallery">
<ul>
<?php foreach( $images as $image ):
$content = '<li>';
$content .= '<a class="gallery_image" href="'. $image['url'] .'">';
$content .= '<img src="'. $image['sizes']['thumbnail'] .'" alt="'. $image['alt'] .'" />';
$content .= '</a>';
$content .= '</li>';
if ( function_exists('slb_activate') ){
$content = slb_activate($content);
}
echo $content;?>
<?php endforeach; ?>
</ul>
</div>
<?php endif; ?>
you need to add a js libary that provides the popup functionality.
with plugins Simple Lightbox you can use:
<?php $images = get_field('gallery');
if( $images ): ?>
<div class="gallery">
<ul>
<?php foreach( $images as $image ):
$content = '<li>';
$content .= '<a class="gallery_image" href="'. $image['url'] .'">';
$content .= '<img src="'. $image['sizes']['thumbnail'] .'" alt="'. $image['alt'] .'" />';
$content .= '</a>';
$content .= '</li>';
if ( function_exists('slb_activate') ){
$content = slb_activate($content);
}
echo $content;?>
<?php endforeach; ?>
</ul>
</div>
<?php endif; ?>
it is possible, just check if value is there
but for that you need to re-arrange / re-write your code.
here a part of it. just add the other rows the same way.
<?php if( have_rows('model_specifications') ):
while( have_rows('model_specifications') ): the_row();
$ms_model = get_sub_field('model');
$ms_gpm = get_sub_field('gpm');
endwhile;
echo '<div id="model-specifications">';
echo '<dl>';
if ($ms_model) {echo '<dt class="model">Model</dt>';}
if ($ms_gpm) {echo '<dt class="gpm">GPM</dt>';}
if ($ms_model) {echo '<dd class="model">'.$ms_model.'</dd>';}
if ($ms_gpm) {echo '<dd class="gpm">'.$ms_gpm.'</dd>';}
echo '</dl>';
?>
@JiveDig Is it possible to use the same acf/pre_save_post filter for 2 acf_form that are on 2 different page templates? I can create a new post but I can’t Edit an existing post. You can see my question here: http://support.advancedcustomfields.com/forums/topic/acf_form-not-creating-new-post/
The Edit Post page loads the ACF Fields but the update only updates the ACF Fields and does not edit the Post Title and Post Content.
Any ideas how to fix this?
Thanks again, you solution was good, I just had to tweak script so fitvids would recognize and fire,
jQuery(function( $ ) {
$('.load-video-button').click(function() {
$('.embed-container').addClass('loaded-video').html($(this).data('video-embed'));
$( '.loaded-video' ).fitVids();
});
});
This should do the trick:
<?php
$args = array(
'post_type' => $type,
'post_status' => 'publish',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'banner',
'compare' => '=',
'value' => 1
)
),
'meta_key' => 'start_time'
'orderby' => 'meta_value_num',
'order' => 'ASC'
);
?>
See the documentation here: http://www.advancedcustomfields.com/resources/how-to-query-posts-filtered-by-custom-field-values/
You’ll want to query the database using the custom field’s name as a meta key and comparing the values against your current custom field’s value. Try this:
<?php
$current_event_date = get_field('date');
$next_event_query = new WP_Query(array(
'post_type' => 'event',
'post_status' => 'publish',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'date',
'compare' => '>',
'value' => $current_event_date,
'type' => 'DATE'
)
),
'orderby' => 'meta_value_num',
'order' => 'ASC'
));
if ($next_event_query->have_posts()) while ($next_event_query->have_posts()): $next_event_query->the_post();
// Print information about next event.
endwhile;
?>
I expect something like this would do the trick:
<button class="load-video-button" data-video-embed="<?php echo htmlspecialchars(get_field('oembed_video_url')) ?>">Load Video</button>
<div class="video-container"></div>
<script>
$(function() {
$('.load-video-button').click(function() {
$('.video-container').html($(this).data('video-embed'));
});
});
</script>
(I assume you’re using jQuery here.)
I resolved my problem by changing the fields name from ['fields']
to ['acf']
The problem I am having now is editing a post. Here is the code in the functions.php file:
add_filter('acf/pre_save_post' , 'my_pre_save_post' );
function my_pre_save_post( $post_id ) {
if( $post_id != 'new_post' ) {
return $post_id;
}
$post = array(
'post_status' => 'publish',
'post_title' => $_POST['acf']['field_54e6020d07a17'],
'post_content' => $_POST['acf']['field_54db8cd72d3c2'],
'post_type' => 'post' ,
);
$post_id = wp_insert_post( $post );
$_POST['return'] = add_query_arg( array('post_id' => $post_id), $_POST['return'] );
return $post_id;
}
Here is the acf_form on the Add Post page template:
acf_form( array(
'post_id' => 'new_post',
'fields' => array( 'field_54e6020d07a17', 'field_54db8cd72d3c2' ),
'updated_message' => 'Post Created'
));
Here is the acf_form on the Edit Post page template:
acf_form( array(
'post_id' => $_GET['post_id'],
'fields' => array( 'field_54e6020d07a17', 'field_54db8cd72d3c2' ),
'submit_value' => 'Update the post!'
));
Can anyone help me figure this one out? Thanks
is there any progress?
what tables/fields in the DB are beeing used to store field groups etc?
maybe its not too complicated to write some small sql update file.
It makes sense logically, but I don’t get the “why” of it. Let me explain / link my site, to help clear things up.
The site is on night life / entertainment, and covers every restaurant, bar, hotel, etc, in the local area. In theory, each of those would be a custom post type, if I needed it (or knew why it was advantageous to use them). I’m also not sure how my sub-types would handle (Dining has – restaurants, cafes, delis, street vendors, etc).
The current custom ACF groups are as follows:
“Location Info”, which contains a “location_category” field (a check box, used to determine if it’s a bar, restaurant, music venue, or a mix), and a “city” field, used to mark which city the venue belongs to (the site covers various cities in the area).
“Basic Info”, which contains “description” and “images” fields, which is pretty self-explanatory.
“Dining_info”, which is tricky. This one, simply determines if the venue has dine in, take out, or delivery, and isn’t specific to the “Dining” section of my website, but instead applies to all templates that occasionally also have food (Bars, Dining, for now, but this is perhaps the one “messy” area at the moment).
I then have templates for “Main” categories (Music, Dining, Loding, etc), and “Secondary” categories (for music, artists and venues, dining has the sub sections mentioned earlier, etc). The templates aren’t quite done yet (design), but code-wise, dynamically show everything I need based on the ACF information, which works great. I can show events on a calendar dynamically using the page string, which is identical to the “location_category” field. This allows me to query Maps with all venues dynamically, events, and more.
IMO, I’ve done a very clever job of making this all work, but again, it needs to be future-proof, and I have a nagging in the back of my mind that says custom post types are MEANT to do exactly what I’m doing. That being said, my setup seems to make them obsolete in my situation (based on what I know), and I’m a bit lost. I also really like my url structure (site.com/dining, site.com/dining/restaurants, site.com/dining/restaurants/venue-name), and I’ve heard custom post types don’t play well with custom permalinks. Anyway, sorry for the essay of a post, but I figured I had to explain it as best I could to make proper sense here.
Ignore the incomplete main and sub category pages – anightinburlington.com
I’m not sure I understand your question fully; it’s hard to tell without knowing your site’s needs. But: ACF’s ability to conditionally show fields on different post templates, taxonomies, etc. does make it possible to avoid creating different post types when the default post types and conditionally-shown ACF fields will suffice.
Custom post types are certainly not obviated by this plugin, however. For example, a website for a running club would have custom post types for race
and course
in addition to the default post
and page
post types. An ACF object relationship field on the Race edit page could tie it to a specific course.
Does that make sense?
Hi, I went to your website and couldn’t find any email or contact form to sign up for the Beta testing of the plugin you describe: http://www.theportlandcompany.com/contact-and-support/support-request-form/. Please advise how I can also become a Beta tester. Thanks!
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.