Hello,
thank you for your reply.
Hm i have added it before (forgot to have it on the code i gave to the topic) but nothing happened. I tested again with the code below but nothing works.
<?php
acf_form_head();
get_header();
?>
<div class="container">
<?php
$settings = array(
'id' => 'photos_form',
'post_id' => 'new_post',
'new_post' => ['post_type' => 'photos', 'post_status' => 'draft'],
'field_groups' => array("group_6025117548b3f"),
'fields' => false,
'submit_value' => __("Submit", 'acf'),
'label_placement' => 'top',
'html_submit_button' => '<input type="submit" id="submitbtn" class="acf-button button primary_btn" value="%s" />',
'html_submit_spinner' => '<span class="acf-spinner"></span>',
'updated_message' => '',
'html_after_fields' => '',
);
acf_form($settings);?>
</div>
<?php get_footer(); ?>
Is there any mistake in that code? Didn’t understand something in the correct way?
Thank you again.
George
You need to add the post_id and new_post param to the $settings array.
Plz replace ‘your-cpt’ with the name of your custom post type.
$settings = array(
'id' => 'photos_form',
'post_id' => 'new_post',
'new_post' => [
'post_type' => 'your-cpt',
'post_status' => 'draft',
],
'field_groups' => array("group_6025117548b3f"),
'fields' => false,
'submit_value' => __("Submit", 'acf'),
'label_placement' => 'top',
'html_submit_button' => '<input type="submit" id="submitbtn" class="acf-button button primary_btn" value="%s" />',
'html_submit_spinner' => '<span class="acf-spinner"></span>',
'updated_message' => '',
'html_after_fields' => '',
);
Just answering my own question here… it was hitting a ModSec rule on the server that is in place to protect from a XSS vulnerability in a plugin called Modern Events Calendar Lite plugin 4.2.1 for WordPress – which I don’t use so server guys have disabled rule and it works now!
Hope this helps someone else sometime!
The existing frontend form is for the members to submit a new post in website. As the post is about is discount or coupon details, I wanna use ACF to add few more fields to the existing form, which are deals start date, end date and coupon code.
Do you mean the existing form may not allow ACF to function well?
Didn’t know if this information useful, I tried to add a field of tag by ACF in the function.php, and it works. Does it mean ACF is able to function in the form?
$submission_data = array(
'id' => $post_id,
'title' => $title,
'source' => $source,
'ref_link' => $ref_link,
'description' => $description,
'category_id' => $categories,
'tags' => $_POST['acf']['field_6023f52f7b6f0'],
'list_voting' => $list_voting,
'list_submission' => $list_submission,
'author' => $author_id,
'status' => $status,
);
do_action( 'snax_handle_'. $format .'_submission', $submission_data, $request );
i have posts filter after that tab code not before that
but i added wp_reset_postdata();
but stilllthings dont work
see the full page code
<?php get_header();?>
<main id="main">
<!--Breadcrumbs Section-->
<section class="breadcrumbs">
<div class="container">
<div class="d-flex justify-content-between align-items-center">
<h2><?php the_title();?></h2>
</div>
</div><!--container-->
</section> <!--Breadcrumbs Section-->
<!--departments-inner-page-->
<section id="departments" class="departments">
<div class="container">
<div class="section-title">
<h2>Departments</h2>
<p>Magnam dolores commodi suscipit. Necessitatibus eius consequatur ex aliquid fuga eum quidem. Sit sint consectetur velit. Quisquam quos quisquam cupiditate. Et nemo qui impedit suscipit alias ea. Quia fugiat sit in iste officiis commodi quidem hic quas.</p>
</div>
<div class="row">
<div class="col-lg-3">
<ul class="nav nav-tabs flex-column">
<li class="nav-item">
<a class="nav-link active show" data-toggle="tab" href="#tab-1">Cardiology</a>
</li>
<li class="nav-item">
<a class="nav-link" data-toggle="tab" href="#tab-2">Virologist</a>
</li>
<li class="nav-item">
<a class="nav-link" data-toggle="tab" href="#tab-3">Eye specialist</a>
</li>
<li class="nav-item">
<a class="nav-link" data-toggle="tab" href="#tab-4">Pediatrics</a>
</li>
<li class="nav-item">
<a class="nav-link" data-toggle="tab" href="#tab-5">Hepatology</a>
</li>
<li class="nav-item">
<a class="nav-link" data-toggle="tab" href="#tab-6">Neurology</a>
</li>
</ul>
</div><!--col-lg-3-->
<?php
$Department_tab = get_field('department_tab');
$Cardiology = $Department_tab['cardiology'];
$Virology = $Department_tab['virologist'];
$Eye_Specialist = $Department_tab['eye_specialist'];
$Pediatrics = $Department_tab['pediatrics'];
$Hepatology = $Department_tab['hepatology'];
$Neurology = $Department_tab['neurology'];
?>
<div class="col-lg-9 mt-4 mt-lg-0">
<div class="tab-content">
<div class="tab-pane active show" id="tab-1">
<div class="row">
<div class="col-lg-8 details order-2 order-lg-l">
<h3> <?php echo $Cardiology['header'];?></h3>
<p class="font-italic">
<?php var_dump($Cardiology); ?>
<?php echo $Cardiology['sub_header'];?></p>
<p><?php echo $Cardiology['body_text'];?>
</p>
</div><!--col-lg-8-->
<div class="col-lg-4">
<img src="<?php echo $Cardiology['image'];?>" alt="department" class="img-fluid">
</div><!--col-lg-4-->
</div><!--row-->
</div><!--tab-pane-->
<div class="tab-pane fade" id="tab-2">
<div class="row">
<div class="col-lg-8 details order-2 order-lg-1">
<h3><?php echo $Virology['header'];?></h3>
<p class="font-italic">
<?php var_dump($Virology); ?>
<?php echo $Virology['sub_header'];?></p>
<p><?php echo $Virology['body_text'];?>
</p>
</div><!--col-lg-8-->
<div class="col-lg-4">
<img src="<?php echo $Virology['image'];?>" alt="department" class="img-fluid">
</div><!--col-lg-4-->
</div><!--row-->
</div><!--tab-pane-->
<div class="tab-pane fade" id="tab-3">
<div class="row">
<div class="col-lg-8 details order-2 order-lg-1">
<h3><?php echo $Eye_Specialist['header'];?></h3>
<p class="font-italic">
<?php echo $Eye_Specialist['sub_header'];?></p>
<p><?php echo $Eye_Specialist['body_text'];?>
</p>
</div><!--col-lg-8-->
<div class="col-lg-4">
<img src="<?php echo $Eye_Specialist['image'];?>" alt="department" class="img-fluid">
</div><!--col-lg-4-->
</div><!--row-->
</div><!--tab-pane-->
<div class="tab-pane fade" id="tab-4">
<div class="row">
<div class="col-lg-8 details order-2 order-lg-1">
<h3><?php echo $Pediatrics['header'];?></h3>
<p class="font-italic">
<?php echo $Pediatrics['sub_header'];?></p>
<p><?php echo $Pediatrics['body_text'];?>
</p>
</div><!--col-lg-8-->
<div class="col-lg-4">
<img src="<?php echo $Pediatrics['image'];?>" alt="department" class="img-fluid">
</div><!--col-lg-4-->
</div><!--row-->
</div><!--tab-pane-->
<div class="tab-pane fade" id="tab-5">
<div class="row">
<div class="col-lg-8 details order-2 order-lg-1">
<h3><?php echo $Hepatology['header'];?></h3>
<p class="font-italic">
Qui laudantium consequatur laborum sit qui ad sapiente dila parde sonata raqer a videna mareta paulona marka</p>
<p>Et nobis maiores eius. Voluptatibus ut enim blanditiis atque harum sint. Laborum eos ipsum ipsa odit magni. Incidunt hic ut molestiae aut qui. Est repellat minima eveniet eius et quis magni nihil. Consequatur dolorem quaerat quos qui similique accusamus nostrum rem vero
</p>
</div><!--col-lg-8-->
<div class="col-lg-4">
<img src="assets/image/departments-5.jpg" alt="department" class="img-fluid">
</div><!--col-lg-4-->
</div><!--row-->
</div><!--tab-pane-->
<div class="tab-pane fade" id="tab-6">
<div class="row">
<div class="col-lg-8 details order-2 order-lg-1">
<h3><?php echo $Neurology['header'];?></h3>
<p class="font-italic">
Qui laudantium consequatur laborum sit qui ad sapiente dila parde sonata raqer a videna mareta paulona marka</p>
<p>Et nobis maiores eius. Voluptatibus ut enim blanditiis atque harum sint. Laborum eos ipsum ipsa odit magni. Incidunt hic ut molestiae aut qui. Est repellat minima eveniet eius et quis magni nihil. Consequatur dolorem quaerat quos qui similique accusamus nostrum rem vero
</p>
</div><!--col-lg-8-->
<div class="col-lg-4">
<img src="assets/image/departments-5.jpg" alt="department" class="img-fluid">
</div><!--col-lg-4-->
</div><!--row-->
</div><!--tab-pane-->
</div><!--tab-content-->
</div><!--col-lg-9-->
</div><!--row-->
</div><!--container-->
</section>
<!--doctors-inner-page-->
<section id="doctors" class="doctors">
<div class="container">
<div class="section-title">
<h2>Doctors</h2>
<p>Magnam dolores commodi suscipit. Necessitatibus eius consequatur ex aliquid fuga eum quidem. Sit sint consectetur velit. Quisquam quos quisquam cupiditate. Et nemo qui impedit suscipit alias ea. Quia fugiat sit in iste officiis commodi quidem hic quas.</p>
</div><!--section-title-->
<div class="row">
<?php
$args = array(
'post_type' => 'doctor',
'posts_per_page' => 4
);
$doctors = new WP_Query($args);
while($doctors->have_posts()){
$doctors->the_post();
?>
<div class="col-lg-6 mt-4 mt-lg-0">
<div class="member d-flex align-items-start">
<div class="pic">
<img src="<?php the_post_thumbnail_url('post_image'); ?>" class="img-fluid" alt="doctor">
</div><!--pic-->
<div class="member-info">
<h4><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h4>
<span><?php the_field('doctor_subheading'); ?></span>
<p><?php echo wp_trim_words(get_the_excerpt(),10);?></p>
<div class="social">
<a href=""><i class="ri-twitter-fill"></i></a>
<a href=""><i class="ri-facebook-fill"></i></a>
<a href=""><i class="ri-instagram-fill"></i></a>
<a href=""><i class="ri-linkedin-box-fill"></i></a>
</div><!--social-->
</div><!--member-info-->
</div><!--member-->
</div><!--col-lg-6-->
<?php } wp_reset_postdata(); ?>
</div><!--row-->
</div><!--container-->
</section><!--doctors-->
</main>
<?php get_footer();?>
@laju and @hube2, thank you both so much for the help!
if( !$query->is_main_query() ) return;
Did the trick for me! Adding in the or is_admin() threw an error, so I will be leaving that portion out.
Thanks again to both of you! I was near keyboard smashing levels of hung up on this one. You both rock!
Rather than build code in the child theme that adds a load point I have this code in the parent theme to reduce code in the child theme.
In the funcitons.php file of the parent theme I have
add_filter('acf/settings/load_json', 'parent_theme_acf_json_load_point);
function parent_theme_acf_json_load_point($paths) {
if (!is_child_theme()) {
return $paths;
}
array_unshift($paths, get_template_directory().'/acf-json');
return $paths;
}
The reason I use array_unshift() is to ensure that the parent json files will be first and override the child theme because I do not allow changes to these field groups in the child theme. If more fields are needed for a specific child theme then these field must be created by adding additional field groups to the child theme.
This could be done in the child theme, but like I said, it’s added code in every child theme. To me this is like the way most people build themes where when using a child theme the developer must do extra work to enqueue the parent theme’s stylesheet when they can simply check in the parent theme to see if a child theme is being used and automatically add both the parent theme’s stylesheet and the child theme’s stylesheet in the correct order with the correct dependencies to begin with. Little QOL improvements to keep me sane and reduce my workload.
@hube2 – I guessed it would work automatically, but I re-read your post and saw this:
Part of the parent theme code is checking to see if a child theme is being loaded and if it is then it adds a load point to load the field groups from the parent theme.
Could you elaborate a bit on this, how would I set this up?
I do not sync field groups to all sites in a mutlisite installation. There is not really any need to do this.
This is what I do.
I create a theme with an acf-json folder and I activate this theme on only one site. This is my dev site. This is the only place where changes to a field group are made.
Any other site in the network that uses this theme is set up using a child theme. Part of the parent theme code is checking to see if a child theme is being loaded and if it is then it adds a load point to load the field groups from the parent theme. No syncing to these sites is needed.
I’m trying this solution, but even with all plugins disabled I keep getting endless loading (white screen) when I activate the child theme and visit the front-end.
I’ve tried manual child theme and a plugin “child theme creator” but neither work. Any ideas? I’m nog 100% knowledgable on multisites.
https://stackoverflow.com/questions/52254794/get-and-use-the-product-type-in-woocommerce
$field['value'] = $product->get_type();
You need to have bidirectional relationship fields (there are plugins that will do this) or use a reverse relationship query.
The code involve would be quite complex. You would need to create some type of a recursive function to go both up and down the relationships and build arrays with nested arrays holding all of the data and/or display that data. You’d probably also need to use an output buffer, or potentially several output buffers, to build the content.
I can’t give you any example code because it would take many hours to come up with something and I don’t know of any examples of doing something like this.
The best place to get your questions answered about using ACF with Divi is either the Divi documentation or a Divi community. I can help with specific questions about ACF if you know what you need to do in Divi.
Thank you very much John! Finally got it 🙂
My mistake was to assume have_rows() is equivalent to “is not empty” or “exists” so I didn’t appreciate that placing my code inside have_rows() would not work.
Got it & works perfectly now, thank you!
To set the parent post from the front end you need a relationship field that is only available on the front end. Set this up to start. Add a post object field that only allows one selection.
Create the following filters for your field in functions.php
// only show field on front of site
// https://www.advancedcustomfields.com/resources/acf-prepare_field/
add_filter('acf/prepare_field/name=YOUR FIELD NAME HERE', 'field_only_on_front');
function field_only_on_front($field) {
if (is_admin()) {
return false;
}
return $field;
}
// alter the acf query of the post object field to only show top level posts
https://www.advancedcustomfields.com/resources/acf-fields-post_object-query/
add_filter('acf/fields/post_object/query/name=YOUR FIELD NAME HERE', 'only_top_level_posts', 20, 3);
function only_top_level_posts($args, $field, $post_id) {
$args['post_parent'] = 0;
return $args;
}
After you do this set up your taxonomy field to return term objects
Once all that is done then then create an acf/save_post filter in functions.php
add_action('acf/save_post', 'update_and_save_journal_post');
function update_and_save_journal_post($post_id) {
// make sure to only do this on the right post type
$post_type = get_post_type($post_id);
if ($post_type != 'YOUR POST TYPE HERE') {
return;
}
// get the post
$post = get_post($post_id);
// get the parent, unformatted, ID only
$parent = get_field('YOUR POST OBJECT FIELD', $post_id, false);
if ($parent ) {
$post->post_parent = $parent;
}
// get the taxonomy field
$terms = get_field('YOUR TAXONOMY FIELD;, $post_id);
if (!empty($terms) && !is_wp_error($terms)) {
$term = terms[0];
$post->post_title = $term->name.$post->post_title;
}
// remove this action so that an infinite loop is not created
remove_filter('acf/save_post', 'update_and_save_journal_post');
// update the post
wp_update_post($post);
// re-add this filter
add_action('acf/save_post', 'update_and_save_journal_post');
}
Please note that none of the above code has been tested for syntax errors.
object is coming back as an array, which we can’t seem to get the name of for some reason. I have tried
$term = wp_get_object_terms($post->ID, 'myterm');
$first_term = $term[0];
$term_name = $first_term->name;
$new_title = $post_id.'- '.$term_name;
to to avail. my php is quite limited so I’m sure I am not going about this the right way. post ID shows, but I either get an array, or nothing. The case above results in nothing, but if I set new_title to $term it will say array
I didn’t transfer any data from my local site to my current site. I uploaded the blank theme.
Did I understand your question correctly?
Hi John! Thank you for your response! It was the function “acf_get_all_field_keys”. It’s a custom function which gets the field-keys (field_123456) from the database. Here is the code:
function acf_get_all_field_key( $meta_key ) {
global $wpdb;
$acf_fields = $wpdb->get_results( $wpdb->prepare( "
SELECT 'meta_value'
FROM '$wpdb->postmeta'
WHERE 'post_id' = 3077
AND 'meta_key'
LIKE %s",
$meta_key
)
);
// get all fields with that name.
switch ( count( $acf_fields ) ) {
case 0: // no such field
return false;
case 1: // just one result.
return $acf_fields[0]->meta_value;
default:
return array ($acf_fields);
}
}
I changed the post-id for this function to a fix ID of a published event, so it’s always returning values.
I founded this function here: https://gist.github.com/mcguffin/81509c36a4a28d9c682e
Thank you for your help! This topic is solved.
Before I answer the entire question. What is the return value of the taxonomy field set to? Term ID or Object?
I think you want AND relationships on your rules instead of OR.
For example, looking it the 3rd rule. Post Type is not equal to Review
A post of type FAQ is not equal so this is true and the field group will be shown on the FAQ post type even with the rule above it.
is not equal to
is difficult to use by itself.
For example, if you want this group to show up on pages except for contact us then you should use.
Post Type is equal to Page
AND
Page is not equal to Contact Us
This will also prevent the group from showing on Review and FAQ post types because they are eliminated by the first part of the location rule.
Hi! Sorry to revive this old thread, but I found this searching for an option to deactivate certain fields on the frontend. Right now, I’m able to dequeue the CSS and JS manually and that’s okay for most of my fields, but I also have some fields that echo some content (namely SVG icons that are hidden with display:none) to facilitate rendering them on the backend when they’re needed.
That also comes from the fact that I have a few custom fields plugins enabled on the website I’m working on and I’d like to selectively stop them to be “activated” at all on the frontend. All of the fields are always enqueueing their settings and assets regardless if they’re used or not in the form I’m actively displaying on the frontend. In that example, my SVG Icon selector enqueues it’s CSS, JS and echoes its icons even if there’s no SVG Icon selector field added in the form itself.
Seems like there should be a filter to shortcircuit a field altogether in that case but I can’t find it right now, but that brings me to the other question: aren’t fields lazily initialized? Should they not or is it too complicated since it doesn’t really matter for the backend of things?
Unfortunately, there really isn’t a lot of information.
Basically you need to create a temporary function, what I mean by that is that you create a function that you will remove after your run it once. I usually do this kind of thing on the “init” hook. Also, I do not update anything until I’m sure it’s going to run correctly.
// add to functions.php
add_action('init', 'migrate_user_data');
function migrate_user_data() {
// I usually set this to only run when I am logged it
// so that this does not affect the site
// swap id for your id
if (!is_user_logged_in() || get_current_user_id() != 1) {
// not me
return;
}
}
The rest of the function gets complicated and I don’t have specific code. I’ve pretty much explained already what you’ll do in this function.
1) Do a query to get all of the posts of your post type
2) Loop over the returned posts
3) Get the user ID that matches the post by matching something in the post to the user
4) get the ACF fields that you want to copy from the post
5) Use update_field() to update the values for the user.Be sure to use the field key and not the field name for each field. Also supply the correct user ID for acf which will be 'user_'.$user_id
. update_field('your-field-key, $value, 'user_'.$user_id);
yeah !
<?php $numbers = get_option('options_' .'numbers', true);
if ($numbers) {
for ($i=0; $i < $numbers; $i++) {
$num = 'numbers_'.$i.'_num';
$tel = get_option('options_' . $num, true);
$tel_preg = preg_replace('/[^\d+]/', '', $tel ); ?>
<a href="te:<?php echo $tel_preg;?>" class="phone">
<?php echo $tel; ?>
</a>
<?php }
} ?>
instead
<?php if ( have_rows( 'numbers', 'options' ) ) : ?>
<?php while ( have_rows( 'numbers', 'options' ) ) :
the_row(); ?>
<?php if ( $num = get_sub_field( 'num', 'options' ) ) : ?>
<?php echo esc_html( $num ); ?>
<?php endif; ?>
<?php endwhile; ?>
<?php endif; ?>
You are adding this action, but I do not see the function that is supposed to run
add_action( 'acf/init', 'register_custom_acf_fields' );
Adding an action requires a corresponding function
function register_custom_acf_fields() {
// your code to add field group here
}
Is there anything connecting the posts to the users? Is there some type of a relationship field that is populated?
Is there some unique information that exists for both the post and the user?
Without some way to know what post goes with what user the only way to do this would be manually.
But it there is something that connects them you could create a temporary function that will do a query to get all of one (posts or users) then loop over them and then find an update the other end for each.
Hi, i want to do the same thing, but inside my post.
to describe it more, here is what i have:
within my post i can already return the price of the product that are selected in the ACF relation field (product_id).
the code is:
<?php
$price = get_field('product_id');
if( $price ): ?>
<?php foreach( $price as $p): ?>
<?php global $post;
$product = new WC_Product($p->ID);
echo wc_price( $product->price );
?>
<?php endforeach; ?>
<?php endif;
?>
now i try the same thing, but i don’t want to output the price only, but the whole product instead.
I tried to replace the code with something like this:
<ul class="product">
<?php
$id = get_field('product_id');
$args = array(
'post_type' => 'product',
'post__in' => $id
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
wc_get_template_part( 'content', 'product' );
echo '</div>';
endwhile;
}
wp_reset_postdata();
?>
</ul><!--/.product-->
but it ofc displays all products from the post type ‘product’.
how can i get the correct output, with the products that i’ve selected with the acf relationships field?
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.