Wordpress/ACF merging multiple fields value to one
Is there any way to get values from multiple fields (for example: “value_1”, “value_2”, etc.) and merge all of them to a new field, let`s say “merged_values” after publishing/editing the post?
Something like this:
If:
value_1 = red blue; value_2 = gold silver;
Then:
merged_values = red blue gold silver;
I need to achieve this, because of a search plugin, that doesn`t show the results if the search query is “red silver” and there is no option in it to combine the search within multiple ACF fields.
(to be exact, I need the “merged_values” field to be written in the db as the example above – not just echo the values together in the front-end).
The output from the default fields is pure text with spaces and the "merged_values" should also be just pure text (no arrays, etc.)
wordpress advanced-custom-fields
add a comment |
Is there any way to get values from multiple fields (for example: “value_1”, “value_2”, etc.) and merge all of them to a new field, let`s say “merged_values” after publishing/editing the post?
Something like this:
If:
value_1 = red blue; value_2 = gold silver;
Then:
merged_values = red blue gold silver;
I need to achieve this, because of a search plugin, that doesn`t show the results if the search query is “red silver” and there is no option in it to combine the search within multiple ACF fields.
(to be exact, I need the “merged_values” field to be written in the db as the example above – not just echo the values together in the front-end).
The output from the default fields is pure text with spaces and the "merged_values" should also be just pure text (no arrays, etc.)
wordpress advanced-custom-fields
add a comment |
Is there any way to get values from multiple fields (for example: “value_1”, “value_2”, etc.) and merge all of them to a new field, let`s say “merged_values” after publishing/editing the post?
Something like this:
If:
value_1 = red blue; value_2 = gold silver;
Then:
merged_values = red blue gold silver;
I need to achieve this, because of a search plugin, that doesn`t show the results if the search query is “red silver” and there is no option in it to combine the search within multiple ACF fields.
(to be exact, I need the “merged_values” field to be written in the db as the example above – not just echo the values together in the front-end).
The output from the default fields is pure text with spaces and the "merged_values" should also be just pure text (no arrays, etc.)
wordpress advanced-custom-fields
Is there any way to get values from multiple fields (for example: “value_1”, “value_2”, etc.) and merge all of them to a new field, let`s say “merged_values” after publishing/editing the post?
Something like this:
If:
value_1 = red blue; value_2 = gold silver;
Then:
merged_values = red blue gold silver;
I need to achieve this, because of a search plugin, that doesn`t show the results if the search query is “red silver” and there is no option in it to combine the search within multiple ACF fields.
(to be exact, I need the “merged_values” field to be written in the db as the example above – not just echo the values together in the front-end).
The output from the default fields is pure text with spaces and the "merged_values" should also be just pure text (no arrays, etc.)
wordpress advanced-custom-fields
wordpress advanced-custom-fields
edited Nov 14 '18 at 13:01
barbsan
2,43721223
2,43721223
asked Nov 14 '18 at 12:51
Martin PetroMartin Petro
32
32
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
I think you want something like this
// This function runs after your post is saved
function my_acf_save_post( $post_id ) {
// Get new value of field 1
$value1 = get_field( 'field1', $post_id );
// Get new value of field 2
$value2 = get_field( 'field2', $post_id );
// Merged values with ; on the end
$merge = implode(" ",$value1).' '.implode(" ",$value2);
// Update field 3 with the new value which should be
// value1 value2;
update_field( 'field3', $merge, $post_id );
}
add_action('acf/save_post', 'my_acf_save_post', 20);
I'm not sure if you wanted to put the merged value in another ACF field. If not you can use $wpdb to insert it in de database manually.
This returns "Array Array;" to the final field. (And also, no need to remove ";", that`s not in the content of the fields (just used it to separate it here)).
– Martin Petro
Nov 14 '18 at 14:22
Sorry, my bad - the main fields were checkboxes - I used implode - answer at the bottom :) THANK YOU SO MUCH!
– Martin Petro
Nov 14 '18 at 14:39
I changed this answer. Can you mark this one as the answer and remove your own answer. Otherwise it will confuse people who might have the same question.
– Wilco
Nov 14 '18 at 16:01
Done. Just the "// Merged values with ; on the end" isnt right, cause there
s no ; there :)
– Martin Petro
Nov 15 '18 at 13:09
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53300685%2fwordpress-acf-merging-multiple-fields-value-to-one%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I think you want something like this
// This function runs after your post is saved
function my_acf_save_post( $post_id ) {
// Get new value of field 1
$value1 = get_field( 'field1', $post_id );
// Get new value of field 2
$value2 = get_field( 'field2', $post_id );
// Merged values with ; on the end
$merge = implode(" ",$value1).' '.implode(" ",$value2);
// Update field 3 with the new value which should be
// value1 value2;
update_field( 'field3', $merge, $post_id );
}
add_action('acf/save_post', 'my_acf_save_post', 20);
I'm not sure if you wanted to put the merged value in another ACF field. If not you can use $wpdb to insert it in de database manually.
This returns "Array Array;" to the final field. (And also, no need to remove ";", that`s not in the content of the fields (just used it to separate it here)).
– Martin Petro
Nov 14 '18 at 14:22
Sorry, my bad - the main fields were checkboxes - I used implode - answer at the bottom :) THANK YOU SO MUCH!
– Martin Petro
Nov 14 '18 at 14:39
I changed this answer. Can you mark this one as the answer and remove your own answer. Otherwise it will confuse people who might have the same question.
– Wilco
Nov 14 '18 at 16:01
Done. Just the "// Merged values with ; on the end" isnt right, cause there
s no ; there :)
– Martin Petro
Nov 15 '18 at 13:09
add a comment |
I think you want something like this
// This function runs after your post is saved
function my_acf_save_post( $post_id ) {
// Get new value of field 1
$value1 = get_field( 'field1', $post_id );
// Get new value of field 2
$value2 = get_field( 'field2', $post_id );
// Merged values with ; on the end
$merge = implode(" ",$value1).' '.implode(" ",$value2);
// Update field 3 with the new value which should be
// value1 value2;
update_field( 'field3', $merge, $post_id );
}
add_action('acf/save_post', 'my_acf_save_post', 20);
I'm not sure if you wanted to put the merged value in another ACF field. If not you can use $wpdb to insert it in de database manually.
This returns "Array Array;" to the final field. (And also, no need to remove ";", that`s not in the content of the fields (just used it to separate it here)).
– Martin Petro
Nov 14 '18 at 14:22
Sorry, my bad - the main fields were checkboxes - I used implode - answer at the bottom :) THANK YOU SO MUCH!
– Martin Petro
Nov 14 '18 at 14:39
I changed this answer. Can you mark this one as the answer and remove your own answer. Otherwise it will confuse people who might have the same question.
– Wilco
Nov 14 '18 at 16:01
Done. Just the "// Merged values with ; on the end" isnt right, cause there
s no ; there :)
– Martin Petro
Nov 15 '18 at 13:09
add a comment |
I think you want something like this
// This function runs after your post is saved
function my_acf_save_post( $post_id ) {
// Get new value of field 1
$value1 = get_field( 'field1', $post_id );
// Get new value of field 2
$value2 = get_field( 'field2', $post_id );
// Merged values with ; on the end
$merge = implode(" ",$value1).' '.implode(" ",$value2);
// Update field 3 with the new value which should be
// value1 value2;
update_field( 'field3', $merge, $post_id );
}
add_action('acf/save_post', 'my_acf_save_post', 20);
I'm not sure if you wanted to put the merged value in another ACF field. If not you can use $wpdb to insert it in de database manually.
I think you want something like this
// This function runs after your post is saved
function my_acf_save_post( $post_id ) {
// Get new value of field 1
$value1 = get_field( 'field1', $post_id );
// Get new value of field 2
$value2 = get_field( 'field2', $post_id );
// Merged values with ; on the end
$merge = implode(" ",$value1).' '.implode(" ",$value2);
// Update field 3 with the new value which should be
// value1 value2;
update_field( 'field3', $merge, $post_id );
}
add_action('acf/save_post', 'my_acf_save_post', 20);
I'm not sure if you wanted to put the merged value in another ACF field. If not you can use $wpdb to insert it in de database manually.
edited Nov 15 '18 at 14:20
answered Nov 14 '18 at 14:03
WilcoWilco
305210
305210
This returns "Array Array;" to the final field. (And also, no need to remove ";", that`s not in the content of the fields (just used it to separate it here)).
– Martin Petro
Nov 14 '18 at 14:22
Sorry, my bad - the main fields were checkboxes - I used implode - answer at the bottom :) THANK YOU SO MUCH!
– Martin Petro
Nov 14 '18 at 14:39
I changed this answer. Can you mark this one as the answer and remove your own answer. Otherwise it will confuse people who might have the same question.
– Wilco
Nov 14 '18 at 16:01
Done. Just the "// Merged values with ; on the end" isnt right, cause there
s no ; there :)
– Martin Petro
Nov 15 '18 at 13:09
add a comment |
This returns "Array Array;" to the final field. (And also, no need to remove ";", that`s not in the content of the fields (just used it to separate it here)).
– Martin Petro
Nov 14 '18 at 14:22
Sorry, my bad - the main fields were checkboxes - I used implode - answer at the bottom :) THANK YOU SO MUCH!
– Martin Petro
Nov 14 '18 at 14:39
I changed this answer. Can you mark this one as the answer and remove your own answer. Otherwise it will confuse people who might have the same question.
– Wilco
Nov 14 '18 at 16:01
Done. Just the "// Merged values with ; on the end" isnt right, cause there
s no ; there :)
– Martin Petro
Nov 15 '18 at 13:09
This returns "Array Array;" to the final field. (And also, no need to remove ";", that`s not in the content of the fields (just used it to separate it here)).
– Martin Petro
Nov 14 '18 at 14:22
This returns "Array Array;" to the final field. (And also, no need to remove ";", that`s not in the content of the fields (just used it to separate it here)).
– Martin Petro
Nov 14 '18 at 14:22
Sorry, my bad - the main fields were checkboxes - I used implode - answer at the bottom :) THANK YOU SO MUCH!
– Martin Petro
Nov 14 '18 at 14:39
Sorry, my bad - the main fields were checkboxes - I used implode - answer at the bottom :) THANK YOU SO MUCH!
– Martin Petro
Nov 14 '18 at 14:39
I changed this answer. Can you mark this one as the answer and remove your own answer. Otherwise it will confuse people who might have the same question.
– Wilco
Nov 14 '18 at 16:01
I changed this answer. Can you mark this one as the answer and remove your own answer. Otherwise it will confuse people who might have the same question.
– Wilco
Nov 14 '18 at 16:01
Done. Just the "// Merged values with ; on the end" isn
t right, cause there
s no ; there :)– Martin Petro
Nov 15 '18 at 13:09
Done. Just the "// Merged values with ; on the end" isn
t right, cause there
s no ; there :)– Martin Petro
Nov 15 '18 at 13:09
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53300685%2fwordpress-acf-merging-multiple-fields-value-to-one%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown