Support

Account

Home Forums Search Search Results for 'q'

Search Results for 'q'

reply

  • I’m assuming that you are using ACF4 because you indicate that you cannot add field to the user registration form. ACF5 can add custom fields to the user registration form.

    ACF5 does not however have a way to add a disabled option in the select field.

    I think that this can be simulated by setting the field to required and to not allow null values and then using an acf/load_field filter to insert a null entry into the beginning of the selections https://www.advancedcustomfields.com/resources/dynamically-populate-a-select-fields-choices/. While the first item would be selective, validation would fail submitted with it selected. I haven’t tested this, but it seems like it should work.

  • This is a starting point specific to my own personal needs. I haven’t finished my form yet, but this would get you started. Would be good to branch out into validation once the fields are situated.

    You would need to update this with your own data since it has specific fields from $_POST currently. My fields were: first_name, last_name, user_email. The first and last name are already meta values so they work well for editing already, but I had to add support for editing the email address. Creating a new user, the point of this thread, was fully custom.

    Creates a form in your template based on a query variable.

    
    <?php
    if ( ! empty($_GET['user']) ) :
    	$user_id = ( is_numeric($_GET['user']) ? 'user_' . $_GET['user'] : 'new_user' );
    	acf_form([
    		'field_groups' => [ 510 ],
    		'post_id'      => $user_id,
    	]);
    endif;
    ?>
    

    In functions.php

    
    <?php
    /**
     * Add / Edit users
     */
    function trac_load_value_user_email( $value, $post_id, $field ) {
    	if ( false !== strpos($post_id, 'user_') && $user_id = str_replace('user_', '', $post_id) ) {
    		$userdata = get_userdata( $user_id );
    		if ( $userdata ) {
    			$value = $userdata->user_email;
    		}
    	}
        return $value;
    }
    add_filter( 'acf/load_value/name=user_email', 'trac_load_value_user_email', 10, 3 );
    
    function trac_update_userdata( $post_id ) {
    	if ( empty($_POST['acf']) || empty($_POST['acf']['field_599c48218c0ab']) || false === strpos($post_id, 'user') ) { return; }
    
    	$user_email = $_POST['acf']['field_599c48218c0ab'];
    
    	// Create a new user
    	if ( 'new_user' === $post_id ) {
    		// If email already exists, this should be part of validation or at least return an error...
    		if ( email_exists( $user_email ) ) { return false; }
    
    		// Create a password
    		$length = 13;
    		$include_standard_special_chars = false;
    		$random_password = wp_generate_password( $length, $include_standard_special_chars );
    		// Create the user, use email as username
    		$user_id = wp_create_user( $user_email, $random_password, $user_email );
    		// Update name and role
    		wp_update_user([
    			'ID' => $user_id,
    			'first_name' => ( ! empty($_POST['acf']['field_599c479c8c0a9']) ? $_POST['acf']['field_599c479c8c0a9'] : '' ),
    			'last_name' => ( ! empty($_POST['acf']['field_599c480e8c0aa']) ? $_POST['acf']['field_599c480e8c0aa'] : '' ),
    			'role' => 'editor',
    		]);
    	// Edit the user's email
    	} else {
    		$user_id = str_replace('user_', '', $post_id);
    		wp_update_user([
    			'ID' => $user_id,
    			'user_email' => $user_email,
    		]);
    	}
    }
    add_action( 'acf/save_post', 'trac_update_userdata', 1 );
    

    References:

    1. https://codex.wordpress.org/Function_Reference/wp_update_user
    2. https://www.advancedcustomfields.com/resources/create-a-front-end-form/
  • The problem is that you deleted the posts in phpMyAdmin. When you do this it becomes extremely difficult to delete the orphaned post meta values. The link I posted is to a plugin that’s supposed to be able to clean this up.

    The only way short of this is to build your own SQL queries and run them through php. The steps involved are

    1. Query the database and get a list of all active post IDs
    2. Run a delete query on the _postmeta table WHERE post_id NOT IN (post id list)

    The developer of ACF does not visit here much, this is a users helping users forum. If you want to get input from the developer then the best chance you have of that is submitting a new support ticket here https://support.advancedcustomfields.com/new-ticket/

  • Hi!

    I opened a support ticket and just received an answer. James’ reply was:

    Can you please deactivate and re-activate your ACF PRO license (from the custom fields -> updates page) and try the plugin update again?
    You can also try the ‘Dashboard -> Updates’ page and click the ‘Check again’ button to clear plugin update cached data.

    The first solution did the trick for me.

    Hope it works for you 🙂

  • I think this has been reported before, either with this plugin or another. I tested and I’m seeing the same issue. Please submit a support ticket here https://support.advancedcustomfields.com/new-ticket/ and you may want to also submit a support request with the other plugin as well.

  • Your problem is here

    
    'post_id'	=> 'new_post',
    

    in ACF 5 there is a built in pre_save_post filter that runs before yours and the value of ‘new_post’ triggers this filter which creates the new “Post”.

    Use something else like

    
    'post_id'	=> "new_{$custom_post_type}",
    
  • For feature requests please submit a new support ticket here https://support.advancedcustomfields.com/new-ticket/

  • There are built in ways in WP for adding things like Products and Brands. These would be custom post type and custom taxonomy. Most filtering can be done using custom taxonomies for things like brands and product categories. I would start there.

    ACF is great for things that would be difficult to manage using a custom taxonomy. For example, the number in stock or something like a part number associated with each product. Or for that matter, any value that would be unique, or even semi unique for each product.

    You can create filters using ACF, but most of the work of building the filter/search form and the queries that display the posts for the search would need to be coded by you.

  • I had to look it up because I was under the impression that only published posts would be returned but

    From: https://codex.wordpress.org/Class_Reference/WP_Query
    post_status (string / array) – use post status. Retrieves posts by Post Status. Default value is ‘publish’, but if the user is logged in, ‘private’ is added. Public custom statuses are also included by default. And if the query is run in an admin context (administration area or AJAX call), protected statuses are added too. By default protected statuses are ‘future’, ‘draft’ and ‘pending’.

    These are retrieved using a AJAX call.

    Anyway, these augments used in a relationship field can be altered using an acf/fields/relationship/query filter https://www.advancedcustomfields.com/resources/acf-fields-relationship-query/. The post object field has a similar hook https://www.advancedcustomfields.com/resources/acf-fields-post_object-query/

    As far as the link field goes, I don’t have a clue about this one. It appears to use the built in WP link popup, but after testing, this only appears to return published posts.

  • I don’t know a lot about the jQuery plugins your using, but if you want ACF values in your code somewhere then you need to put them there. I am assuming that there are “data-” attributes that you can add to some element that are used by one of these plugins.

    If the image gallery plugin you mentioned is this one https://wordpress.org/plugins/jquery-masonry-image-gallery/ then I would start by looking at the documentation for that plugin if there are any filters that will let add these custom values.

  • Outside of “The Loop” which is where the header is, you need to tell ACF what $post_id you want to use get_field('field_name', $page_id); $page_id can take many forms form an actual post id to a user to a term. This is covered on https://www.advancedcustomfields.com/resources/get_field/ in the section Get a value from different objects.

    How you get the post ID for the current page depends on what you’re trying to get. For example an archive page is generally associated with a term

    
    $post_id = 0;
    $queried_object = get_queried_object();
    if (isset($queried_ojbect->term_id)) {
      $post_id = 'term_'.$term_id;
    }
    

    and this will only work if you have a custom field associated with the term in question.

    For most “archive” pages, there isn’t anywhere to put a custom field. Archive pages outside of terms do not have an admin page, so there isn’t any way to add a field to a specific archive. In this case the best option is an options page that is associated with a specific post type.

  • It looks like your field hero is a group field. If so try this

    <?php
    		
    // vars
    $hero = get_field('hero');	
    
    if( $hero ):
        if( $hero['image'] ): >
            <div id="hero">
                <img />" alt="<?php echo $hero['image']['alt']; >" />
            </div>
    
        <?php endif; >
    <?php endif; >
  • Thanks for checking this John.

    It’s unfortunate that there are no filters available for targeting the layouts themselves… perhaps that should be another feature request on its own?

  • I got this code:

    Array
    (
        [bathroom_projects_title] => 
        [bathroom_projects_img] => 
        [bathroom_projects_download_url] => 
    )

    I want all fields in the field group if there is no data, the front end does not output HTML

    Do not show html <div id="hero">

  • Thanks for this information John, much appreciated.

    Maybe for now I will just manually add the User IDs to create a simple query for each office.

  • Hi John,

    thanks for the example. this is the approach I eventually thought would be the case. I was going to save a taxonomy to query on eg “has_multiple” but if i can do a query on all items where meta value related_employees_count > 1 , that would do it

    thanks again
    J.

  • There isn’t any way that you can do this with one query because you are adding locations to users. I’m not even sure you could do it in one query if you were adding users to locations.

    Locations added to users

    1. Get users
    2. Loop through users to get figure out who is at a the location and compile a list/array
    3. Query posts by users

    Users added to locations, this would be easier

    1. get location post
    2. Get list of authors from location
    3. query posts by list of authors

    Due to the way relationship fields are stored (this includes, relationship, taxonomy, user and any field that relates one WP object to another) as serialized arrays, and the fact that you can’t really query across multiple types of WP objects (multiple post types or users/post types) there isn’t going to be a way to do this with just a WP_Query and you’re going to need to do some kind of extra work and more than likely multiple queries to make it happen.

  • I have this exact same request. I have setup the following with wp_query and it does display the artist’s gallery images. However, it does not sort them by the gallery custom field across all posts. Instead it sorts them by the CPT post first and then sorts the photos in the order you have them in the gallery field.

    //* Define a custom function that queries the database for your posts
    function display_artist_post_images(){
    
    		//* Limit query to posts with "post type" set to "artist"
    		$queryArgs = array(
    			'post_type' => 'artist',
    			'orderby' => 'date',
    			'order' => 'ASC'
    		);
    
    		//* The query
    		$the_query = new WP_Query( $queryArgs );
    
    		//* The loop
    		if( $the_query->have_posts() ) :
    			echo '<ul>';
    			while( $the_query->have_posts() ) :
    				$the_query->the_post();
    				$images = get_field('gallery');
    				if( $images ):
    						foreach( $images as $image ):
    							echo '<li>';
    								echo '<a href=';
    								echo $image['url'];
    								echo '>';
    									echo '<img src=';
    									echo $image['sizes']['thumbnail'];
    									echo ' alt=';
    									echo $image['alt'];
    									echo ' />';
    								echo '</a>';
    								echo '<p>';
    								echo $image['caption'];
    								echo '</p>';
    							echo '</li>';
    						endforeach;
    				endif;
    			endwhile;
    			echo '</ul>';
    		endif;
    
    		//* Restore original Post Data
    		wp_reset_postdata();
    	}
    	//* Add your custom function to the site using WP's "add_action" function with a Genesis hook.
    	add_action( 'genesis_entry_content', 'display_artist_post_images' );
  • Currently, the only way that I can see this might be done is by adding custom JavaScript to the page to turn on autocomplete on some fields after the page loads. I’m not 100% sure how you’d do that. https://www.advancedcustomfields.com/resources/adding-custom-javascript-fields/

    This is only a guess:

    
    jQuery(document).ready($) {
      // the field key of your field
      $('[data-key="field_1234567"] input').attr('autocomplete', 'off');
    }
    
  • Basically, on this page https://www.advancedcustomfields.com/resources/query-posts-custom-fields/ if you look in the section 3. Multiple custom field values (array based values) it tells you how to query based on these values. I understand it and my advice is this, ignore it and don’t do it. Even if you get it to work if you add too many “LIKE” queries for the same meta key you’ll just end up timing out your site because of the limits of WP_Query.

    Instead make your life easier. You know that you can use an “IN” query with normal WP post meta fields, you already have an example of this. So instead of trying to search the ACF field, which is difficult, convert the ACF field into something that is easy to search.

    
    add_filter('acf/save_post', 'convert_classes_to_standard_wp', 20);
    function convert_classes_to_standard_wp($post_id) {
      // use a different field name for your converted value
      $meta_key = 'converted_classes';
      // clear any previously stored values
      delete_post_meta($post_id, $meta_key);
      // get new acf value
      $values = get_field('classes', $post_id);
      if (is_array($values) && count($values) {
        foreach ($values as $value) {
          add_post_meta($post_id, $meta_key, $value, true);
        } // end foreach
      } // end if
    }
    

    Now you can use converted_classes instead of classes in your meta query with the 'compare' => 'IN'.

  • Since the relationship field stores a serialized array holding the ID values of each related post, you’re correct, it would be virtually impossible to do a query based on this value. It would be much easier to create an acf/save_post filter that gets the of related posts and sets that number into another “hidden” post meta field. It doesn’t really need to be hidden, just an extra field outside of ACF. Something like:

    
    add_action('acf/save_post', 'count_related_employees', 20); //run after acf
    function count_related_employees($post_id) {
      if (get_post_type($post_id) != 'company') {
        return;
      }
      // 3rd parameter: false: gets value without formatting
      $employees = count(get_field('related_employees', $post_id, false));
      update_post_meta($post_id, 'related_employees_count', $employees); 
    }
    
  • A little late here, but an easy JS solution I created is this:

    
    jQuery(function(){
    	function parseHash(){var hash=window.location.hash.substring(1),params={};if(hash.length<5){return params;}var vars=hash.split('&');for(var i=0;i<vars.length;i++){if(!vars[i]){continue;}var pair=vars[i].split('=');if(pair.length<2){continue;}params[pair[0]]=pair[1];}return params;},
    	var hashData=parseHash();
    	if(hashData&&hashData.acf_tab){jQuery('a.acf-tab-button[data-key="'+hashData.acf_tab+'"]').trigger('click');jQuery('form#post').attr('action','#&acf_tab='+hashData.acf_tab);}
    	jQuery('a.acf-tab-button').on('click',function(event){window.location.hash='&acf_tab='+jQuery(this).data('key');jQuery('form#post').attr('action','#&acf_tab='+jQuery(this).data('key'));});
    });
    

    Just add that anywhere in the admin area of your site through an action/filter. Here’s an example of that for your functions.php file:

    
    add_action( 'admin_enqueue_scripts', array(__CLASS__,function(){
    	wp_enqueue_script( 'psAdminCustom', get_template_directory_uri() . '/admin/custom.js', array(), '2.0.0', false );
    } ) );
    
  • Thank you for your reply.

    I went back and looked at all the code again and I can’t figure out what I was doing wrong before. When I did as your suggested (which I thought I had already tried) then this time it did work! Weird. In case any one else wants to know, this is what worked:

    <script type="application/ld+json">
         {
          "@context": "http://schema.org/",
          "@type": "Recipe",
          "name": "<?php the_field('recipe_name'); ?>",
          "image": "http://images.media-allrecipes.com/userphotos/600x600/1116471.jpg",
          "author": {
          "@type": "Person",
          "name": "scoopnana"
         }
         }
    </script>

    Thank you. Please mark as resolved.

  • Hi Trishah,

    Try doing an echo for any php that you want to appear inside a JS:

    
    <script type="application/ld+json">
         var recipeName = '<?php echo the_field('recipe_name'); ?>';
    </script>
    

    or

    
    <script type="application/ld+json">
         var recipeName = '<?php echo get_field('recipe_name'); ?>';
    </script>
    
  • For a feature request like this, please open a new support ticket https://support.advancedcustomfields.com/new-ticket/

Viewing 25 results - 10,826 through 10,850 (of 21,317 total)