Support

Account

Forum Replies Created

  • Hi all!

    I’m trying to use the code with a telephone field called ‘phone’ but it doesn’t work. This is the code I insert in functions php.
    What I’m doing wrong?

    Thx.

    add_filter('acf/validate_value/name='.$field_name, 'acf_unique_value_field', 10, 4);
      
      function acf_unique_value_field($valid, $value, $field, $input) {
        if (!$valid || (!isset($_POST['post_ID']) && !isset($_POST['post_id']))) {
          return $valid;
        }
        if (isset($_POST['post_ID'])) {
          $post_id = intval($_POST['post_ID']);
        } else {
          $post_id = intval($_POST['post_id']);
        }
        if (!$post_id) {
          return $valid;
        }
        $post_type = get_post_type($post_id);
        $field_name = $field['phone'];
        $args = array(
          'post_type' => $post_type,
          'post_status' => 'publish, draft, trash',
          'post__not_in' => array($post_id),
          'meta_query' => array(
            array(
              'key' => $field_name,
              'value' => $value
            )
          )
        );
        $query = new WP_Query($args);
        if (count($query->posts)){
          return 'This Value is not Unique. Please enter a unique '.$field['label'];
        }
        return true;
      }
  • Thanks John. Would there be any temporary code inserted as a plugin that when activated opened and saved all draft products? It’s a waste of time to have to open and save one by one.

  • Hi John.
    I use a plugin called Content Crawler that copy products from other stores and publish it on my site. There’re many options, for example how to get titles, content, images, etc, and one of this options is to add post meta key, that I’m using with the ACF plugin. My problem is that some fields are only displayed when I save and publish the post, but no if I publish directly after crawling. For example:

    Example 1: Select field called “games” is displayed if the post is published directly with this code:

    <?php
    if( get_field('games') == 'option1' ): ?>
    	<a href="https://www.site.com/games/option1/">Option 1</a>
    <?php endif; ?>

    Example 2: Select field called “company” isn’t displayed if the post is published directly with this code:

    <?php 
    $field = get_field_object('company');
    $value = get_field('company');
    $label = $field['choices'][ $value ];
    echo $label;
     ?>

    Example 3: Text field called “source” isn’t displayed if the post is published directly with this code:
    <?php echo the_field('sources'); ?>

    In summary, I have draft products created with a crawling plugin that contains meta-keys, and this meta-keys are only displayed when I open each draft product one by one and publish it, If I publish directly without open it or select multiple draft products and I the bulk option to publish it, the fields aren’t displayed. I hope this explanation will be more understandable, sorry for my bad English.

  • I use this code with an oEmbed field, only display the video is there’s a url 😉

    <?php 
    if (get_field('my_video')) {
    ?>
    <div class="media_embed_youtube"><?php the_field('my_video''); ?></div>
        <?php 
      } // end if value
    ?>
  • It works inverting the terms 🙂

    add_action('acf/save_post', 'move_image_to_post_thumbnail');
    function move_image_to_post_thumbnail($post_id) {
      if (get_field('_thumbnail_id', $post_id, false)) {
        // get unformatted value
        // we did it above too, no point formatting the value for this
        $image_id = get_field('_thumbnail_id', $post_id, false);
        update_post_meta($post_id, 'image', $image_id);
      }
    }
  • Sorry John, but the code not working 🙁

  • Would it be possible to use the same code with some modifications to get the value of a field and use it as the meta-key _yoast_wpseo_focuskw? Something like this:

    add_action('acf/save_post', 'automatic_keywords');
    function automatic_yoast_keywords($post_id) {
      if (get_field('_yoast_wpseo_focuskw', $post_id, false)) {
        // get unformatted value
        // we did it above too, no point formatting the value for this
        $image_id = get_field('_yoast_wpseo_focuskw', $post_id, false);
        update_post_meta($post_id, 'name', $meta);
      }
    }
  • Yeah!! Works perfectly. Thanks a lot John.

  • Solved reading other answers.

    A new question, how can display various field labels in different lines? I don’t know how to use echo implode function 🙁

  • Well, I fixed it with whit code but…

    $field = get_field_object('field_name');
    $value = get_field('field_name');
    $label = $field['choices'][ $value ];
    echo $label;

    This code don’t work with checkbox and multiple values. Anyone can help me?

Viewing 12 posts - 1 through 12 (of 12 total)