Support

Account

Home Forums Search Search Results for 'q'

Search Results for 'q'

reply

  • @ryancb you never really answered my questions.

    Turn in debugging so that you can see what error is produced instead of getting WOD. Then let me know what the error is.

    What do you have this field set to return? “Posts Object” or “Post ID”?
    Does this field allow multiple selections?

    
    $option_a = get_field('my_option_field', 'option'); //post object , array of post ids
    
  • Solved it with this so only main query is affected.

    if( !$query->is_main_query() ) return;

  • Add this line:

    if( !$query->is_main_query() ) return;

    at the beginning of you filter, solved it for me

  • @princeofabyss

    Do you have to show the parent posts in the field? If not then I would filter the query using acf/fields/post_object/query by adding $args['post_parent__in'] = array(0);

    If you must show the parents and have them disabled in the field, Unfortunately, there are no hooks that will let you filter the json response from the server. In this case what you would have to do is do a query to get a list of post IDs that have parent = 0 and output those as a JS variable so that you can compare the values returned from the request to determine disabled or not.

    
    add_action('acf/input/admin_footer', 'my_acf_input_admin_footer');
    function my_acf_input_admin_footer() {
        $args = array(
          'post_type' => 'your post type',
          'posts_per_page' => -1,
          'post_parent' => 0,
          'fields' => 'ids'
        );
        $parents = new WP_Query($args);
        ?>
        <script type = "text/javascript">
        (function($) {
            var parents = <?php echo json_encode($parents); ?>;
            // JS here
            acf.add_filter('select2_ajax_results', function(json, params, instance) {
                $.each($(json.results), function() {
    
                    // compare returned id with parents array here
    
                    $(this).prop("disabled", true);
                });
                return json;
            });
        })(jQuery);
        </script>
        <?php
    }
    
  • Add this line:

    if( !$query->is_main_query() ) return;

    at the beginning of you filter, solved it for me

  • @radgh (or anyone else) could you help me with my situation please, which is even simpler than yours but still I haven’t been able to fully figure it out…

    So, in my case I only use one Post Object (cousin of Relationship), that pulls data from a hierarchical (parent-child) CPT. Very simple and stock up to here. Ah, the Post Object is within a Repeater, so that the admin will be able to add rows of Post Objects, but that doesn’t affect the case anyway.

    The problem is I want to prevent the admin who will use the system from choosing top categories (where parent=0), but rather choose only child categories, by disabling the top categories within the select2.

    At this point let me tell you, in case you don’t already know, that it’s a select2 stock functionality to disable specific options by adding a property disabled:true for that option.

    So, I did a lot of research and found that the only JS filter that is being triggered every time the select2 gets populated is acf.add_filter(‘select2_ajax_results’, function( json, params, instance ) (https://www.advancedcustomfields.com/resources/javascript-api/#filters-select2_ajax_results), where json param holds only the id and text properties of the results.

    For testing purposes I added a third param disabled:true to each option, and indeed all options where greyed out…

    add_action('acf/input/admin_footer', 'my_acf_input_admin_footer');
    function my_acf_input_admin_footer() {
        ?>
        <script type = "text/javascript">
        (function($) {
            // JS here
            acf.add_filter('select2_ajax_results', function(json, params, instance) {
                $.each($(json.results), function() {
                    $(this).prop("disabled", true);
                });
                return json;
            });
        })(jQuery);
        </script>
        <?php
    }

    The problem is I don’t know at the time select2_ajax_results filter gets triggered which options are top categories and which are children. So, I need somehow to add a third param, like top:true, to json at an earlier stage. But what filter would that be? Probably it should be server side, but what exactly filter?

    The following screenshot was taken with the test code above in place, which disabled ALL options…

    Screenshot

    Any insight would be greatly appreciated.

  • Okay, I think your having a caching issue. It appears that updating the sub field is not clearing either the ACF cache for the field or the WP meta cache for the field.

    Going to be honest, I don’t know if there is a way to update a sub field and then immediacy get the value that was just saved during the same request (page load). Generally updating of fields and reloading of fields happens in separate requests. I’ve just taken a look at the ACF code and I don’t see any apparent way to clear the current value old value from the cache.

    You might want to ask the developer about this one to see if he has any advice https://www.advancedcustomfields.com/contact/

  • I tried the following and nothing seems to resolve it:
    – hard refresh
    – testing in new incognito/private session (Chrome, my default browser)
    – testing in different/unique browser (Firefox)
    But maybe I’m overlooking something so I’m hoping the other users can test your fix as well.

  • @mrrrlou Can you confirm your browser is not loading cached assets? Please perform a hard-refresh or try a unique browser. Thanks 🙏

  • I’m not sure if we’re on the same page. My problem is specificially with ACF Repeater fields. And yes, the fields are there & have content, they are output on the page. But I cannot count them with “count()”.

    The repeater code as per ACF example:

    <?php
    
    // Check rows exists.
    if( have_rows('repeater_field_name') ):
    
        // Loop through rows.
        while( have_rows('repeater_field_name') ) : the_row();
    
            // Load sub field value.
            $sub_value = get_sub_field('sub_field');
            // Do something...
    
        // End loop.
        endwhile;
    
    // No value.
    else :
        // Do something...
    endif;

    Now if I add count “$count = count(get_field(‘repeater_field_name’) );” , it doesn’t work:

    <?php
    
    // Check rows exists.
    if( have_rows('repeater_field_name') ):
    
    $count = count(get_field('repeater_field_name') );
    
        // Loop through rows.
        while( have_rows('repeater_field_name') ) : the_row();
    
            // Load sub field value.
            $sub_value = get_sub_field('sub_field');
            // Do something...
    
        // End loop.
        endwhile;
    
    // No value.
    else :
        // Do something...
    endif;

    My question – what code do I need to count how many repeats I have?

  • Hello,
    this is console output in page edit with wysiwyg editor custom fields:

    
    This page uses the non standard property “zoom”. Consider using calc() in the relevant property values, or using “transform” along with “transform-origin: 0 0”. post.php
    
    JQMIGRATE: Migrate is installed, version 3.3.2 load-scripts.php:5:709
    
    MouseEvent.mozPressure is deprecated. Use PointerEvent.pressure instead. 2 wp-tinymce.js:3:8856
    

    Nothing else appears there even during custom fields editing.

  • Hi all đź‘‹. Thank you for your patience whilst we tested and debugged the issue.

    We have successfully identified this issue as a regression in ACF PRO 5.9.4 which caused ACF Blocks to be registered *after* the WP editor instance was initialized. This explains why new blocks could be added whilst existing blocks did not appear on page load.

    Whilst we continue testing of the fix, please find an updated JS file to copy and paste into the pro/assets/js/ folder within ACF PRO. This file contains the related patch and should solve the problem 100% – please comment bellow with your results. The quicker we can get positive feedback from you, the quicker we can release an update 🙌.

    Dropbox link will be available for 7 days: https://www.dropbox.com/t/gTRoawP1TRGUgXT3

  • <input type="number"> does not allow thousands separator.

  • The code I supplied will do what you are looking for, you need to add fields and then change the code to match your field names

    
    add_filter('the_title', 'wps_sponsored', 10, 2);
    function wps_sponsored($title, $post_id) {
      $sub_title = get_field('sub-title-field-name', $post_id);
      if ($sub_title) {
        $url = get_field('sub-title-url-field-name', $post_id);
        if ($url) {
          $sub_title = '<a href="'.$url.'">'.$sub_title.'</a>';
        }
        $sub_title = '<span class="subtitle">'.$sub_title.'</span>';
        $title = $sub_title.$title;
      }
      return $title;
    }
    
  • The problem with filtering the_title is that your sub title will be inside of whatever heading tag the theme is using (h1, h2, h3, whatever)

    
    add_filter('the_title', 'wps_sponsored', 10, 2);
    function wps_sponsored($title, $post_id) {
      $sub_title = get_field('sub-title-field-name', $post_id);
      if ($sub_title) {
        $url = get_field('sub-title-url-field-name, $post_id);
        if ($url) {
          $sub_title = '<a href="'.$url.'">'.$sub_title.'</a>';
        }
        $sub_title = '<span class="subtitle">'.$sub_title.'</span>';
        $title = $sub_title.$title;
      }
      return $title;
    }
    
  • I think you’ve missed my point.

    No, the fields do not appear in the admin because they have been moved to PHP.

    I think your conditional logic is not correct, but I cannot tell from what you’ve provided why it is incorrect.

    You are going to create a temporary field group.
    Then you are going to create a field in this group that exactly mimics the field you are referring to here

    when a choice menu selected this field should appear

    Including all of the values and settings for this field.

    Then you are going to create the field you want conditional on this field and set it up so that it works properly.

    Then you are going to export this group to php and you’re going to copy your new field into the PHP file where you want the new field to appear and you are going to alter the field key in the conditional logic of your new field to match the old field that you want the new field to be conditional on.

    Once you get all of this done and your new field is working then you will delete the temporary group that you created.

  • Ok, I figured out that this could be achieved by resetting the value to false (no) after a product is published. That way the duplicate is loaded with No selected.

    This code works, but please let me know if I can improve upon it. Thanks.

    function reset_product_readiness_after_publish( $post_id ){  
        $value = "0";
        update_field('field_30705432c7518', $value, $post_id);
    }
    add_action('acf/save_post', 'reset_product_readiness_after_publish', 20);
  • I solved it, for future references: the theme was using a custom compiled version of jquery with god knows what plugins embedded in it, removed and replaced with a clean version everything works fine now.

  • Thanks, John. I amended the function accordingly and this was what I got.

    
    function cpt_date_orderby( $query ) {
     if (!$query->is_main_qieru()) {
      return;
     }
     $orderby = $query->get( 'orderby' );
     if( 'date' == $orderby ) {
        $query->set('meta_key', 'date');
        $query->set('orderby', 'meta_value_num');
     }
    }
    add_action( 'pre_get_posts', 'cpt_date_orderby' );
    

    Did I get it wrong?

  • Hi John, thank you for your advice.
    For some reason filter is not working when it’s added to the page. Definitely works when add_action is being added to the functions.php, but it’s preventing me from seeing any drafts on the site.

    
    <?php
    if (get_field('alternative_method')):
    	// add filter to only get published posts
    	add_action('pre_get_posts', 'acf_field_only_published_posts', 10, 1);
    	// get field
      $alt_posts = get_field('alternative_method');
    	// remove the filter so it does not affect other queries
    	remove_filter('pre_get_posts', 'acf_field_only_published_posts');
    
    	if (!empty($alt_posts)): ?>
    
    	<section id="alt-method">
          <div class="text full">
            <h2>Alternative methods</h2>
          </div>
    
          <div class="recipes-list item-listing">
              <?php foreach( $alt_posts as $post ):
                  setup_postdata($post); ?>
                  <?php if ( get_post_status() == 'publish' ) :?>
                     <?php get_template_part( 'template-parts/content-recipe-card', get_post_format() ); ?>
                  <?php endif;?>
              <?php endforeach; ?>
              <?php
              wp_reset_postdata(); ?>
    
          </div>
        </section>
    
    	<?php endif; ?>
    <?php endif; ?>
    
  • This is what I would do if I needed to do this. (after doing a little research)

    I would use a URL field for the video and not an oembed field.
    I would add an additional file field to upload the captions

    In my PHP code I would do

    
    <?php 
      $shortcode = '[video src="'.get_field('video_url_field').'"]';
      $shortcode .= '<track srclang="en" label="English" kind="subtitles" src="'.get_field('captions-file-field').'" default>';
      $shortcode .= '[/video]';
      echo do_shortcode($shortcode);
    ?>
    

    See this https://stphnwlkr.co/web/adding-video-to-wordpress/

    It might be possible to use an oembed field if you get the value without formatting so that ACF does not do the formatting of the field.

  • 
    <?php 
    	// add to function.php
    	function acf_field_only_published_posts($query) {
    		$query->set('post_status', 'publish');
    	}
    ?>
    
    
    if (get_field('alternative_method')) {
    	// add filter to only get published posts
    	add_action('pre_get_posts', 'acf_field_only_published_posts');
    	// get field
      $alt_posts = get_field('alternative_method');
    	// remove the filter so it does not affect other queries
    	remove_filter('pre_get_posts', 'acf_field_only_published_posts');
    	
    	if (!empty($alt_posts)) {
    		
    		// the rest of code to output section here
    		
    	} // end if !empty($alt_posts)
    } // end if get_field('alternative_method')
    
  • @hkmakchui the issue with your filter is that it is running every time a query is done, and this includes queries that ACF runs to get the results for fields.

    You should have conditionals at the top of every pre_get_posts filter so that it only runs when you need it to.

    For example: If you don’t want it to run in the admin

    
    if (is_admin()) {
      return;
    }
    

    or if you only want it to run on the main query

    
    if (!$query->is_main_qieru()) {
      return;
    }
    
  • You cannot just rename a field, as you’ve found and there isn’t a simple way to do this.

    There are two fields in the database that are associated with each field, not including group, repeater and flex fields.

    The meta keys for these fields are $field_name and '_'.$field_name. The first one contains the field value and the second one contains the field key index. All of these meta keys need to be altered to the new field name. A warning, if this field name is not unique site wide then trying to do this in the database will result it you loosing data for other fields with the same name.

    Since the field name is not seen, if you’re goal is to change the label so it makes more sense I would just change the label and leave the field name alone. Outside of dev for use in ACF function the name of a field really does not matter.

  • Hi, ulascansadi. I ran into this problem and successfully got it solved today. Please check whether you used any function where ‘date’ is mentioned. In my case, one of my custom fields is called ‘date’ and then I wrote a function to make the column of this custom field sortable, which caused the problem.

    function cpt_date_orderby( $query ) {
     $orderby = $query->get( 'orderby' );
     if( 'date' == $orderby ) {
        $query->set('meta_key', 'date');
        $query->set('orderby', 'meta_value_num');
     }
    }
    add_action( 'pre_get_posts', 'cpt_date_orderby' );

    The problem lies in line 3, where ‘date’ is value for the parameter. I replaced ‘date’ with a different string, like ‘date1’, and that’s why I solved the problem.

    It seems ‘date’ is a reserved word in WordPress, and should be used carefully.

Viewing 25 results - 5,976 through 6,000 (of 21,330 total)