Advanced Custom Fields: Select values and create repeater rows - php

I have a list of posts from a custom post type. What I want to do, is for the user to select (checkboxes) all the posts they want to add and create a repeater row for each selection.
Within the repeater, I am using a Post Object field - this post object field is the same as the checkboxes of custom post type posts. (I am doing this, as I need to pull information from each post and the user can then assign further options to this row)
So, how would I allow the user to select these posts and then upon clicking a button, populate the repeater rows? The select value is the post object value from this repeater...
Any help would be appreciated, as I have spent far too long on this, with no solution in sight!

Create a new field group and link it with the post type you require.
In this field group create a field called posts and make it a relationship type field, filtered with the post type you want to be selectable to create repeater field rows.
Make sure return format is set to Post ID.
Then create a repeater field called posts_repeater with sub rows post (post object field) and quantity (number field).
Make sure the post (post object field) return format is also set to Post ID
Now add this to your functions. (read my code comments)
// run modifications when saving or updating the post
add_action('acf/save_post', 'acf_save_post_type', 20);
/**
* action to run modifications when saving or updating the post
* #param int $post_id
* #return void
*/
function acf_save_post_type($post_id) {
// get our current global post object
global $post;
// check we are on the correct post type else return now
if($post->post_type <> 'post') return;
// if post post status is publish or draft
if($post->post_status == 'publish' || $post->post_status == 'draft') {
// if posts repeater does not already have any rows set
if (!get_field('posts_repeater', $post_id)) {
// get the posts relationship field
$posts = get_field('posts', $post_id);
// create an empty repeater field array
$repeater = [];
// for each post selected in posts relationship field
foreach ($posts as $key => $value) {
// add repeater row and assign current posts field id to post object field
$repeater[] = [
'post' => $value
];
}
// temp remove the save post action
remove_action('acf/save_post', 'acf_save_post_type', 20);
// create the repeater field rows
update_field('posts_repeater', $repeater, $post_id);
// re add the save post action
add_action('acf/save_post', 'acf_save_post_type', 20);
}
}
}
This will then populate the repeater with the posts selected in the posts field when updating the post in the admin.
But if the posts_repeater field already has rows, then nothing with happen.
So in your post editor, if you select some posts in the posts acf relationship field...
Then click update, you will now notice the posts_repeater field has the select posts from the posts field defined in each repeater row. Ready to populate quantities.
If you update the post again, because the repeater field now has row data, nothing will happen.
User will have to manually add any extra posts via the repeater field.

Related

Retrieve meta field values of ACF flexible content items using get_post_meta - WordPress

Thanks for your help in advance. Here's what I'm trying to achieve:
I have a custom post type called 'Campaigns' and I have a custom taxonomy called 'Countries' that is related to the campaign custom post type. When a user adds a new country to a campaign a new campaign post is generated that is the child of the current campaign. I'm duplicating the ACF fields that are assigned to the parent campaign and replicating the values in the child post, however I've run into an issue using the ACF flexible content fields. Here'a snippet of my code that is retrieving the parent post fields and updating the newly created ACF field in the child post with that value.
$action_text = get_post_meta($parent_id, 'action_text', true);
update_field('action_text', $action_text, $post_id);
I've tried doing this with flexible content, but I know I need to loop through and find what content blocks have been created. What is the best way to go about this?
// About Fields
$about_fields = get_post_meta($parent_id, 'content');
var_dump($about_fields);
$meta_key = // How to retrieve the flexible content keys
$meta_value_of_flexible_content = get_post_meta($parent_id, $meta_key);
if($about_fields) {
}
For clarification 'content' is the flexible container name. 'text_and_image' is an example name of one of the flexible content blocks I've created.
Thanks again for any insights.
I've tried doing this with flexible content, but I know I need to loop
through and find what content blocks have been created.
You could just use the get_field() and update_field() functions to duplicate any ACF fields, including Flexible Content fields.
So for example, to clone the whole content field:
$about_fields = get_field( 'content', $parent_id );
if ( $about_fields ) {
update_field( 'content', $about_fields, $post_id );
}
// How to retrieve the flexible content keys
foreach ( $about_fields as $arr ) {
echo 'Layout: ' . $arr['acf_fc_layout']; // e.g. "Layout: text_and_image"
// The rest of items in `$arr` are the SUB-fields of that specific layout as
// identified by the `$arr['acf_fc_layout']`, which is the layout's name. So
// if you have two SUB-fields named `text1` and `image1` respectively, then
// these items are set: `$arr['text1']` and `$arr['image1']`
}
Additional Code
To clone all ACF fields:
$fields = get_fields( $parent_id );
foreach ( $fields as $name => $value ) {
update_field( $name, $value, $post_id );
}
Additional Note
I'd change this to use the get_field() function:
$action_text = get_post_meta($parent_id, 'action_text', true);
So:
$action_text = get_field('action_text', $parent_id);

Repeater Field empty in ACF

I'm fairly new in using ACF so I used the code that was displayed in their site to display what is inside of my repeater field.
However, when I try to show the repeater's content it is empty??
This is my code, this is still in trial mode just to see if it's working-which it isn't. My repeater field already has 2 rows but it's not showing up any of those and just displays the else:
// check if the repeater field has rows of data
if( have_rows('map_infogrp') ):
// loop through the rows of data
while ( have_rows('map_infogrp') ) : the_row();
// display a sub field value
the_sub_field('google_map');
the_sub_field('branch_name');
//$street = get_sub_field('street');
//$district = get_sub_field('district');
//$phonenum = get_sub_field('phone_number');
//$email = get_sub_field('email');
endwhile;
else:
echo 'why is this empty???';
endif;
You need to specify the page id that you have set the ACF Repeater, otherwise it will get from the current page ID
have_rows($field_name, $post_id); //syntax
So, update your loop inserting the page ID you've entered the repeater data:
if( have_rows('map_infogrp', 'page_id') ):
// loop through the rows of data
while ( have_rows('map_infogrp', 'page_id') ) : the_row();
...
If you have the fields filled in on the specific page it should be showing up. If not, double check the field name(not label) that you used. If you have more than one row make sure you're printing it out as an array too

Update WordPress user_meta Custom Field with post_meta Custom Field value

I have a front end application, which does what follows:
on a WordPress default page, user selects some options (Custom Fields)
This data is passed to the user_meta Custom Fields when post is submitted
The options selected on the front-end are Custom Fields (post_meta), the fields in the User profile to update are also Custom fields, but in this case user_meta custom Fields
I have many Fields, some have single values, others are with many options (checkboxes Custom Field with 6 options)
My Code does correctly pass the values form all Posts Custom Fields chosen in the front end to the user profile Custom Fields as long the fields are single value fields
When it comes to the checkboxes, it does not update the user fields pendant
The checkboxes field has as said 6 options, so it is not a single value, but I assume a array of values.
This is my code, please see inline comments
the code works very well as long the Custom Fields and User Profile Fields include only ONE value
function user_save_data_action($post_id, $form_data) {
if ($form_data['id']==210) {
//get curretn user's ID
$user_id = get_current_user_id();
//get skype field value suer submits in post (single value)
$skype = get_post_meta( $post_id, 'wpcf-skype', true );
//update the user field with this value
update_user_meta($user_id, 'wpcf-skype', $skype);
//repeats as above
$phone = get_post_meta( $post_id, 'wpcf-phone-number', true );
update_user_meta($user_id, 'wpcf-phone', $phone);
$pic = get_post_meta( $post_id, 'wpcf-profile-photo', true );
update_user_meta($user_id, 'wpcf-profile-image', $pic);
//now the trouble part, this is a checkboxes field, 6 options can be chosen (languages) before submitting the post
//the Custom Field (for post) name is wpcf-languages
//get the values form the Custom Field that user submits
//my checkbox field with 6 options, false because I want a array, right?
$langs = get_post_meta( $post_id, 'wpcf-languages', false);
//separate the options received, get the value of each option of the Custom Field (checkboxes wpcf-languages)
$new_lang = array();
foreach( $langs as $lang ) {
$new_lang[] = $lang->value;
}
//then update the Wordpress user_meta profile field (custom field as well, name is wpcf-support-languages)
update_user_meta($user_id, 'wpcf-support-languages', $new_lang);
//below I do some other staff that also correctly works.
//Update slug with user_name
$custom_title = wp_get_current_user();
$new_title = $custom_title->user_login;
//collect data and define new title
$my_post = array(
'ID' => $post_id,
'post_name' => $new_title,
'post_title'=> $new_title,
);
// Update the post into the database
wp_update_post( $my_post );
}
}
add_action('cred_save_data', 'user_save_data_action',10,2);
It is worth mentioning that I have tried many different approaches, I won't share them all here because it would simply clutter the post with non-working code.
Basically, I know I must somehow get all options from wpcf-languages, then update all options in wpcf-support-languages
I have done all the var_dump etc, the problem is, I don't need to know "what" it returns (I already know, it returns 6 options, value either checked or not)
I need to know how to update the user fields with.
I can, retrieving manually form DB, update the user meta_fields passing the name and value of each field in a array like this:
$lang_array = array(
"wpcf-fields-checkboxes-option-9f3e73c981257b02b9ea5dd006b8fcf9-1" => $English,
"wpcf-fields-checkboxes-option-ea8fcf9b3859a64eed39e169c9d6e42e-1" => $German,
"wpcf-fields-checkboxes-option-b9e50423feb4d0adcd90a7a0a9dc34d6-1" => $Spanish,
"wpcf-fields-checkboxes-option-e7f388081cbee7db725dec32df09b30f-1" => $French,
"wpcf-fields-checkboxes-option-574adb7dec19fad81973a4c3b323b898-1" => $Italian,
"wpcf-fields-checkboxes-option-e2d4422868a74ea8cbc85ab2d70943f7-1" => $Arabic,
"wpcf-fields-checkboxes-option-8ee5abd3454446fb0423a6c5300126b9-1" => $Russian,
"wpcf-fields-checkboxes-option-16d64ea0842251287f056a9eebecf756-1" => $Chinese,);
update_user_meta($user_id, $lang_array , $lang);
But anyone can see, this is NOT what I want, this will ALWAYS set ALL options to "true" and user still can't edit his WordPress Profile choosing from Front-End just as he can do with single fields as Skype, Phone, etc.
Any solution or point-to is very welcome

get custom fields value in wordpress while saving the post and save these values in different table in db

I have 2 custom post types created with ACF
1 is check box, meta key = google_search
2nd is a text field, meta key = search_keyword
While publishing post I want to check if the google_search is checked and there is a value in search_keyword then
Get the search_keyword value and save in to a custom table
if search_keyword is empty then use the Title of post and save it into the custom table.
I tried this code in functions.php and post.php (both) but no luck,
add_action('save_post', 'googleSearch');
function googleSearch()
{
global $wpdb;
if ( $_POST['google_results'] == "Y") // if checkbox (google_search) is checked
{
if ($_POST['search_keyword'] != "") // if search_keyword has value
{
$sSearchKeyword = get_field('search_keyword');
}
else
{
//if search_keyword is empty then use post title as keyword.
$sSearchKeyword = get_the_title( );
}
$wpdb->query("INSERT INTO tbl_keyword SET post_id = '$post_ID',
keyword = '$sSearchKeyword'");
}
}
Any help how to get custom field values while saving post and save these values in db.
If you are not using the function get_field within a wordpress loop, then you have to define the ID of the post.
get_field('search_keyword', $post_ID);
And var_dump $post_ID to confirm that this variable is actually holding the post id.

Pods - display link to relationship post

I'm using Pods to set up Custom Post Types in WordPress.
I have two custom post types: Car and Garage. In Car I have set up a relationship field with Garage, so when editing a Car post I can select one of the Garage posts available from the drop-down.
When viewing a Car post, I would like to display the name and the link to the garage post selected. How would I do that?
Below is an example from http://pods.io/tutorials/get-values-from-a-custom-relationship-field
//get Pods object for current post
$pod = pods( 'pod_name', get_the_id() );
//get the value for the relationship field
$related = $pod->field( 'relationship_field' );
//loop through related field, creating links to their own pages
//only if there is anything to loop through
if ( ! empty( $related ) ) {
foreach ( $related as $rel ) {
//get id for related post and put in ID
//for advanced content types use $id = $rel[ 'id' ];
$id = $rel[ 'ID' ];
//show the related post name as link
echo ''.get_the_title( $id ).'';
//get the value for some_field in related post and echo it
} //end of foreach
} //endif ! empty ( $related )
Instead of a permalink to a Garage post, I'm getting several permalinks to the Car post I'm viewing.
Any Ideas?

Categories