Support

Account

Home Forums Search Search Results for 'q'

Search Results for 'q'

reply

  • This has been requested in the past, the best way to bring this to the developers attention would be to contact them directly.

    I created custom filters for this a long time ago, but I do not know if they still work.

  • When editing a field group ACF loads the field group (post) then loads the fields (child posts) for the group. The field keys are part of the post and when editing a field group everything is dependent on the field key.

    When editing content, again, all editing is done by field key.

    The field name is really just a convenience for us to make it easier when coding or if you want to use a function like get_post_meta() to get the field value.

    Yes, if a value is save to a field for a post there will be an entry in the DB that relates that field to the post, but this is so that ACF can determine the unique field object since field names do not need to be unique.

  • Field keys are available in the admin when editing the field group, there is a checkbox under screen options.

    There really isn’t a reliable way to get the field key using PHP. Doing so requires that the field is already in the DB using get_field_object(‘FIELD NAME’). This will not work if the field does not already have a value.

  • I missed your question about 'meta_value' => ':' . $post_ID . ';'

    But I did look at this. Looking back to your data in the database, meta ID 5823 that meta query for post ID of 1 would match i:1;s:3:"857";. To be honest I can’t say how much this will affect things. I have with post IDs in the 6 digit range. In this case I’d only be safe looking for posts ID > 6.

    I also have clients with > 20 posts in a relationship field so I would only be safe if the post ID is greater than 20 or 30 or it would match the a:22 portion of the serialized array. You could potentially limit this by using I missed your question about 'meta_value' => 'i:' . $post_ID . ';' but that would not eliminate matching array indexes that match post ID values.

    I have never liked serialized data as a storage mechanism, but that’s WP.

  • Thanks for the answer 🙂

    Yes it is, I just checked

    Just move my fields into another group but same problem.
    This is the new code in footer.php:

    <?php $postID = get_the_ID();
    $parametres = get_field('parametres', $postID);
    $imageSliderOpacity = $parametres['opacity']; 
    $imageSliderSpeed = $parametres['slider_speed'];
    echo $imageSliderOpacity; // returning nothing
    echo $postID; // returning 260 it's the correct post ID
    ?>
    <script>
    	jQuery(document).ready(function($){
    		$('.slider').slick({
    		slidesToShow: 1,
    		slidesToScroll: 1,
    		dots: true,
    		arrows: false,
    		focusOnSelect: true,
    		fade: true,
    		autoplay: true
    		});	
    
    		var opacitySlider = '<?php echo $imageSliderOpacity ?>';
    		$('.slick-track').css( "opacity", opacitySlider);
    
    		
    	});
    
    	
    </script>

    In my template block the same values (without the postID in the get_field) are working, but not in the footer.
    Can we use may be the key block in the get_field rather than post id, if yes how ?

  • @hube2 also, what are you thoughts on my earlier query of using ':' . $post_ID . ';'?

    My plugin users in total have thousands of rows in the database that are effected by this so I’m not only trying to think how to stop it from happening in the future but also get the query to work for the customers that do have serialised arrays missing the quotes.

  • @hube2 okay thanks John, here is my code. Do I just wrap the update_post_meta value in quotes?

    /**
       * The relationship between the class and swimmer for confirmed classes.
       *
       * @since    1.0.0
       */  
      public function class_swimmer_relationship($value, $post_id, $field) {
    
    		// set the two fields that you want to create
    		// a two way relationship for
    		// these values can be the same field key
    		// if you are using a single relationship field
    		// on a single post type
    	
    		// the field key of one side of the relationship
    		$key_a = 'field_spal_swimmer_classes';
    		// the field key of the other side of the relationship
    		// as noted above, this can be the same as $key_a
    		$key_b = 'field_spal_class_swimmers';
    	
    		// figure out wich side we're doing and set up variables
    		// if the keys are the same above then this won't matter
    		// $key_a represents the field for the current posts
    		// and $key_b represents the field on related posts
    		if ($key_a != $field['key']) {
    			// this is side b, swap the value
    			$temp = $key_a;
    			$key_a = $key_b;
    			$key_b = $temp;
    		}
    	
    		// get both fields
    		// this gets them by using an acf function
    		// that can gets field objects based on field keys
    		// we may be getting the same field, but we don't care
    		$field_a = acf_get_field($key_a);
    		$field_b = acf_get_field($key_b);
    	
    		// set the field names to check
    		// for each post
    		$name_a = $field_a['name'];
    		$name_b = $field_b['name'];
    	
    		// get the old value from the current post
    		// compare it to the new value to see
    		// if anything needs to be updated
    		// use get_post_meta() to a avoid conflicts
    		$old_values = get_post_meta($post_id, $name_a, true);
    		// make sure that the value is an array
    		if (!is_array($old_values)) {
    			if (empty($old_values)) {
    				$old_values = array();
    			} else {
    				$old_values = array($old_values);
    			}
    		}
    		// set new values to $value
    		// we don't want to mess with $value
    		$new_values = $value;
    		// make sure that the value is an array
    		if (!is_array($new_values)) {
    			if (empty($new_values)) {
    				$new_values = array();
    			} else {
    				$new_values = array($new_values);
    			}
    		}
    	
    		// get differences
    		// array_diff returns an array of values from the first
    		// array that are not in the second array
    		// this gives us lists that need to be added
    		// or removed depending on which order we give
    		// the arrays in
    	
    		// this line is commented out, this line should be used when setting
    		// up this filter on a new site. getting values and updating values
    		// on every relationship will cause a performance issue you should
    		// only use the second line "$add = $new_values" when adding this
    		// filter to an existing site and then you should switch to the
    		// first line as soon as you get everything updated
    		// in either case if you have too many existing relationships
    		// checking end updated every one of them will more then likely
    		// cause your updates to time out.
    		//$add = array_diff($new_values, $old_values);
    		$add = $new_values;
    		$delete = array_diff($old_values, $new_values);
    	
    		// reorder the arrays to prevent possible invalid index errors
    		$add = array_values($add);
    		$delete = array_values($delete);
    	
    		if (!count($add) && !count($delete)) {
    			// there are no changes
    			// so there's nothing to do
    			return $value;
    		}
    	
    		// do deletes first
    		// loop through all of the posts that need to have
    		// the recipricol relationship removed
    		for ($i=0; $i<count($delete); $i++) {
    			$related_values = get_post_meta($delete[$i], $name_b, true);
    			if (!is_array($related_values)) {
    				if (empty($related_values)) {
    					$related_values = array();
    				} else {
    					$related_values = array($related_values);
    				}
    			}
    			// we use array_diff again
    			// this will remove the value without needing to loop
    			// through the array and find it
    			$related_values = array_diff($related_values, array($post_id));
    			// insert the new value
    			update_post_meta($delete[$i], $name_b, $related_values);
    			// insert the acf key reference, just in case
    			update_post_meta($delete[$i], '_'.$name_b, $key_b);
    		}
    	
    		// do additions, to add $post_id
    		for ($i=0; $i<count($add); $i++) {
    			$related_values = get_post_meta($add[$i], $name_b, true);
    			if (!is_array($related_values)) {
    				if (empty($related_values)) {
    					$related_values = array();
    				} else {
    					$related_values = array($related_values);
    				}
    			}
    			if (!in_array($post_id, $related_values)) {
    				// add new relationship if it does not exist
    				$related_values[] = $post_id;
    			}
    			// update value
    			update_post_meta($add[$i], $name_b, $related_values);
    			// insert the acf key reference, just in case
    			update_post_meta($add[$i], '_'.$name_b, $key_b);
    		}
    	
    		return $value;
    	
    	} // end function acf_reciprocal_relationship
    	
    	/**
       * The relationship between the class and swimmer for provisional classes.
       *
       * @since    1.0.0
       */  
    	function class_swimmer_provisional_relationship($value, $post_id, $field) {
    
    		// set the two fields that you want to create
    		// a two way relationship for
    		// these values can be the same field key
    		// if you are using a single relationship field
    		// on a single post type
    	
    		// the field key of one side of the relationship
    		$key_a = 'field_spal_swimmer_provisional_classes';
    		// the field key of the other side of the relationship
    		// as noted above, this can be the same as $key_a
    		$key_b = 'field_spal_class_provisional_swimmers';
    	
    		// figure out wich side we're doing and set up variables
    		// if the keys are the same above then this won't matter
    		// $key_a represents the field for the current posts
    		// and $key_b represents the field on related posts
    		if ($key_a != $field['key']) {
    			// this is side b, swap the value
    			$temp = $key_a;
    			$key_a = $key_b;
    			$key_b = $temp;
    		}
    	
    		// get both fields
    		// this gets them by using an acf function
    		// that can gets field objects based on field keys
    		// we may be getting the same field, but we don't care
    		$field_a = acf_get_field($key_a);
    		$field_b = acf_get_field($key_b);
    	
    		// set the field names to check
    		// for each post
    		$name_a = $field_a['name'];
    		$name_b = $field_b['name'];
    	
    		// get the old value from the current post
    		// compare it to the new value to see
    		// if anything needs to be updated
    		// use get_post_meta() to a avoid conflicts
    		$old_values = get_post_meta($post_id, $name_a, true);
    		// make sure that the value is an array
    		if (!is_array($old_values)) {
    			if (empty($old_values)) {
    				$old_values = array();
    			} else {
    				$old_values = array($old_values);
    			}
    		}
    		// set new values to $value
    		// we don't want to mess with $value
    		$new_values = $value;
    		// make sure that the value is an array
    		if (!is_array($new_values)) {
    			if (empty($new_values)) {
    				$new_values = array();
    			} else {
    				$new_values = array($new_values);
    			}
    		}
    	
    		// get differences
    		// array_diff returns an array of values from the first
    		// array that are not in the second array
    		// this gives us lists that need to be added
    		// or removed depending on which order we give
    		// the arrays in
    	
    		// this line is commented out, this line should be used when setting
    		// up this filter on a new site. getting values and updating values
    		// on every relationship will cause a performance issue you should
    		// only use the second line "$add = $new_values" when adding this
    		// filter to an existing site and then you should switch to the
    		// first line as soon as you get everything updated
    		// in either case if you have too many existing relationships
    		// checking end updated every one of them will more then likely
    		// cause your updates to time out.
    		//$add = array_diff($new_values, $old_values);
    		$add = $new_values;
    		$delete = array_diff($old_values, $new_values);
    	
    		// reorder the arrays to prevent possible invalid index errors
    		$add = array_values($add);
    		$delete = array_values($delete);
    	
    		if (!count($add) && !count($delete)) {
    			// there are no changes
    			// so there's nothing to do
    			return $value;
    		}
    	
    		// do deletes first
    		// loop through all of the posts that need to have
    		// the recipricol relationship removed
    		for ($i=0; $i<count($delete); $i++) {
    			$related_values = get_post_meta($delete[$i], $name_b, true);
    			if (!is_array($related_values)) {
    				if (empty($related_values)) {
    					$related_values = array();
    				} else {
    					$related_values = array($related_values);
    				}
    			}
    			// we use array_diff again
    			// this will remove the value without needing to loop
    			// through the array and find it
    			$related_values = array_diff($related_values, array($post_id));
    			// insert the new value
    			update_post_meta($delete[$i], $name_b, $related_values);
    			// insert the acf key reference, just in case
    			update_post_meta($delete[$i], '_'.$name_b, $key_b);
    		}
    	
    		// do additions, to add $post_id
    		for ($i=0; $i<count($add); $i++) {
    			$related_values = get_post_meta($add[$i], $name_b, true);
    			if (!is_array($related_values)) {
    				if (empty($related_values)) {
    					$related_values = array();
    				} else {
    					$related_values = array($related_values);
    				}
    			}
    			if (!in_array($post_id, $related_values)) {
    				// add new relationship if it does not exist
    				$related_values[] = $post_id;
    			}
    			// update value
    			update_post_meta($add[$i], $name_b, $related_values);
    			// insert the acf key reference, just in case
    			update_post_meta($add[$i], '_'.$name_b, $key_b);
    		}
    	
    		return $value;
    	
    	} // end function acf_reciprocal_relationship
  • @hube2 Thanks for the very quick response John, I appreciate your time.

    Okay, so you are saying that every time I use update_field() or update_post_meta() I need to pass the IDs as strings and then every time I use get_field() or get_post_meta() I should convert the values to integers?

    If so, I’ll go through and make this change in my bidirectional relationship code and also whenever I interact with either field elsewhere in my plugin.

  • I also have this issue and believe it to be a bug with ACF. I have a created a bidirectional relationship between two post types using two relationship fields. I have been querying these fields using WP_Query as you would normally wrapping the value (in this case a post ID) with double quotes.

    However customers have told me that the query is not selecting the correct number of posts. When I looked into this I noticed that sometimes the array values are not being saved with double quotes within the serialised array. See screenshot of phpmyadmin.

    Any ideas on what could be causing this would be great, it’s bringing me great stress and frustration at the moment.

    Also, @hube2 as a work around I wondered if I could query both '"' . $post_ID . '"' as you would normally but also ':' . $post_ID . ';' or am I likely to run into issues with selecting the wrong posts due to the numbers in the serialised array?

  • ok, trying again. This doesn’t display empty buttons… but seems to not display anything at all.

    
    							$buttons = get_fields();
    							if( $buttons ) { 
    								echo
    									'<div class="et_pb_button_module_wrapper et_pb_button_3_tb_body_wrapper">';
    									foreach( $buttons as $but_name => $but_value ) {
    										if( !empty( $but_value ) ) { 
    											$but_label = get_field_object($but_name);
    											echo
    												'<a class="vendor-link" title="',$but_label['label'] ,'" href="', $but_value ,'">', $but_label['label'] ,'</a>';
    										}
    									}
    								echo
    									'</div>';
    							}
    

    Am I missing something obvious?

  • empty depends on what the value is.

    https://www.php.net/manual/en/language.types.boolean.php#language.types.boolean.casting.

    The values in the field object are the values before acf has formatted them (raw value), a true/false field set to false could be a string value "0" which is not empty.

    Try get_fields() instead of get_field_objects() and then use get_field_object() for non empty fields to get the field label.

  • First question is, is the taxonomy field set up to save and load terms?

  • Alright, I came up with something if anybody stumbles upon this topic and has the same problem. Basically, I’m hiding the fields I want to disable using CSS when they’re disabled in the main options page.

    Here goes in functions.php:

    // ACF admin CSS
    function acf_css() { ?>
        <style type="text/css">
    
        /* checks if option is checked on main option page*/
        <?php if (!get_field('papier', 'options')) { ?>
            
            /* hides option in admin */
            th[data-name="cache_papier"], td[data-name="cache_papier"] {
                display: none;
            }
    
        <?php } ?>
    
        </style>
        <?php
    }
    
    // adds the CSS above to ACF's admin
    add_action('acf/input/admin_head', 'acf_css');

    Of course you need to replace get_field(‘papier’, ‘options’) for the field that disables the other, as well as data-name=”cache_papier” for the field to hide, with your own names if you do it this way. I hope that’s clear 🙂

    It’s not perfect since it’s not actually disabled, it’s just hidden, but it’s better than nothing.

  • Thanks @hube2 .

    Any tips about where in the DB to look? I remember trying to find ACF fields in the past and got lost looking, also in the documentation.

    Maybe this helps… after another round of testing (with debugging turned on) I see these errors:

    [08-Sep-2021 13:57:35 UTC] PHP Notice: Trying to get property ‘ID’ of non-object in /www/htdocs/w01a2ea9/sanktoberholz.de/wp-includes/class-wp-query.php on line 4044
    [08-Sep-2021 13:57:35 UTC] PHP Notice: Trying to get property ‘post_title’ of non-object in /www/htdocs/w01a2ea9/sanktoberholz.de/wp-includes/class-wp-query.php on line 4046
    [08-Sep-2021 13:57:35 UTC] PHP Notice: Trying to get property ‘post_name’ of non-object in /www/htdocs/w01a2ea9/sanktoberholz.de/wp-includes/class-wp-query.php on line 4048

    Looking at class-wp-query.php this appears (to my limited understanding) to be the code referenced when making queries in template files. Could this help explain why the post sort of “disappears” to the post query, although it is still visible (and marked published in the backend)?

  • Posting my workaround here in case it’s of use to anyone stumbling across this issue. First I use acf/load_field to move the instructions to a new item in the array and empty the instructions item, checking first to see if this is a child of my repeater field. Then I filter the output of all fields using acf/render_field to see if there’s an ‘instructions_below’ item in the array and output it after the input.

    add_filter( 'acf/load_field', 'tweak_repeater_instructions' );
    function tweak_repeater_instructions( $field ) {
    
    	if( 'group_123xyz' !== $field['parent'] ) return $field;
    
    	$field['instructions_below'] = $field['instructions'];
    	$field['instructions'] = '';
    
    	return $field;
    }
    
    add_filter( 'acf/render_field', 'position_repeater_instructions' );
    function position_repeater_instructions( $field ) {
    
    	if( ! isset( $field['instructions_below'] ) ) return;
    
    	echo '<p class="description">'.$field['instructions_below'].'</p>';
    }
  • While you can select draft posts in the admin they should not appear on the front end, usually, it can happen if you return post IDs.

    ACF has a filter that can be used to modify the values that can be selected in the admin, acf/fields/relationship/query

  • Try

    
    $post_id = 0;
    $object = get_queried_object();
    if (is_a($object, 'WP_Post')) {
      $post_id = $object->ID;
    }
    the_field('header_code', $post_id);
    
  • I’d want to utilize ACF to produce unique data in the head> and footer for each page.

  • When a new post is created doing the sync, what happens?

    First round is that it creates a draft, which is normal for the plugin.

    When an existing post is updated and fields that did not have values are populated, what happens?

    I can see the new text when viewing the post in the back end, but it does not become visible on the front-end until manually clicking publish.

    When an existing post is updated and fields that did not have values are populated, what happens?

    Same as above.

    What type of fields are they?

    Text and text area. Not sub-fields.

    Just did another test… a post listed as being published:
    https://sanktoberholz.de/wp-content/uploads/2021/09/Screen-Shot-2021-09-06-at-4.24.39-PM.png

    And yet isn’t visible via post query until I hit Update again. Here’s the post query that I’m using, which shows a meta query for displaying only those that have been marked as a specific category (in Salesforce, not a WP category) and a checkbox for whether to make the post visible. (Its for a rudimentary inventory management process.)

    
    			<?php
    				$args = array( 'posts_per_page' => -1,  'post_type' => 'assets',
    
    				'meta_query'	=> array(
    						'relation'		=> 'AND',
    						array(
    							'key'		=> 'area',
    							'value'		=> '["MITTE"]',
    							'compare'	=> '='
    						),
    						array(
    							'key'		=> 'show_on_website',
    							'value'		=> 1,
    							'compare'	=> '='
    						)
    					)
    				);
    
    				$myposts = get_posts( $args );
    
    				if($myposts):
    
    				foreach ( $myposts as $post ) : setup_postdata( $post );
    
     			?>
  • Adding that I want to use ACF to output unique values for each page in the <head> and footer.

  • See the section on this page about Dynamic $_GET parameters

    This is the direction I would go in. This would allow altering the existing archive page to only show pages where the alternate language field has a value

    meta_key => your field key
    meta_value => ”
    meta_compare => ‘!=’

  • I do not know how your site was built. I’m guessing that the developer used flexible content fields for the “components”. If the theme templates do not already add id attributes to the HTML then you would need to alter the templates to add this and quite possibly add some type of field to each of the flexible content layouts to allow entering or allowing unique ID values for each layout.

  • Thanks for tip!

    I also found the following resource quite helpful: https://www.advancedcustomfields.com/resources/acf_add_options_page/

  • Hi @james

    Could you please tell me how, in your previous example, I can add the following code, in order for me to create a new post ?

    'new_post' => array(
           'post_type' => 'questionnaire_ws',
           'post_status' => 'publish',
           'post_title' => 'lorem test',
    ),

    My goal is to create a post type “questionnaire_ws” with multiple acf groups 🙂

  • Anchor IS indirectly mentioned in the documentation: the documentation states:

    supports
    (Array) (Optional) An array of features to support. All properties from the JavaScript block supports documentation may be used. The following options are supported:

    If you read the linked documentation at https://developer.wordpress.org/block-editor/developers/block-api/block-supports/ — anchor is the first parameter on the list.

Viewing 25 results - 5,301 through 5,325 (of 21,337 total)