I succeeded in realize what i wanted. But i don’t know if it’s safe way :
<?php function sidebar_related() {
<strong>ob_start();</strong>
$type = get_field( "themes", get_the_ID() );
$args = array(
'post_type' => 'post',
'posts_per_page' => -1,
'orderby' => 'date',
'post__not_in' => array(get_the_ID()),
'order' => 'DESC',
'meta_query' => array(
array(
'key' => 'themes',
'value' => $type
)
),
);
$my_posts = new WP_Query($args); ?>
<ul>
<?php if ( $my_posts->have_posts() ) : while ( $my_posts->have_posts() ) : $my_posts->the_post(); ?>
<li><a href="<?php the_permalink(); ?>"><?php the_title() ;?></a></li>
<?php endwhile; ?>
</ul>
<?php endif;
wp_reset_postdata();
<strong>$output_string = ob_get_contents();
ob_end_clean();
return $output_string;</strong>
}
add_shortcode('related-terms', 'sidebar_related');
Any idea ?
Ok, i resolve the problem not to display in the list the title of the current post :
<?php
function sidebar_related() {
$type = get_field( "themes", get_the_ID() );
$args = array(
'post_type' => 'post',
'posts_per_page' => -1,
'orderby' => 'date',
'post__not_in' => array(get_the_ID()),
'order' => 'DESC',
'meta_query' => array(
array(
'key' => 'themes',
'value' => $type
)
),
);
$my_posts = new WP_Query($args); ?>
<?php if ( $my_posts->have_posts() ) : while ( $my_posts->have_posts() ) : $my_posts->the_post(); ?>
<a href="<?php the_permalink(); ?>"><?php the_title() ;?></a>
<?php endwhile; else : ?>
<p><?php _e( 'No Posts To Display.' ); ?></p>
<?php endif;
}
add_shortcode('related-terms', 'sidebar_related');
?>
To complete, anyone has an idea to display my shortcode [related-terms] in the right place ?
I can’t understand why is out of its module…
I succeeded to have a beginning of resukt with :
function sidebar_related() {
$type = get_field( "themes", get_the_ID() );
$args = array(
'post_type' => 'post',
'posts_per_page' => -1,
'orderby' => 'date',
'order' => 'DESC',
'meta_query' => array(
array(
'key' => 'themes',
'value' => $type
)
),
);
$my_posts = new WP_Query($args); ?>
<?php if ( $my_posts->have_posts() ) : while ( $my_posts->have_posts() ) : $my_posts->the_post(); ?>
<a href="<?php the_permalink(); ?>"><?php the_title() ;?></a>
<?php endwhile; else : ?>
<p><?php _e( 'No Posts To Display.' ); ?></p>
<?php endif;
}
add_shortcode('related-terms', 'sidebar_related');
Is it possible not to display in the list the title of the current post ?
Thank you !
EDIT :
I use the DIVI builder and try to realize that query as shortcode in a TXT module.
I can’t understand why the results don’t display properly in this right place.
Thank you again
thx @hube2 for the quick reply… unfortunately we did speed up our servers now and just hope to cover this issue for a longer time…
Your query should look more like this
$args = array(
'numberposts' => -1,
'post_type' => 'artikel',
'meta_query' => array(
'relation' => 'AND',
'date_clause' => array(
'key' => 'year',
'value' => 1900,
'type' => 'NUMERIC',
'compare' => '>'
)
),
'orderby' => array('date_clause' => 'ASC')
);
You do not need the “EXISTS” meta query since you only want posts that have a value greater than something.
I have also added a “date_clause” for a index so that they can be ordered by this field and added the orderby.
$query = new WP_query($arg);
if ($query->have_posts()) {
$last_post_year = 0; // year of last post shown initialized to zero
while ($query->have_posts()) {
the_post();
$this_post_year = get_field('year');
if ($this_post_year != $last_post_year) {
// this post is in a different year than the last one
echo $year;
}
$last_post_year = $this_post_year; // update last to this
// anything else you want to this post
}
}
Got it! This just happens to be a nested flexible content field.
<?php if (get_row_layout() == 'flexibl_content_field') :
$layouts = get_sub_field('sub_flexible_content_field');
$layoutsCount = count($layouts);
?>
and use it like this in the while loop
<div class="col-md-6 <?php echo $layoutsCount == 3 ? 'col-lg-4' : 'col-lg-6'; ?>
You would need to contact the developer to get an answer to that question.
Just bringing this topic up again.
I’ve been using the last solution posted here for a while now, it’s great but it just isn’t perfect yet. Sometimes when duplicating and dragging a field to another repeater row it just deletes it after publishing (and I’m experiencing some other small bugs).
My only question right now is: Is it still planned to implement this in ACF Pro? I’ve been reading this question a multiple times now and I think it would be a great feature if implemented in ACF Pro.
Thank you so much John!
Just a quick change I made for UI’s sake. Instead of a repeater, I’m using a Post Object field which allows for multiple values, and it returns a Post ID. How does that impact your code?
Hmmm, I had tried it with quotes also. Either way, with or without the quotes, it reads the field OK and does the calculation as expected.
I also read somewhere (forgotten where, amongst all the Googling!) that with date types, the field key should be used instead of the field name.
Field name, field key, with and without quotes, the update field doesn’t seem to write data and the field remains blank.
I tried also adding $post_id too, although I believe this defaults to the current post anyway.
$reviewDate = get_field('document_review_date');
$remindDate = date("Ymd", strtotime($reviewDate . '-7 days'));
update_field('field_60f4466dcddc8', $remindDate, $post_id);
Old post but if it can helps someone, wp_reset_query(); worked for me (instead of wp_reset_postdata();
It sadly did not work for me but I tried $args['eventDisplay'] = 'custom';
and it works now 🙂
My final code:
function my_relationship_query( $args, $field, $post )
{
// add and define tribe events eventDisplay to 'all' since it's predifined only to future.
$args['eventDisplay'] = 'custom';
return $args;
}
// acf/fields/relationship/result - filter for every field
add_filter('acf/fields/relationship/query', 'my_relationship_query', 10, 3);
is that the actual code? The field name should be in quotes
update_field('document_review_date_reminder_trigger', $remindDate);
You may need to provide the post ID
update_field('document_review_date_reminder_trigger', $remindDate, $post_id);
UPDATE
I got it to work (thanks to ACF support), so it seems only right to let you guys know if you ever run into this problem.
The solution for getting the repeater values to appear on the WooCommerce search page was the use of acf-json. This way, the field groups are loaded locally and not from the database. So the field definition lookup query is not affected by any other WP filters.
Worked like a charm!
Hi,
I am not quite sure if this is the right topic but before opening a new thread I’d like to try it here first as it is somehow the same issue. Pls let me know if this would be worth a new topic…
My problem:
I use AFC very effective and pleased in the whole project. Since I created a new flip card group component I have the issue that editing the specific page creates an overload on RAM and CPU on my server. Viewing the page and clicking around in the admin console is fine. Only when I start editing and especially when I try to save my changes, the server looses connection to the database…
here is my layout:
<?php
namespace Flynt\Components\BnbCmsFlipCardGroup;
function getACFLayout()
{
return [
'name' => 'BnbCmsFlipCardGroup',
'label' => 'BNB CMS Flip Card Group',
'sub_fields' => [
[
'label' => __('Allgemeines', 'flynt'),
'name' => 'general',
'type' => 'tab',
],
[
'label' => __('Jumpmark', 'flynt'),
'name' => 'jumpmark',
'type' => 'text',
'instructions' => 'ACHTUNG: Jumpmarks MÜSSEN pro Seite einzigartig sein und dürfen KEINE Leerzeichen, Sonderzeichen oder Umlaute enthalten.<br>Doppelte Jumpmarks verringern die SEO Qualität und nur die erste Jumpmark wird angesprungen.',
],
[
'label' => 'Farbe (Hintergrund-Rahmen)',
'name' => 'color',
'type' => 'select',
'choices' => [
'black-black' => 'Standard Schwarz',
'yellow-red' => 'Gelb-Rot',
'red-yellow' => 'Rot-Gelb',
'green-red' => 'Grün-Rot',
'red-green' => 'Rot-Grün',
'yellow-purple' => 'Gelb-Lila',
'purple-yellow' => 'Lila-Gelb',
'green-purple' => 'Grün-Lila',
'purple-green' => 'Lila-Grün',
],
],
[
'label' => __('Headline', 'flynt'),
'name' => 'headline',
'type' => 'text',
],
[
'label' => __('Text', 'flynt'),
'name' => 'text',
'type' => 'wysiwyg',
'media_upload' => 0,
'delay' => 1,
],
[
'label' => __('Globaler Prefix Text Vorderseite', 'flynt'),
'name' => 'prefix',
'type' => 'text',
],
[
'label' => __('Flip Card Elemente', 'flynt'),
'name' => 'flip_card_items',
'type' => 'tab',
],
[
'label' => __('Flip Cards', 'flynt'),
'name' => 'flip_cards',
'type' => 'repeater',
'collapsed' => 'field_pageComponents_pageComponents_BnbCmsFlipCardGroup_flip_cards_text_front',
'layout' => 'row',
'min' => 1,
'button_label' => 'Hinzufügen',
'sub_fields' => [
[
'label' => __('Text', 'flynt'),
'name' => 'text_settings',
'type' => 'tab',
],
[
'label' => __('Text Vorderseite', 'flynt'),
'name' => 'text_front',
'type' => 'text',
],
[
'label' => __('Text Rückseite', 'flynt'),
'name' => 'text_back',
'type' => 'wysiwyg',
'media_upload' => 0,
'delay' => 1,
],
[
'label' => __('Bild', 'flynt'),
'name' => 'image_setting',
'type' => 'tab',
],
[
'label' => 'Image',
'name' => 'image',
'type' => 'image',
'return_format' => 'id',
'preview_size' => 'medium',
],
[
'label' => 'Bildrechte',
'name' => 'image_copyright',
'type' => 'text',
'instructions' => 'Überschreibt die Daten aus dem Feld "Beschreibung" der Mediathek.<br>NOTIZ: KEINE Zeilenumbrüche. Anführungszeichen müssen einen vorhergehenden Backslash haben (z.B. \"Lorem Ipsum\").',
],
[
'label' => 'Bild Alt-Text',
'name' => 'image_alt',
'instructions' => 'Überschreibt die Daten aus dem Feld "Alternativer Text" der Mediathek. Wird nur gesetzt, sollte keine Bildunterschrift gesetzt werden!<br>NOTIZ: KEINE Zeilenumbrüche. Anführungszeichen müssen einen vorhergehenden Backslash haben (z.B. \"Lorem Ipsum\").',
'type' => 'text',
],
[
'label' => 'Bild Helligkeit',
'name' => 'image_mode',
'type' => 'radio',
'choices' => [
'false' => 'Dark copyright',
'true' => 'Bright copyright',
],
],
[
'label' => 'Bild Abdeckung',
'name' => 'image_coverage',
'type' => 'radio',
'instructions' => '
Cover: Der ersetzte Inhalt wird in der Größe angepasst, sodass das Seitenverhältnis beibehalten wird während die gesamte Inhaltsbox des Elements ausgefüllt wird: die konkrete Objektgröße wird als eine Abdeckbeschränkung auf die verwendete Breite und Höhe des Elements bestimmt.
<br>
<br>
Contain: Der ersetzte Inhalt wird in der Größe angepasst, sodass das Seitenverhältnis beibehalten wird während es an die Inhaltsbox des Elements angepasst wird: die konkrete Objektgröße wird als eine Inhaltsbeschränkung auf die verwendete Breite und Höhe des Elements bestimmt.
',
'choices' => [
'cover' => 'Cover',
'contain' => 'Contain Whole Image',
],
],
[
'label' => 'Bild Position',
'name' => 'image_position',
'type' => 'group',
'sub_fields' => [
[
'label' => 'Bild Position Wert #1',
'name' => 'image_position_value1',
'type' => 'radio',
'choices' => [
'top' => 'top',
'bottom' => 'bottom',
'left' => 'left',
'right' => 'Right',
'center' => 'Center',
],
'layout' => 'horizontal',
],
[
'label' => 'Bild Position Wert #2',
'name' => 'image_position_value2',
'type' => 'radio',
'choices' => [
'top' => 'top',
'bottom' => 'bottom',
'left' => 'left',
'right' => 'Right',
'center' => 'Center',
],
'layout' => 'horizontal',
],
[
'label' => 'Prozentwerte',
'name' => 'percentage',
'type' => 'text',
'instructions' => '
Insert 1-2 values as
<br>
A) Prozentwerte <b>z.B. 50% 20%</b> für die horizontale und vertikale Achse
<br>
<b>OR</b>
<br>
B) Pixel <b>z.B. 100px 20px</b> für eine pixelgenaue Position
',
],
],
],
],
],
],
];
}
As a hotfix we where able to solve it by increasing the RAM and CPU on the server but I am afraid that this will pop up again as we did not even have the full content on our staging system so far.
Any ideas why? As we will have to go live with the feature pretty soon, I am a bit concerned I missed something in the structure to prevent this issue…
thx in advance.
Your problem is that the action hook that you are using happens before the hook used by acf
/** This action is documented in wp-includes/post.php */
do_action( "save_post_{$post->post_type}", $post->ID, $post, true );
/** This action is documented in wp-includes/post.php */
do_action( 'save_post', $post->ID, $post, true );
ACF is triggered on the save_post hook with a priority of 10. This means that when your action runs ACF has not yet saved any values. The first time your action runs there are no values and thereafter each times that your action runs it is getting the old value instead of the new value.
You have 2 choices.
1) Get the value from the submitted value in $_POST[‘acf’] see discussion about applied before save for acf/save_post https://www.advancedcustomfields.com/resources/acf-save_post/
2) You could use the save_post hook with a priority > 10.
let me know if you have any questions
add_filter('acf/load_value/name=add_on_repeater', 'prepopulate_add_on_repeater', 20, 3);
function prepopulate_add_on_repeater($value, $post_id, $field) {
if (!empty($value)) {
// already has a value, do not overwrite
return $value;
}
// do a query to get all posts of the post type
$args = array(
'post_type' => 'add-ons', // your post type
'posts_per_page' => -1, // -1 get all
'fields' => 'ids', // return only list of IDs
);
$query = new WP_Query($args);
if (empty($query->posts)) {
// no posts found
return $value;
}
// initialize value, a repeater has an array value
$value = array();
// loop over posts and built repeater value
foreach ($query->posts as $add_on_post_id) {
// each row of the repeater is a nested array
$value[] = array(
// you must use field key to set value of sub field of each row
'field_XXXXXXX' => $add_on_post_id
);
}
return $value;
}
Just jumping on this for advice.
I’m trying to calculate a date (review date – 7 days = reminder date)
so I have 2 ACF fields:
– document_review_date
– document_review_date_reminder_trigger
In my function, I have:
$reviewDate = get_field(document_review_date);
$remindDate = date("Ymd", strtotime($reviewDate . '-7 days'));
update_field(document_review_date_reminder_trigger, $remindDate);
$remindDate is returning the correct calculated date.
However, it doesn’t seem to be updating my ACF field as it is not shown in the field in the post editor.
What am I doing wrong here?
Hi, Noone responds but I try to use pre_get_posts:
function my_pre_get_posts( $expo_query ) {
// do not modify queries in the admin
if( is_admin() ) {
return $expo_query ;
}
// only modify queries for 'exposiciones' post type
if( isset($expo_query ->query_vars['post_type']) && $expo_query ->query_vars['post_type'] == 'exposiciones' ) {
$expo_query ->set('meta_key', 'expo_start_date');
$expo_query ->set('orderby', 'meta_value');
$expo_query ->set('order', 'DESC');
}
// return
return $expo_query ;
}
add_action('pre_get_posts', 'my_pre_get_posts');
without success. 🙁
Hi!
Some who know what i’m doing wrong?
Can’t get the custom tab or field to display for each specific variation, when i choose the variants color and size, the unique description appears, not the tab and field.
It’s unique product User manual links foreach.
Variation custom field code is from this forum and works so far!
Problem is to display when specific variation is active or not, and to hide and display the “Downloads” tab window, when there is or not a download file link in the wysiwyg editor field?
add_filter('acf/location/rule_values/post_type', 'acf_location_rule_values_Post');
function acf_location_rule_values_Post( $choices ) {
$choices['product_variation'] = 'Product Variation';
return $choices;
}
add_action( 'woocommerce_product_after_variable_attributes', function( $loop, $variation_data, $variation ) {
global $abcdefgh_i;
$abcdefgh_i = $loop;
add_filter( 'acf/prepare_field', 'acf_prepare_field_update_field_name' );
$acf_field_groups = acf_get_field_groups();
foreach( $acf_field_groups as $acf_field_group ) {
foreach( $acf_field_group['location'] as $group_locations ) {
foreach( $group_locations as $rule ) {
if( $rule['param'] == 'post_type' && $rule['operator'] == '==' && $rule['value'] == 'product_variation' ) {
acf_render_fields( $variation->ID, acf_get_fields( $acf_field_group ) );
break 2;
}
}
}
}
remove_filter( 'acf/prepare_field', 'acf_prepare_field_update_field_name' );
}, 10, 3 );
function acf_prepare_field_update_field_name( $field ) {
global $abcdefgh_i;
$field['name'] = preg_replace( '/^acf\[/', "acf[$abcdefgh_i][", $field['name'] );
return $field;
}
add_action( 'woocommerce_save_product_variation', function( $variation_id, $i = -1 ) {
if ( ! empty( $_POST['acf'] ) && is_array( $_POST['acf'] ) && array_key_exists( $i, $_POST['acf'] ) && is_array( ( $fields = $_POST['acf'][ $i ] ) ) ) {
foreach ( $fields as $key => $val ) {
update_field( $key, $val, $variation_id );
}
}
}, 10, 2 );
function xxx_admin_head_post() {
global $post_type;
if ($post_type === 'product') {
wp_register_script( 'xxx-acf-variation', get_template_directory_uri() . '/inc/ss.js', array(
'jquery-core',
'jquery-ui-core'
), '1.1.0',
true ); // Custom scripts
wp_enqueue_script( 'xxx-acf-variation' ); // Enqueue it!
}
}
/* actions fired when adding/editing posts or pages */
/* admin_head-(hookname) */
add_action( 'admin_head-post.php', 'xxx_admin_head_post' );
add_action( 'admin_head-post-new.php', 'xxx_admin_head_post' );
Showing the tab:
/* Create Technical details tab */
//adding new tab//
add_filter( 'woocommerce_product_tabs', 'product_tab' );
function product_tab( $tabs ) {
if ( get_field('downloads', $post_id) ) {
$tabs[] = array(
'title' => 'Downloads',
'priority' => 15,
'callback' => 'show_content'
);
}
return $tabs;
}
function show_content() {
$downloads = get_field('downloads', $post_id);
if( $downloads ):
echo get_field('downloads', $post_id);
endif;
}
The settings in the ACF – admin panel is this as attached images below:
the name “Vare” = “Product”
This is a user help forum. Getting an answer here would need some other user that has solved this issue. I do not create options pages that need to be edited regularly. If something needs to be edited on an ongoing basis by multiple people then I would likely look for a better solution than an options page.
I just did search for something, locking an options page would be the same as locking any other admin page added with add_menu_page(), which is what ACF uses to add the page. As well as any of the existing admin pages on the site. If these pages could be easily locked I would suspect that someone would have figured it out by now.
The difference here is that a post as a specific entry in the database and a flag can be added to the database to indicate that page is currently being edited by someone else. Options do not support this. All fields on any options page or any settings page are all stored in the options table and there is no mechanism available to flag all of the fields on a particular page as being edited.
This question has not gotten an answer probably because what was asked is not possible.
Here’s how I’m handling block color settings:
This function parses the $block
attributes and settings:
/**
* Get an ACF block's color settings.
*
* @param array $block The block settings and attributes.
*/
function get_block_color_attrs( $block = null ) {
if ( ! $block ) {
return;
}
$block_class = null;
$block_style = null;
if ( $block['backgroundColor'] ) {
$block_class .= ' has-background has-' . $block['backgroundColor'] . '-background-color ';
}
if ( $block['textColor'] ) {
$block_class .= ' has-text-color has-' . $block['textColor'] . '-color ';
}
if ( $block['style']['color']['background'] ) {
$block_class .= ' has-background ';
$block_style .= 'background-color: ' . $block['style']['color']['background'] . ';';
}
if ( $block['style']['color']['text'] ) {
$block_class .= ' has-text-color ';
$block_style .= 'color: ' . $block['style']['color']['text'] . ';';
}
return array(
'class' => $block_class,
'style' => $block_style,
);
}
Then I print that information in the block wrapper element:
<div class="<?php echo esc_attr( $block_color_attrs['class'] ); ?>" style="<?php echo esc_attr( $block_color_attrs['style'] ); ?>">
[...]
</div>
Block color settings are available via the $block
array, but it’s trickier than it seems. First of all, if you’re referencing an array value by key name, the key must be in quotes. E.g.:
$block[textColor]; // <-- THIS *WON'T* WORK.
$block['textColor']; // <-- THIS *WILL* WORK.
The next thing to note is that different $block
settings will be returned depending on whether a predefined named color was picked from the palette or a custom color was picked.
If you pick one of the default palette colors (or if you have set your own palette colors with the add_theme_support('editor-color-palette', array( ... ));
, it will return the slug
of the color (e.g., blue
) instead of a hex-value, and in two separate array keys.
If you pick custom colors, they will be accessible via $block['style']['color']['background']
and $block['style']['color']['text']
.
If you pick two predefined colors:
$block Array => (
...
[backgroundColor] => bright-purple
[textColor] => white
)
If you pick two custom colors:
$block Array => (
...
[style] => Array (
[color] => Array (
[background] => #cccccc
[text] => #aa0000
)
)
)
And if you pick one palette color and one custom color:
$block Array => (
...
[textColor] => black
[style] => Array
(
[color] => Array
(
[background] => #cccccc
)
)
)
** Ignore this – I have asked a similar question a few months ago **
Alternatively – is there a way I can get the file field form within the Java file – currently using – field name ‘gas_elec_data’
// Set up data source
chart.dataSource.url = “https://weareinteb.co.uk/wp-content/uploads/data/gas-elec-monthly.csv”;
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.