Support

Account

Forum Replies Created

  • Theory: $field is blank for the same reason as the starting issue – that, until a field value is actually saved, the object does not actually have a field value obtainable by its name (?). And it will actually be impossible for me to programmatically obtain a field key using a field name, until that field is actually saved with some values? Am I right/wrong?

    Assuming that’s right, okay, I’ll bow to hard-code the field keys instead…

               // Convert Json
               $json_object = json_decode($json_result_string);
    
               // SAVE TO ACF GROUP:
    
               // Categories (source stdClass object):
    
               // sector
              $field_key = 'field_5c1bb19b00004';
              update_field($field_key, $json_object->category->sector, $org_id_prefixed);
    
              // industryGroup
              $field_key = 'field_5c1bb1f100005';
              update_field($field_key, $json_object->category->industryGroup, $org_id_prefixed);
    
              // industry
              $field_key = 'field_5c1bb36d55bf7';
              update_field($field_key, $json_object->category->industry, $org_id_prefixed);
    
              // subIndustry
              $field_key = 'field_5c1bb474dfb1a';
              update_field($field_key, $json_object->category->subIndustry, $org_id_prefixed);
    
              // sicCode
              $field_key = 'field_5c1bb495dfb1b';
              update_field($field_key, $json_object->category->sicCode, $org_id_prefixed);
    
              // naicsCode
              $field_key = 'field_5c1bb4bddfb1c';
              update_field($field_key, $json_object->category->naicsCode, $org_id_prefixed);
    
              // Tags (source Array):
              $field_key = 'field_5c0a9d251831a';
              update_field($field_key, $json_object->tags, $org_id_prefixed);

    For a test, I have wiped the pre-fixed “Choices” out of all fields in the field group editor, and re-saved the group. Also, I have run this code on a taxonomy term which has not previously had any of the values set. The outcome is this…

    That is, the values are saving to the fields, but still not in the way intended re: cementing Choices as per my original problem. Only the values for the “Tags” Checkbox field have been set as Choices, the rest have not…

    One thing that’s noticeable (this may be coincidence, or not) is that the source for “Tags” is an array from the Json (“tags”), whilst the source for the rest of the values is a “std Class Object”. See the red/green rectangles in each of the screengrabs…

    Any correlation in this discrepancy, or am I already doing things correctly?

  • I also just realized that you are using update_field($field_name, $value). If these fields to not already have values for these posts, in other words, you are adding the post and then immediately trying to update them where they have never had a value, then update field will not work using the field name and you must use the field key.

    For info, in my case, I am attempting to save field values for taxonomy terms, rather than posts. In current test phase, the term/s in question already exist.

    I think you’re saying – if the term already exists but if it has not yet had a value for the ACF field eg. “sector” already saved previously, I have to use update_field with the field key, not name…

    This gets me thinking, “rather than hard-code in the field key, can I programmatically obtain the field key from the field name?
    I see in the post Get field key by name, @almbay uses…

    $field = get_field_object('listing_type');
    $field_key = $field['key'];

    So maybe I could/should do…

    $field = get_field_object('sector');
    $field_key = $field['key'];
    update_field($field_key, $json_object->category->sector, $org_id_prefixed);

    But, I tried. And, in this situation, $field_key is assigned no value.

    I’ve checked the get_field_object docs. Maybe I should add $org_id_prefixed as a get_field_object parameter… ?

    $field = get_field_object('sector', $org_id_prefixed);
    $field_key = $field['key'];
    update_field($field_key, $json_object->category->sector, $org_id_prefixed);

    But, no, $field_key is still blank.
    In fact, before that, $field itself is also blank.

    So, why is $field = get_field_object('sector', $org_id_prefixed); failing to put anything in $field?

    FYI, $org_id_prefixed‘s value is like company_7456, corresponding to a taxonomy term ID formulation, and this value is present.

    (My previous topic attempt did not appear to save and the forum asked me to send a ticket instead).

  • Is $json_string a json string or is it an “object” as you posted.

    If it’s a json string you can’t access the values in it using $json_string->category->sector unless you decode it first.

    $object = json_decode($json_string);

    I’m already decoding …

    $json_string = json_decode($result);

    and echo 'sector: '.$json_string->category->sector.'<br />'; confirms that the specific value is getting plucked out.

    Need to alter my use of language/name. String result to start, object once decoded.

  • This screenshot is for a different entry, but illustrates how some values saved only with update_field have not solidified as Choices but others which have since been saved-over manually appear to have done so…

    NB. I am doing all this on a taxonomy term.

  • @hube2

    Like this…

    update_field('sector', $json_string->category->sector, $org_id_prefixed);
    update_field('industry_group', $json_string->category->industryGroup, $org_id_prefixed);
    update_field('industry', $json_string->category->industryGroup, $org_id_prefixed);
    update_field('subindustry', $json_string->category->industryGroup, $org_id_prefixed);

    Where, FYI, the incoming values are…

       [category] => stdClass Object
            (
                [sector] => Information Technology
                [industryGroup] => Technology Hardware & Equipment
                [industry] => Technology Hardware, Storage & Peripherals
                [subIndustry] => Computer Hardware
                [sicCode] => 73
                [naicsCode] => 51
            )

    When I enter a new Checkbox value in the GUI, manually, it is properly saved as a Choice.

    But, when I use update_field, it does not happen. This means the values 1. persist as editable text boxes, not just check boxes and 2. don’t actually even show in columns my wp-admin edit list…

    [No screenshots]

    The weird thing, however, is that, when I enter an arbitrary new value in the GUI, not only does the specific value get saved as a Choice and becomes visible in a column, it also shakes all values previously saved with update_field in to acting properly…

    So, when I add a new value “Test Value” to the “sector” Checkbox, it fixes all “sector” values as Choices – and also those of “industry_group”, “industry” and “subindustry”.

    This is a happy side effect, but it doesn’t feel like this should be the correct behaviour, and it won’t change the fact that using programmatic update_field alone doesn’t fix values as Choices.

    (Don’t want to hijack the thread, but my attempt to create a new one was diverted to support).

  • So, conversely, how do you SET a field object, ie. a Choice?

    I am using update_field to set checkbox values and, despite “Allow Custom” and “Save Custom” being set, the new values are not being saved as Choices, which is against my expectation.

    Is there something like an equivalent set_field_object that is required to save the same value as a Choice at the same time?

  • Albeit, when I do…

    update_field('categories_sector', 'My Value', $org_id_prefixed);

    … I am now getting the following …

    Warning: Invalid argument supplied for foreach() in /home/myfolder/public_html/wp-content/plugins/advanced-custom-fields-pro/includes/fields/class-acf-field-checkbox.php on line 490

    But only when “Save Custom” for the Checkbox named “Sector” is switched ON.

  • It was this answer that finally answered my question on how to save to fields within a Group field – basically, using an underscore reference method.

    I didn’t see that at all in the documentation, which seems more geared toward saving fields within a repeater.

    Also, ACF’s Edit Field Group constructor “Underscores and dashes allowed”. Given that this method depends on underscore separation, is the tip correct?

  • I’m struggling with this and the documentation doesn’t really make it clear…

    I have the following field types in a field group placed on a Taxonomy “company”…

    • Group: “Categories”, org_categories
    • Checkbox: “Sector”, org_cat_sector
    • Checkbox: “Industry Group”, org_cat_industrygroup
    • Checkbox: “Industry”, org_cat_industry

    How do I save values in to those checkboxes?

    $org_id_prefixed = 'company_'. $organisation->term_id; already gives the prefixed ID.

    Elsewhere, I’m successfully using the following to save to a Checkbox that is NOT in a Group…

    // Save "tags" (array) in ACF "org_tags" (checkbox)
    update_field('org_tags', $json_string->tags, $org_id_prefixed);

    What I want to save to org_cat_sector is $json_string->category->sector

    Most of the documentation around update_sub_field refers to Repeater. I’m unclear if I need to use have_rows stuff.

    The following…

    update_sub_field(
    array(‘org_categories’, 1, ‘org_cat_sector’), // Selector
    $json_string->category->industryGroup, // Value
    $org_id_prefixed // Object ID
    );

    … results in PHP throwing: ( ! ) Warning: Invalid argument supplied for foreach() in /home/myfolder/public_html/wp-content/plugins/advanced-custom-fields-pro/includes/fields/class-acf-field-checkbox.php on line 490

  • Just curious to add a couple of things/questions here…

    1. Does the code I have used above match the kind of suggestion you had given the circumstances?

    (To convey a company type, I have weighed up using either 1) a taxonomy, linked to the company term via an ACF field, or 2) just an ACF field itself. My code above uses a taxonomy).

    2. I’m now considering that the “company type” descriptor may actually need to be multiple

    In other words, I may have available multiple source pieces of data with which to describe my companies…

    1. “tags”
    2. “category: sector”
    3. “category: industryGroup”
    4. “category: industry”
    5. “category: subIndustry”
    6. “category: sicCode”
    7. “category: naicsCode”

    I am now considering that, if I choose to use them all, this is clearly too much to fit in to one “company type” as taxonomy.

    So I am now considering going back to baking the company descriptive field/s in to the company term itself as ACF fields, and not using a separate “company type” taxonomy…

    i)

    I had initially believed that doing it that way would make the looping slower.

    But, since I am already currently linking that taxonomy to a term using an ACF field anyway, maybe there is no impact to just pulling that data from term meta (ACF fields on “company”) when it’s open (?).

    ii)

    I will still want to output posts by company type in various ways. I’d value discussing this…

    It seems that using a taxonomy to describe “company type” gives the natural advantage of spinning up template pages (eg. taxonomy-orgtype-agency.php to output posts for all “agency” companies).

    If I move to storing all “company type” fields as ACF fields against “company” terms themselves, how would I go about grouping output of posts per company type?

    To simplify that question – say I have 200 ‘company” terms with a Checkbox “org_tag” value of “agency”. How do you output only those? …

    Because, unlike a taxonomy, I assume there is no relation between entities with the same value? Is there an ACF field that does have this relational database-ability? Leaving aside the Relationship, which seems to want a relationship with a Post.

  • Yes, I think that was a similar logic that I had been using…

        Add a field for the taxonomy term
        Get all companies
        For each, check the addtional ACF field
        If it matches the one we want to constrain, add its ID to an array
        When done, use that array to feed specific company term IDs to the WP_Query. In other words, narrow things

    I had concluded that this method may be slower than desirable. I have know decided that I’ll just have to lumnp it, it doesn’t take light years.

    However, the big switch I have made since first trying this method is this – whilse I initially added an ACF Select field as the additional, cosntraining field on the company term, now I am using an actual Taxonomy field. That is, I registered a new taxonomy, Organisation Type (orgtypetax), and have added that as an ACF field. For good measure, I ensured the taxonomy regisgtration code also attaches to the “company” object – though I suspect this is wishful thinking and actually may not be possible.

    For reference…

          // GET ALL ORGANISATIONS
    
           $organisations = get_terms(
             array(
                'taxonomy' => "company",
             )
          );
    
          // NARROW ONLY SPECIFIC ORG TYPE
    
          // Initialise array we will use to feed the WP_Query
          $orgs_filtered = array();
          // Slug of the orgtypetax taxonomy
          $target_type = "agency";
    
          // Check every organisation's orgtypetax for $targettype - if found, add to array
          foreach ($organisations as $organisation) {
    
             // Construct prefixed field ref for this organisation
             $org_id_prefixed = 'company_'. $organisation->term_id;
    
             // Get ID organisation's orgtypetax taxonomy term
             $orgtypetax_term_id = get_field( 'orgtypetax', $org_id_prefixed );
             // Now use ID to get the actual object
             $orgtypetax_term_obj = get_term( $orgtypetax_term_id );
    
             // If the found term matches the target term, add to our array
             if ( $orgtypetax_term_obj->slug == $target_type ) {
                   array_push($orgs_filtered, $organisation->term_id);
             }
    
          }
    
          // Query posts
          $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
          $args = array(
            // pagination
            'nopaging' => false,
            'posts_per_page' => 50,
            'paged' => $paged,
            // posts
            'post_type' => 'article',
            // order
            'orderby' => 'date',
            'order'   => 'DESC',
            // taxonomy
            'tax_query' => array(
              array(
                'taxonomy' => 'format',
                'field'    => 'slug',
                'terms'    => 'viewpoint'
              ),
              // Only organisations filtered above
              array(
                'taxonomy' => 'company',
                'field'    => 'id',
                'terms'    => $orgs_filtered
              ),
           ),
          );
          // the query
          $this_query = new WP_Query($args);
          if ( $this_query->have_posts() ) :
              set_query_var('my_query', $this_query   );
              set_query_var('name',     ucfirst($target_type).' Views' );
              set_query_var('count',    $this_query->found_posts);
              set_query_var('columns',  'col-md-6 col-lg-6 mb-4');
              // Template part
              get_template_part('partials/card', 'medialist');
              wp_reset_postdata(); ?>
           <?php else : ?>
             Nothing here.
           <?php endif;
    
           ?>
    
  • I have the below in a WordPress theme file template part, called from page.php
    The use case is, my ACF field set is used on Page screens as a page content creator.

    The template files should spit out content. In the below case, it is a social-proof section in Bootstrap mark-up, comprising text fragment and logos.
    This works fine when I display all logos, but there are too many for display on this section of the page. So (using this thread as a guide), I tried limiting this to six logos. That part works fine.

    The weird thing is, it leads to a single, secondary repetition of the whole container, immediately beneath, just without any of the content – no logos_tee-up text, no logos files, just the mark-up.

    I’m confused, because there is actually one instance of the social_proof row. Can’t understand why it has been repeated once and once only.

    When I remove the iteration and break, the repeated container doesn’t happen. But I do need to limit. What is not right here?

    <?php if ( have_rows( 'social_proof' ) ) : ?>
    	<?php while ( have_rows( 'social_proof' ) ) : the_row(); ?>
    
        <div class="container-fluid bg-white p-4 px-md-5 pt-md-4 pb-md-5">
    
          <p class="text-muted"><?php the_sub_field( 'logos_tee-up' ); ?>:</p>
    
          <div class="row">
    
            <?php if ( have_rows( 'logos' ) ) : ?>
              <?php $i = 0; ?>
        			<?php while ( have_rows( 'logos' ) ) : the_row(); ?>
                <?php $i++; ?>
                		<?php if( $i > 6 ): ?>
                			<?php break; ?>
                		<?php endif; ?>
        				<?php $logo_image = get_sub_field( 'logo_image' ); ?>
        				<?php if ( $logo_image ) { ?>
        					<div class="col-4 col-sm-3 col-md-2 col-lg-2 col-xl-2 align-self-center  py-3"><img class="img-fluid" src="<?php echo $logo_image['url']; ?>" alt="<?php echo $logo_image['alt']; ?>"></div>
        				<?php } ?>
        			<?php endwhile; ?>
        		<?php else : ?>
        			<?php // no rows found ?>
        		<?php endif; ?>
    
          </div>
    
        </div>
    
      <?php endwhile; ?>
    <?php endif; ?>
  • This absolutely works for me. Thanks.

    Amazing that it’s been so long.

    I may look for a plugin dedicated to customising placement of all post metaboxes actually.

    Update: I subsequently needed to remove the code from functions.php if I wanted to also respect that custom order with which I had dragged other boxes around. The code keeps resetting them, too, not just the ACF field group.

  • FYI, “mysite” and “context” are the same.

  • If it’s the intended behaviour of the picker to ignore the stored and shown date by instead showing the current date upon pending, I think that’s unnecessarily complex on ACF’s part.

    If it’s a Zulu time format issue, then okay.
    But I don’t think I have the wherewithal to make all these changes, I may just set it as a text field and will enter the time in the correct format manually myself.

    Thanks for your ideas, as ever.

  • Going to assume that’s a no.
    Product idea right there for people 🙂

  • Yeah, I think a taxonomy for related_stories gets in to management difficulty, but definitely pressing ahead with one for publication sources.

    Thanks.

  • Yes, all the Users being searched are only in the blog in question. Apart from me as super-admin, I suppose.

    Blog ID is 2.

    pre_get_users is already a key part of both methods of performing the extended user search (Better User Search and the Misha code you linked to. So, I don’t know whether it’s the problem and/or solution here.

    Some learning to do, I’d say.

  • Update for the public record…

    1.
    I tried both the Better User Search plugin and the code I linked to on a single-install WordPress and it worked fine, but it won’t work on my Multisite, even with the theme switched to default and pretty much everything disabled.

    2.
    I ran “grep -lr “pre_get_users” *” in both the plugins and my theme folder.
    pre_get_users turned up in my Admin Columns Pro plugin – but that plugin is actually disabled.

  • Struggling to understand it a little bit – mainly how it builds the query AND THEN decides what fields to include in the search. But I guess that’s just SQL.

    I am seeing results with a basic query.

    Problem is, paginated results are only showing two per page.
    In fairness, that was also a problem I experienced when using a plugin, Better User Search, which was also built to allow a user to specify custom fields to search. I am now wondering if there is another reason why results in both methods are being pegged to just two per page.

    Indeed, I have just found this issue relating to the “Number of items per page” count set in screen Settings…

        Was 150: only two displayed
        When 200: only two displayed
        When 400: four are displayed
        When 500: four are displayed
        When 600: five are displayed
        When 700: six are displayed
        When 800: six are displayed

    I’ve stripped out other functions that could be causing a conflict and isolated the one in the code you linked to…

         add_action('pre_user_query','rudr_extend_user_search');
    
         function rudr_extend_user_search( $u_query ){
         	// make sure that this code will be applied only for user search
         	if ( $u_query->query_vars['search'] ){
         		$search_query = trim( $u_query->query_vars['search'], '*' );
         		if ( $_REQUEST['s'] == $search_query ){
         			global $wpdb;
    
          			// let's search by users first name
         			$u_query->query_from .= " JOIN {$wpdb->usermeta} fname ON fname.user_id = {$wpdb->users}.ID AND fname.meta_key = 'first_name'";
    
         			// you can add here any meta key you want to search by
         			// $u_query->query_from .= " JOIN {$wpdb->usermeta} cstm ON cstm.user_id = {$wpdb->users}.ID AND cstm.meta_key = 'YOU CUSTOM meta_key'";
    
          			// let's search by all the post titles, the user has been published
         			// $u_query->query_from .= " JOIN {$wpdb->posts} psts ON psts.post_author = {$wpdb->users}.ID";
    
          			// what fields to include in the search
          			$search_by = array( 'user_login' );
    
          			// apply to the query
         			$u_query->query_where = 'WHERE 1=1' . $u_query->get_search_sql( $search_query, $search_by, 'both' );
         		}
         	}
         }
  • I am running ACF Pro 5.5.14, so I assume ACF should now be saving to term meta (?).

    I participated in a previous thread in which it was said this would be supported from 5.5.

  • Switching off the rest with CSS? That’s so sneaky 🙂

    Thanks.

  • Indeed. So far, I have this thanks to their help…

    // 4. WPAllImport function - Import "avatar" user meta from first available image URL in input data
    
    function profile_photo($photo_url,$cb_avatar,$cb_twitter_avatar,$cb_aboutme_avatar,$cb_gravatar_avatar){
       if(!empty($photo_url))
           return $photo_url;
       if(!empty($cb_avatar))
           return $cb_avatar;
       if(!empty($cb_twitter_avatar))
           return $cb_twitter_avatar;
       if(!empty($cb_aboutme_avatar))
           return $cb_aboutme_avatar;
       if(!empty($cb_gravatar_avatar))
           return $cb_gravatar_avatar;
    }
    /* Execute in WPAI import using...
    [profile_photo({photo_url[1]}, {cb_avatar[1]}, {cb_twitter_avatar[1]}, {cb_aboutme_avatar[1]}, {cb_gravatar_avatar[1]})]
    ... wherein profile_photo is function name) */

    I will update this thread if I learn more.

    I had looked at the WPAI docs but, though I understand any PHP can be used in the functions and I have a smattering of PHP, I still needed some info to get going on basics like passing variables in and out of the function.

    Thanks.

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