Support

Account

Forum Replies Created

  • Hi there,

    Here’s an example that allows you to set defaults in a repeater dynamically, for when there is no current value:
    .. change the ‘key’ value in the filter call… you can expose the ‘key’ when editing your fields by checking the box in Screen Options (top right)

    
    <?php
    function my_default_value_subs($value, $post_id, $field) {
      if ($value === false) {
        $value = array(
          array(
            'field_58984ddb5b8cc' => 'My title',
            'field_58985045abac0' => 'My content',
          ),
          array(
            'field_58984ddb5b8cc' => 'Another title for a content-less row',
          ),
        );
      }
      return $value;
    }
    add_filter('acf/load_value/key=key_58c7ee3a37aee', 'my_default_value_subs', 10, 3);
    ?>
    

    Now… to make it conditional based on a page template.. you could use the function:

    get_page_template_slug( get_the_ID() );

    This returns the ‘slug’ of the page template…

    So… they would choose the template first… save the post (as Draft or whatever), and then the defaults would be set if you modified the above code to be like:

    
    <?php
    function my_default_value_subs($value, $post_id, $field) {
      if ($value === false) {
        switch (get_page_template_slug( get_the_ID() )) {
          case 'about.php':
            $value = array(
              array(
                'field_58984ddb5b8cc' => 'My title',
                'field_58985045abac0' => 'My content',
              ),
              array(
                'field_58984ddb5b8cc' => 'Another title for a content-less row',
              ),
            );
            break;
          case 'other-template.php':
            $value = array(
              array(
                'field_58984ddb5b8cc' => 'My title',
                'field_58985045abac0' => 'My content',
              ),
              array(
                'field_58984ddb5b8cc' => 'Another title for a content-less row',
              ),
            );
            break;
          case 'other-template-slug.php':
            $value = array(
              array(
                'field_58984ddb5b8cc' => 'My title',
                'field_58985045abac0' => 'My content',
              ),
              array(
                'field_58984ddb5b8cc' => 'Another title for a content-less row',
              ),
            );
            break;
        }
      }
      return $value;
    }
    add_filter('acf/load_value/key=key_58c7ee3a37aee', 'my_default_value_subs', 10, 3);
    ?>
    

    Your $value would be different depending on the ‘type’ of subfields, but this should get you pointed in the right direction I hope!

    Let me know,
    Keith

  • Ahhh, I see.. I assumed the other way around because it’s a 1 to many from hospitals to branches…

    I can’t think of the logic then.. hmmm…

    The goal is to find “recommended” branches right? And a branch is considered “recommended” if a “recommended” hospital is attached to it? Am I right so far?

    If so… and you are connecting them in the way that you are… choosing 1 hospital for each branch… then….

    I would:

    1) Create an array of all hospital IDs that *are* recommended. Hint: You can use WP_Query to return *just* the IDs.

    Here’s the info from this link:

    /// Return Fields Parameter
    Set return values.

    fields (string) – Which fields to return. All fields are returned by default. There are two other options:
    ‘ids’ – Return an array of post IDs.
    ‘id=>parent’ – Return an array of stdClass objects with ID and post_parent properties.
    Passing anything else will return all fields (default) – an array of post objects ///

    2) Then… Query branches that contain those hospital IDs… you can do a meta query such as:

    
    'meta_query' => array(
      array(
        'key' => 'hospital',
        'value' => array( 3, 4 ), // array of hospitals IDs from 1st query
        'compare' => 'IN',
      ),
    

    Cool?

  • Are you using the plugin for the “Enhanced Message Field”? It’s an add-on for ACF that allows for PHP code.

  • @tobiasheuken

    I have a quick dirty solution for you, if I am understanding the issue correctly. Since this is just temporary, this should be an OK solution for this scenario.

    Now… I am assuming that:

    1) You have a repeater of data
    2) You don’t mind adding another field within that repeater to display the filename

    Also… my brain could have overthought this issue from the beginning. There *may* be a super simple way to accomplish this. However, it only took me a few minutes, so.. here goes:

    Step 1: Install the ACF Enhanced Message Field plugin, so that we can add a field to your repeater that allows for PHP code. Thanks Dreb Bits!

    Step 2: Add the following code into your newly created Enhanced Message Field in your repeater:

    
    <?php
    global $my_row_number;
    if ( !$my_row_number ) { $my_row_number = 0; }
    $rows = get_field('repeater-name');
    $specific_row = $rows[$my_row_number];
    $sub_field_value = $specific_row['image-field-name'];
    echo basename($sub_field_value['url']); // echos *just* the filename for the image
    $my_row_number++;
    ?>
    

    Sure, it’s a weird solution, but assuming I understand your issue correctly, it works like a charm 🙂

    Take care to change the Repeater field name, as well as the Sub field name when using the above code.

    Hope this works! Let me know.
    Keith

  • It’s may be just a matter of specifying the post type in the Query.

    It sounds like you know this, but remember… the below code naturally has values that need to be replaced:

    a) ‘my_custom_post_type’ = the ‘slug’ of your post type (3 places… plus the function name twice if desired)
    b) ‘date’ = the ‘name’ of your ACF Date field (1 place)

    
    <?php
    function mycustomposttype_pre_get_posts() {
      if ( is_admin() ) { return $query; }
      if ( is_post_type_archive( 'my_custom_post_type' )  && !empty( $query->query['post_type']  == 'my_custom_post_type' ) {
        $query->set( 'post_type', 'my_custom_post_type' );
        $query->set( 'meta_key', 'date' );
        $query->set( 'orderby', 'meta_value_num' );
        $query->set( 'order', 'DESC' );
      }
      return $query;
    }
    add_action('pre_get_posts','mycustomposttype_pre_get_posts');
    ?>
    

    You may need to adjust ‘orderby’. I got ‘num’ from the ACF docs about querying date fields.

  • The code you are using looks good.. in terms of what I saw in the Woo docs.

    Now… your callback function… that’s where you are having troubles displaying ACF Repeater field data?

    Have you tried passing the post ID to the Repeater, which would mean adding:

    global $post;

    then…

    get_field( 'wh_info', $post->ID );

  • Sure, I will cobble some code together below. May need some tweaks 🙂

    
    $args = array(
      'post_type' => 'hospital',
      'posts_per_page' => 10, // you could use -1 for 'all'
      'meta_query' => array(
        array(
          'key' => 'recommended',
          'value' => TRUE,
          'compare' => '=',
        ),
      ),
    );
    $the_query = new WP_Query( $args );
    
    $all_branch_ids = array();
    if ( $the_query->have_posts() ) {
      while ( $the_query->have_posts() ) {
        $the_query->the_post();
          $all_branch_ids = array_merge( $all_branch_ids, get_field('branch_id_field') ); // this *should* work (untested)
      }
      wp_reset_postdata();
    }
    // Essentially, with the above, we are looping through 'hospital' records and appending the array of "branch ids" (from the relationship field) to an array of "all" bracnch ids. This *could* potentially introduce duplicates, for which we address below.
    Of course, we are only getting "branch ids" that are *recommended*, because the Hospital Query only returns recommended hospitals.
    
    $all_branch_ids = array_unique($all_branch_ids); // remove duplicates
    
    // Now, we can query branches using: post__in
    
    $args = array(
      'post_type' => 'branch_clinic',
      'posts_per_page' => -1, // use -1 for 'all'
      'post__in' => $all_branch_ids // has to be array like: array( 2, 5, 12, 14, 20 )
    );
    $the_query = new WP_Query( $args );
    

    Note that empty arrays passed to ‘post__in’ can have undesired results, and there is a special note to be made about ‘sticky posts’ when using ‘post__in’

    I encourage you read this entire page: https://codex.wordpress.org/Class_Reference/WP_Query

  • Curious.. when you are using get_field and $post_id is that working as expected? Might need to change $post_id to get_the_id()??

    At any rate…
    Can you make these changes for testing, and let me know how it goes?

    
    function rto_deactivation($id) {
      update_field('field_59ba2159ddd3b', 'Deactive', $id);
    }
    
    // then call it like:
    
    rto_deactivation( get_the_id() );
    
  • I looked in the source code, and there isn’t a filter based on ‘key’ or ‘fieldname’ for the acf/fields/wysiwyg/toolbars filter.

    And truthfully, I couldn’t get it to work by itself. I will dig deeper.

  • Hi there…

    When you are creating a Date field, you can choose the “display” format, and the “return” format independently… there is a Custom option where you can put the mask you specified above.

    For taxonomy… you can choose a return type of Object, rather than ID.

    Then, to output the taxonomy name, you would do:
    (assuming a ‘single’ choice field like a radio or select button)

    
    <?php
    $term = get_field('qicname');
    if( $term ) {
      echo $term->name;
    }
    ?>
    

    For a multi-choice taxonomy, you would create a loop as shown here:
    https://www.advancedcustomfields.com/resources/taxonomy/

  • You can do a quick query like:

    
    $year = date( 'Y' );
    $query = new WP_Query( 'year=' . $year );
    

    Or a date query if you want to do it the regular way:

    
    $args = array(
      'date_query' => array(
        array(
          'year' => date( 'Y' )
        ),
      ),
    );
    $query = new WP_Query( $args );
    

    More info here: https://codex.wordpress.org/Class_Reference/WP_Query#Date_Parameters

  • A thought that popped to mind is to have a repeater attached to the Image itself… with sub fields of type: Post Object and Text

    Then… when you add/edit an image from within the WordPress Media Library, you can create a new repeater row for each Post that you are attaching the image to.

    Then… you will always know the Image ID, for which you can look up the Post ID (from the repeater), for which you can look up the custom caption.

    Let me know if that seems like a reasonable solution, and if you need further guidance.

    Keith

  • Here’s the code that I use at the top of my plugins where I include ACF Pro, and it works flawlessly. Hope it helps you!

    
    if( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
    
    /** Start: Detect ACF Pro plugin. Include if not present. */
    if ( !class_exists('acf') ) { // if ACF Pro plugin does not currently exist
      /** Start: Customize ACF path */
      add_filter('acf/settings/path', 'cysp_acf_settings_path');
      function cysp_acf_settings_path( $path ) {
        $path = plugin_dir_path( __FILE__ ) . 'acf/';
        return $path;
      }
      /** End: Customize ACF path */
      /** Start: Customize ACF dir */
      add_filter('acf/settings/dir', 'cysp_acf_settings_dir');
      function cysp_acf_settings_dir( $path ) {
        $dir = plugin_dir_url( __FILE__ ) . 'acf/';
        return $dir;
      }
      /** End: Customize ACF path */
      /** Start: Hide ACF field group menu item */
      add_filter('acf/settings/show_admin', '__return_false');
      /** End: Hide ACF field group menu item */
      /** Start: Include ACF */
      include_once( plugin_dir_path( __FILE__ ) . 'acf/acf.php' );
      /** End: Include ACF */
      /** Start: Create JSON save point */
      add_filter('acf/settings/save_json', 'cysp_acf_json_save_point');
      function cysp_acf_json_save_point( $path ) {
        $path = plugin_dir_path( __FILE__ ) . 'acf-json/';
        return $path;
      }
      /** End: Create JSON save point */
      /** Start: Create JSON load point */
      add_filter('acf/settings/load_json', 'cysp_acf_json_load_point');
      /** End: Create JSON load point */
      /** Start: Stop ACF upgrade notifications */
      add_filter( 'site_transient_update_plugins', 'cysp_stop_acf_update_notifications', 11 );
      function cysp_stop_acf_update_notifications( $value ) {
        unset( $value->response[ plugin_dir_path( __FILE__ ) . 'acf/acf.php' ] );
        return $value;
      }
      /** End: Stop ACF upgrade notifications */
    } else { // else ACF Pro plugin does exist
      /** Start: Create JSON load point */
      add_filter('acf/settings/load_json', 'cysp_acf_json_load_point');
      /** End: Create JSON load point */
    } // end-if ACF Pro plugin does not currently exist
    /** End: Detect ACF Pro plugin. Include if not present. */
    /** Start: Function to create JSON load point */
    function cysp_acf_json_load_point( $paths ) {
      $paths[] = plugin_dir_path( __FILE__ ) . 'acf-json-load';
      return $paths;
    }
    /** End: Function to create JSON load point */
    
  • Try changing:

    endforeach; wp_reset_postdata();

    to:

    endforeach; $post_article->reset_postdata();

    … and let me know.

  • If I am understanding you correctly, you are saying that:

    1) you create a field of type: Post Object
    2) you leave the ‘Filter by Post Type’ as ‘All post types’
    3) you choose a post that belongs to a CPT, and the array is empty

    But when you:
    1) create a field of type: Post Object
    2) select the CPT type from the ‘Filter by Post Type’ list
    3) choose a post that belongs to a CPT, the array is not empty

    Is that correct?

    If so, I wasn’t able to replicate that. It worked for me. I am using the latest pro version of ACF. Which version are you using? Are you leaving ‘Post Object’ as the ‘Return Format’?

  • Hello…

    Since you have the Book, you can get the Book ID, then you can query all chapters where CF_Book = Book ID

    Here’s a resource to help with Querying posts with Custom Fields.

    Let me know if that’s helpful for you!

  • You can:

    1) Login here: https://www.advancedcustomfields.com/my-account/

    2) Go to: Manage Activations

    3) Remove the old site

    4) Activate the new one 🙂

  • Hello…

    With WP_Query you can use the argument post__in to pass in an array of post IDs…
    See here: https://codex.wordpress.org/Class_Reference/WP_Query#Post_.26_Page_Parameters

    So… with a Post Object ACF filed, you might be returning IDs or Post Objects (default).

    If returning Post Objects, you can use this code to get an array of IDs:

    
    $post_objects = get_field('po');
    $post_object_ids = array_map(function($o) { return $o->ID; }, $post_objects); // array of IDs to pass to: post__in in a WP_Query
    

    Careful though about passing an empty array to post__in because it will return all results.

  • Hi Rich…

    Since you want both comparisons to be true, you can include an “AND relation” within the query:

    
    array(
      'relation' => 'AND',
      array(
        'key' => 'offer_start_date',
        'value' => $today,
        'compare' => '<='
      ),
      array(
        'key' => 'offer_end_date',
        'value' => $today,
        'compare' => '>='
      )
    )
    

    Also, here are more docs on date queries that may be useful.

  • Hi @kollinsayz

    I’m not sure what you mean when you say “based on the already existing posts”

    You could use the acf/load_value filter to pre-populate the value a field.

    Here’s the PHP manual for getting the current date/time.

    But remember that WordPress time stamps posts automatically, and you can use the Date Parameters of a WP_Query to show posts associated with a certain time and date period.

  • Wouldn’t you query the hospitals first then?

    Example:

    1) Get all Branch IDs of all hospitals, where Recommended = TRUE
    2) Add all those IDs to an array
    3) Remove duplicates from the array of Branch IDs
    4) Query Branches with Post IDs in ( post__in ) your array

    Am I understanding correctly?

  • @redeclipse

    It seems to look good in terms of no missing/extra brackets/commas, but I do see one potential issue.

    First, I can only assume $current has the appropriate data. During testing, you can leave one element out at a time… or hard code otherwise dynamic values.

    But.. the issue I see…

    When I was building my query… I looked at the existing ACF examples and docs and when using (as suggested):

    array(
      'key' => 'show_main',
      'compare' => '==',
      'value' => '1'
    )

    .. it didn’t work for me.

    But when I changed it to what made more sense to my brain anyway… which was:

    array(
      'key' => 'show_main',
      'compare' => '=',
      'value' => TRUE
    )

    … then it worked!

    That quick change might do it for you. I am not sure if double equals matters… I didn’t see double equals in the WP docs so I used single (which is the default). Note: I do not wrap TRUE in quotes in my example.

    Also, my bad on steering you towards get_posts() – it seems to be just a matter of preference in most cases.

    I noticed that in your original post you were using get_page() (singular) and I hadn’t heard of it, so I looked it up and saw that it had been deprecated in favour of, as it turns out: get_post() (singular)

    Now… I know you can achieve near same results with get_pages() and get_posts() (both plural)… maybe, just maybe, get_pages() is more appropriate for you. I only say that because Pages are hierarchical by nature, and maybe that fact is important to you and your query. I don’t know… but… with that said… with get_page() falling away, perhaps get_pages() will as well. Time will tell.

    At any rate, if you decide to jump over to get_pages() you just have to modify your args slightly.

    Best of luck with your project!

  • Hi Russell,

    Ok, so if you are returning the ID only, then you can use a built in WordPress function to auto-generate the Image HTML for you.

    As a side benefit to doing it this way, the auto-generated code will also be “responsive” – meaning that when someone is viewing your web site, their device will load an image that is appropriate for their device (in terms of file size, etc). Because… WordPress generates multiple-sized images each time you upload.

    At any rate… since you are only storing the Image ID, that’s all you will see if doing:

    <?php
    echo get_field('image');
    ?>

    or:

    <?php
    the_field('image');
    ?>

    But, since you want the actual image to be generated… you can use the wp_get_attachment_image() WordPress function. If you tell it the Image ID it will display the HTML (i.e. the image will show up). By default, it will just show a ‘thumbnail’. If you want to use a different size, you tell it the size you want (either by name or dimensions). You can learn more about your options here, which makes reference to cropping, etc.

    Below in an example where it specifies a size by name. There are built in sizes in WordPress, plus themes and plugins may also generate different names to create images of different dimensions/orientations. I like to use this plugin so that when I am viewing an image in the WordPress Medial Library, I get to see a list of all available image sizes currently in use.

    Ok, finally, below is the example (assuming again that your field name is ‘image’).
    Note: The example below is pulled direct from this resource. It’s worth a read to wrap your mind around the ACF Image field.

    <?php 
    
    $image = get_field('image');
    $size = 'full'; // (thumbnail, medium, large, full or custom size)
    
    if( $image ) {
    
    	echo wp_get_attachment_image( $image, $size );
    
    }
    
    ?>
  • Hi Seth,

    Maybe I am thinking about this incorrectly… but… it seems to me that the caption *should* stay with the image, and *not* the post, yeah?

    What if you un-attach the image from the post, then the Caption remains, right?

    Wait.. now that I typed that out… I suppose what can be done is…

    1) Create an ACF field attached to Images…

    2) Make it read only (i.e. disable it using the acf/prepare_field filter). That way.. it *stays* with the image, but cannot be edited by anyone.

    Now… when you attach that same image to another Post… the data will carry over and stay consistent.

    This is the first time hearing of IPTC for me, but here are docs on how to parse the data with PHP.

    To attach the field to Images (so that it can be edited in the Media Library), choose ” ‘Attachment’ ‘is equal to’ ‘All image formats’ ” in the Location Rules when creating/editing the ACF Field Group.

    Now, I suspect you can use this filter to do you IPTC parsing work, and saving the data into your Custom field. It’s for “saving posts” but can be used for other data types too. I haven’t worked out precisely how to do this final part yet, but I am confident it’s doable. Let me know what you think.

Viewing 25 posts - 51 through 75 (of 118 total)