Support

Account

Home Forums Search Search Results for 'q'

Search Results for 'q'

reply

  • The problem with image fields is that they store the Post ID of the image in the media library and the image does not exist in the other site and if the ID value does exist it is already used for another post. The first thing you need to do is to insert the image into the media library, get the new ID and then swap the ID from the site you’re copying from with the new ID for the site that you’re copying to.

    You’ll have the same problem with any field type that stores ID values for other WP objects including relationship fields, post object fields, taxonomy fields, etc.

    flex fields and repeaters will make a difference because you’ll need to know how ACF stores values in the DB in order to find the fields that need to be replaced with new ID values. Flex fields and repeaters are stored in a similar fashion.

    A repeater or flex field will have meta keys like
    "{$repeater_name}_{$index}_{$sub_field_name}"
    a nested repeater field will look like
    "{$repeater_name}_{$index}_{$sub_field_name}_{$index}_{$sub_field_name}"

  • The image field shown when a user is not logged into the site or does not have permission to upload to the media library uses the standard browser file field. Unfortunately, there isn’t really any way to modify what this field looks like, at least not easily. Here are some links to places that might help

    https://tympanus.net/codrops/2015/09/15/styling-customizing-file-inputs-smart-way/

    https://stackoverflow.com/questions/572768/styling-an-input-type-file-button

  • Used to ask questions to forget to explore, thanks John Huebner, has been in your answer to find the answer
    https://support.advancedcustomfields.com/forums/topic/a-simple-calculation-right/

  • I implemented [email protected] change and it works well. My question is whether ACF will be updated to fix this issue and if this change does anything to hurt Divi’s functionality.

  • Hi @brad100 use jquery to add an identifier to each marker

    <script src="//ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
    <script type="text/javascript">
    jQuery(document).ready(function($){
            var infowindow;
    /*
    *  new_map
    *
    *  This function will render a Google Map onto the selected jQuery element
    *
    *  @type	function
    *  @date	8/11/2013
    *  @since	4.3.0
    *
    *  @param	$el (jQuery element)
    *  @return	n/a
    */
    
    function new_map( $el ) {
    
    	// var
    	var $markers = $el.find('.marker');
    
    	// vars
    	var args = {
    		zoom		: 16,
    		center		: new google.maps.LatLng(0, 0),
    		mapTypeId	: google.maps.MapTypeId.ROADMAP
    	};
    
    	// create map
    	var map = new google.maps.Map( $el[0], args);
    
    	// add a markers reference
    	map.markers = [];
    
    	// add markers
      // add identifying number to each marker
        var sidebar = 1;
    		$markers.each(function(){
        add_marker( $(this), map, sidebar);
          sidebar++;
    	});
    
    	// center map
    	center_map( map );
    
    	// return
    	return map;
    
    }
    
    /*
    *  add_marker
    *
    *  This function will add a marker to the selected Google Map
    *
    *  @type	function
    *  @date	8/11/2013
    *  @since	4.3.0
    *
    *  @param	$marker (jQuery element)
    *  @param	map (Google Map object)
    *  @return	n/a
    */
    
    function add_marker( $marker, map, sidebar) {
    
    	// var
    	var latlng = new google.maps.LatLng( $marker.attr('data-lat'), $marker.attr('data-lng') );
    
    	// create marker
    	var marker = new google.maps.Marker({
    		position	: latlng,
    		map			: map
    	});
    
    	// add to array
    	map.markers.push( marker );
    
    	// if marker contains HTML, add it to an infoWindow
    	if( $marker.html() )
    	{
    		// create info window
    		infowindow = new google.maps.InfoWindow({
    			content		: $marker.html()
    		});
        // Create a click on the sidebar list and open the info window
    		$('#m'+sidebar).click(function(){
          // Close info windows
    	    $.each(map.markers, function(index,value){
    	        if(infowindow)
              	infowindow.close();
          });
    	    // Click on the marker
    	    google.maps.event.trigger(marker, "click");
    		});
    		// show info window when marker is clicked
    
          google.maps.event.addListener(marker, 'click', function() {
              infowindow.open( map, marker );
          });
    	}
    
    }
    
    /*
    *  center_map
    *
    *  This function will center the map, showing all markers attached to this map
    *
    *  @type	function
    *  @date	8/11/2013
    *  @since	4.3.0
    *
    *  @param	map (Google Map object)
    *  @return	n/a
    */
    
    function center_map( map ) {
    
    	// vars
    	var bounds = new google.maps.LatLngBounds();
    
    	// loop through all markers and create bounds
    	$.each( map.markers, function( i, marker ){
    
    		var latlng = new google.maps.LatLng( marker.position.lat(), marker.position.lng() );
    
    		bounds.extend( latlng );
    
    	});
    
    	// only 1 marker?
    	if( map.markers.length == 1 )
    	{
    		// set center of map
    	    map.setCenter( bounds.getCenter() );
    	    map.setZoom( 16 );
    	}
    	else
    	{
    		// fit to bounds
    		map.fitBounds( bounds );
    	}
    
    }
    
    /*
    *  document ready
    *
    *  This function will render each map when the document is ready (page has loaded)
    *
    *  @type	function
    *  @date	8/11/2013
    *  @since	5.0.0
    *
    *  @param	n/a
    *  @return	n/a
    */
    // global var
    var map = null;
    
    $(document).ready(function(){
    
    	$('.acf-map').each(function(){
    
    		// create map
    		map = new_map( $(this) );
    
    	});
    
    });
    
    });
    </script>
    
    	<?php if( have_rows('locations') ): ?>
    		<ul class="buttons">
    			<?php while ( have_rows('locations') ) : the_row();
    			$location = get_sub_field('location');
    			// Create identifiers for markers [id=m#]
            $i++;
            ?>
    				<li class="view_location" id="m<?=$i?>" data-lat="<?php echo $location['lat']; ?>" data-lng="<?php echo $location['lng']; ?>"><?php the_sub_field('title'); ?></li>
    			<?php endwhile; ?>
    		</ul>
    		<div class="acf-map">
    			<?php while ( have_rows('locations') ) : the_row();
    				$location = get_sub_field('location');
    
    				?>
    				<div class="marker" data-lat="<?php echo $location['lat']; ?>" data-lng="<?php echo $location['lng']; ?>">
    					<h4><?php the_sub_field('title'); ?></h4>
    					<p class="address"><?php echo $location['address']; ?></p>
    					<p><?php the_sub_field('description'); ?></p>
    				</div>
    		<?php endwhile; ?>
    		</div>
    	<?php endif; ?>
  • Will this be able to sync the values entered into the fields? So if for example I update a custom field on Site B, then that value would be saved and synced to Site A too.

    Absolutely. This is its primary purpose.

  • This reply has been marked as private.
  • I think I got it: if I move the is_user_logged_in() part to the bottom of the form, just before the submit button, it correctly passes the user ID to acf_create_new_user(). I suppose that all those instances of acf_form() reset the post ID?

    Do you have any thoughts on my other question, why wp_redirect() isn’t working after form submission? To reiterate, I don’t want to use acf_form( 'return' => 'some-url' ) because the redirects are conditional (e.g. user already exists, user was registered, etc.).

  • I’m looking to do something similar, but I’m focusing on the use case that the user will enter either a product name or a SKU. Name is the title field, so that will come back in results by default. If they enter a SKU, that’s an ACF field.

    So, what I’m doing is firing a query against the default $args array, and if it contains posts, I return that array as-is.

    If it doesn’t return any posts, then I overwrite $args with a new array that looks at my custom field, and also removes the ‘s’ argument to kill the default search functionality.

    It seems to be working pretty well given this use case. I tried to combine both conditions using post__in, but you still wind up with the situation where you have the ‘s’ param searching in title and content, and creating a default ‘AND’ relationship with the meta_query. Also, if the title/content search succeeds, it never looks at the SKU, but that’s not an issue in my case since the SKU value is somewhat human un-readable and would make for a pretty lousy product title.

    My code is below for reference:

    
    function my_related_query($args, $field, $post_id) {
    	
    	if ($args['s'] == '') {
    		// nothing passed in, so just return $args as it stands and get out of here.
    		return $args;
    	}
    	
    	// check for posts using $args
    	$result = new WP_Query($args);
    	if ($result->found_posts == 0) {
    		
    		// no posts found for the query, so it might be a sku... take a look there?
    		$args['meta_query'] = array(
    			array(
    				'key' => 'sku',
    				'value' => $args['s'],
    				'compare' => 'like'
    			)
    		);
    		$args['posts_per_page'] = -1;
    		$args['s'] = '';
    		
    	}
    	
    	return $args;
    	
    	
    }
    add_filter('acf/fields/relationship/query/name=related', 'my_related_query', 10, 3);
    
  • OK Here’s what I have tried. The concept that I am going with is that the “Page Link” custom field type stores the URL in a string and we just need to query by that meta value. But, it’s not working.

    Any ideas?

    
    <?php 
    $currentURL = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
    
    $args = array(
    	'numberposts'	=> -1,
    	'post_type'		=> 'on_site_optimization',
    	'meta_query'	=> array(
    		'relation'		=> 'AND',
    		array(
    			'key'		=> 'website_parent',
    			'value'		=> $currentURL,
    			'compare'	=> 'LIKE'
    		),
    	)
    );
    
    // query
    $the_query = new WP_Query( $args );
    
    ?>
    <?php if( $the_query->have_posts() ): ?>
    	<ul>
    	<?php while( $the_query->have_posts() ) : $the_query->the_post(); ?>
    		<li>
    			<a href="<?php the_permalink(); ?>">
    				<?php the_title(); ?>
    			</a>
    		</li>
    	<?php endwhile; ?>
    	</ul>
    <?php endif; ?>
    
    <?php wp_reset_query(); ?>
    
  • I think I have the same issue. I have a repeater in the options area of site #12 and I can’t access it from site #2.

    
    // on site 12, works, displays all the data in the repeater
    print_r( get_field( 'featured_items', 'options' ) );
    
    
    // on site 2, only shows "6", the number of items in the repeater
    switch_to_blog( 12 );
    print_r( get_field( 'featured_items', 'options' ) );
    restore_current_blog();
    
    
    // on site 2, this is a text field, this works
    switch_to_blog( 12 );
    print_r( get_field( 'company_name_long', 'options' ) );
    restore_current_blog();
    
  • OK, happy days. I found this handy little snippet. I applied it, changed out the values with mine and added it to my functions.php file.

    
    	// populate acf field (select_post) with post types (clients)
    
    	function acf_load_select_post( $field ) {
    		$field['choices'] = get_post_type_values( 'clients' );
    		return $field;
    	}
    	add_filter( 'acf/load_field/name=select_post', 'acf_load_select_post' );
    
    	function get_post_type_values( $post_type ) {
    		$values = array();
    		$defaults = array(
    			'post_type' => $post_type,
    			'post_status' => 'publish',
    			'posts_per_page' => -1,
    			'orderby' => 'title',
    			'order' => 'ASC'
    		);
    		$query = new WP_Query( $defaults );
    		if ( $query->found_posts > 0 ) {
    			foreach ( $query->posts as $post ) {
    				$values[get_the_title( $post->ID )] = get_the_title( $post->ID );
    			}
    		}
    		return $values;
    	}
    

    This populates the Select field on the page called Home. Huzzah!

    Now I just need to create my home page template to populate the page with the content of the selection. That would be the index.php file, right?

  • Sometimes it just takes another set of eyes, don’t be sorry. Me being able to see that error so quickly should tell you that I’ve made the same mistake myself, and it was quite frustrating looking at code and not being able to figure out what was causing it. The human brain is a funny thing, a lot of times it sees what it expects to see rather than what’s really there.

  • I had a thought and it goes something like this…

        Using ACF, create a new Custom Field. I’ve just called it Home.
        Create a new Field. I’ve called that Select Post (select_post).
        Choose the Field Type: Select.
        Under Location – Rules, set it to Page is equal to Home.
        Make a single page index template.

    A problem with this plan is, how to fill that Select field, with all the custom posts.

    If I can figure that out, then I’ve allowed the admin to be able to simply choose a post from a dropdown, without any further WP knowledge and bypassing the standard Page dropdown under Reading.

    I’ll start Googling and trying different things, but do you think this is achievable?

  • So, a guy on wordpress.stackexchange.com gave me this hint:

    $args_jewellery = array(
        'post_type' => 'jewellery',
        'numberposts'   => -1,
        'meta_query' => array(
            array(
                'key' => 'select_brands',
                'value' => '"' . $brand . '"',
                'compare' => 'LIKE'
            )
        )
    );

    Now, why would it take me three days to wrap that shit in quotation marks and use LIKE?

    Sigh!

  • OK. I got this working. Here is my code for reference.

    <?php
    $tasks = get_posts(array(
    	'post_type' => 'task',
    	'meta_query' => array(
    		array(
    			'key' => 'task_parents', // name of custom field
    			'value' => '"' . get_the_ID() . '"', // matches exaclty "123", not just 123. This prevents a match for "1234"
    			'compare' => 'LIKE'
    		)
    	)
    ));
    
    ?>
    <?php if( $tasks ): ?>
    	<table>
    	<?php foreach( $tasks as $tasks ):?>
    		<tr>
    			<td>
    				<a href="<?php echo get_permalink( $tasks->ID ); ?>">
    					<?php echo get_the_title( $tasks->ID ); ?>
    				</a>
    			</td>
    		</tr>
    	<?php endforeach; ?>
    	</table>
    <?php endif; ?>
  • I’ve created a Field Group called Brands, in that field group I’ve added a field with a field name of ‘select_brands’ of type Checkbox (to be able to select multiple). In the choices I’ve added this:

    
    bolou : Bolou
    c6 : C6
    deakin francis : Deakin Francis
    fope : FOPE
    piero milano : Piero Milano
    

    From this I generate a drop down on the front page, along a second drop down generated from categories (the built in widget in the admin interface).

    So, now I can pick – from two different drop downs – a combination of both Brand and Category. But, – I’ve now tried for three days to get the posts from the DB without any luck (yes, luck is actually what I’m counting on now, ’cause nothing seem to make sense).

    Now, from the 3.456.745 different versions of an $args array I’ve tried, none work. Now, I need some help understanding what parameters I actually need to put where, as I simply can’t grasp why this is so hard to get going.

    I would expect the following to get me; posts of custom post type ‘jewellery’ with a category of whatever is selected in the category drop down ($category is an integer from the option value in the drop down) that is also of a specific Brand ($brand is a string from the above options from the custom field).

    
    $args_jewellery = array(
    	'post_type' => 'jewellery',
    	'numberposts'	=> -1,
    	'tax_query' => array(
    		array(
    			'taxonomy' => 'category',
    			'terms' => array( $category )
    		)
    	),
    	'meta_query' => array(
    		array(
    			'key' => 'select_brands', <-- IS THIS RIGHT?
    			'value' => $brand <-- AND IS THIS?
    		)
    	)
    );
    

    How do the fields below relate to my custom post type and its value?

    'meta_query' => array(
    	array(
    		'key' => 'select_brands', <-- ?
    		'value' => $brand <-- ?
    	)
    )
    

    And WHY isn’t this working? What is it that I’m totally missing here … ?

  • I’m using trial and error with values I know exists:

    But no matter what I put it, I still get nothing:

    $args_jewellery = array(
    	'post_type' => 'jewellery',
    	'numberposts'	=> -1,
    	'tax_query' => array(
    		array(
    			'taxonomy' => 'category',
    			'terms' => array( 6 ) // Also tried category name as string 'Ringe', no difference
    		)
    	),
    	'meta_query' => array(
    		array(
    			'key' => 'select_brands',
    			'value' => 'bolou'
    		)
    	)
    );
    

    If I remove the meta_query part I get all rings. But why isn’t the meta_query part working – these are values I know exists. But I don’t know if that’s what I’m supposed to use!?

    ANY help is appreciated!

  • For the sake of needing this at a later point:

    	$field_key = "field_5925c714d03e7"; // <-- Find this by turning on Field Keys under Screen Options in admin interface
    	$field = get_field_object($field_key);
    
    	if( $field ) {
    		echo '<select id="brands" name="brands" class="brands" autocomplete="off">';
    
    		echo '<option value="-1">All</option>';
    
    		foreach( $field['choices'] as $k => $v ) {
    			echo '<option value="' . $k . '"';
    
    			if ( $k == $brand ) {
    				echo ' selected';
    			}
    			echo '>' . $v . '</option>';
    		}
    
    		echo '</select>';
    	}
    
  • Hi John,

    Thanks for this and thanks for the clarification. (also apologies for my delayed response).

    I’ve since updated my functions to reduce post_meta functions. However, I use acf_forms in multiple locations and I have them perform multi-relational actions (not just on the post_id itself, but rather ‘connected’ ones)…either way I’m rolling out functions to simplify this. Essentially, based on the page within which I embed an acf_form, I need acf/save_post to perform different actions without interfering with each other.

    My main question was if it was better (more performant) to use conditionals within one add_action hook or have multiple add_action hooks, i.e.

    
    function my_acf_save_post( $post_id ) {
        
        if($post_id = 1){
    	    // do something 1
        }
        if($post_id = 2){
    	    // do something 1
        }
        if($post_id = 3){
    	    // do something 1
        }
        if($post_id = 4){
    	    // do something 1
        } 
        
    }
    
    add_action('acf/save_post', 'my_acf_save_post', 20);
    

    vs.

    
    function my_acf_save_post_1( $post_id ) {
        
        // do something 1
    }
    
    add_action('acf/save_post', 'my_acf_save_post_1', 20);
    
    function my_acf_save_post_2( $post_id ) {
        
        // do something 2
    }
    
    add_action('acf/save_post', 'my_acf_save_post_2', 20);
    
    function my_acf_save_post_3( $post_id ) {
        
        // do something 3
    }
    
    add_action('acf/save_post', 'my_acf_save_post_3', 20);
    
    function my_acf_save_post_4( $post_id ) {
        
        // do something 4
    }
    
    add_action('acf/save_post', 'my_acf_save_post_4', 20);
    

    I personally prefer multiple ‘if’ functions because I’ve found that without that some forms seem to interfere with each other (I have a lot of them and sometimes multiple forms on the same page).

  • Thanks! Got it working, mostly.

    calling acf_form_head() is required before get_header() or before any output is created in header.php

    Noted. I’ve moved this bit to the page template, called it in at right place.

    // if you don’t want the above fields also saved as custom fields for the user

    Regarding unset(), good call. I wish I had done this sooner, now I have hundreds of instances of orphaned post data I have to delete…

    I’ve simplified the code (and also complicated it a bit). It’s solved most issues, created others.

    
    <?php
    
    // set ACF pre-save stuff
    add_filter('acf/pre_save_post' , 'acf_create_new_user' );
    
    function acf_create_new_user( $post_id ) {
    
        error_log( 'post id: ' . $post_id);
    
        // // exit if user already logged in, front-end form sent his ID
        // if ( $post_id != 'new' ) {
    
        //     error_log( 'post not new: ' . $post_id);
        //     // wp_redirect( home_url() );
        //     return $post_id;
    
        // }
    
        $field_group = acf_get_fields_by_id('302');
    
        //prepare basic user info
        foreach ( $field_group as $key => $value ) {
    
            $$value['name'] = $_POST['acf'][$value['key']];
    
        }
    
        $password           = wp_generate_password( 10, true, false );
        $username           = $candidate_email;
        $display_name       = $candidate_given_name_romaji . ' ' . $candidate_family_name_romaji;
    
        // check if user exists, exit if so
        if ( email_exists( $candidate_email ) ) {
    
            error_log( 'email exists: ' . $candidate_email );
            wp_redirect( home_url() . '/sign-up/?email_exists&user_email=' . $candidate_email );
            return $candidate_email;
    
        }
    
        // register user
        $user_id = wp_create_user( $username, $password, $candidate_email );
        error_log( 'guy just created: ' . $user_id );
    
        // update other user info
        wp_update_user( array(
    
            'ID'            => $user_id,
            'first_name'    => $candidate_given_name_romaji,
            'last_name'     => $candidate_family_name_romaji,
            'display_name'  => $display_name
    
        ) );
    
        // update other user meta, prevent creation of post data
        foreach ( $field_group as $key => $value ) {
    
            update_user_meta( $user_id, $value['name'], $_POST['acf'][$value['key']] );
            unset( $_POST['acf'][$value['key']] );
    
        }
    
        // redirect to login page after finish
        wp_redirect( home_url() . '/sign-in/?new_user=true&user_email=' . $candidate_email );
        return 'user_' . $user_id;
    
    }
    
    ?>
    

    (1) // exit if user already logged in bit has been commented out because it just grabs the ID of the page, not the post data. I can’t remember if this was working before.

    (2) wp_redirect() bits are not working. I know I can add redirects to acf_form() but they’re conditional: a successful registration will go to one place and an unsuccessful one will go elsewhere.

    Please note, the way I use acf_form() code has changed. I ran into some issues with calling in an entire field group and had to call in the fields one-by-one. It looks like this:

    
    <?php
    
    // ensures that user is not logged in
    if ( is_user_logged_in() == true ) : 
        global $current_user;
        $user_id = 'user_' . $current_user->ID;
    else : $user_id = 'new';
    endif;
    
    acf_form( array(
        'post_id' => $user_id,
        'form' => false,
    ) );
    
    ?>
    

    Then further down the page I have a bunch of these:

    
    <?php
    acf_form( array(
        'fields' => array('some_acf_field'),
        'form' => false,
    ) );
    ?>
    
  • hey scheurta

    i would like to get help if you can 🙂

    this is my code
    <?php
    // The Query
    $user_query = new WP_User_Query( array( ‘role’ => ‘Dj’ ) );
    $map_location = get_field( ‘map_pin’ );
    echo $map_location[‘address’];
    // User Loop
    if ( ! empty( $user_query->results ) ) {
    foreach ( $user_query->results as $user ) {
    echo ‘<p>’ . $user->display_name . ‘</p>’;
    echo ‘<p>’ . $user->$map_location[‘address’] . ‘</p>’;

    }
    } else {
    echo ‘No users found.’;
    }
    ?>

    the code is showing the display name of the users and i am trying to show all the addresses of those users but some how the code that you wrote here does not work at the code that i wrote.

    can you please help with this?

  • Thanks, John, but I have to adapt this to my code to fully understand it:

    I have a range of jewellery (using a Custom Post Type = jewellery) that I separate by Category (using the built in Categories of WP) and Brands (using a custom field to select the brand in the custom post type).

    From that I create two separate drop downs: Category with values of IDs of categories, and Brands with values of strings.

    So if the user select a Brand from one of the selects and a Category from the other select, the code you provide should look like this, right:

    'post_type' => 'jewellery',
    'tax_query' => array(
    		array(
    			'taxonomy' => 'category',
    			'terms' => array( $category ) // <-- THIS I GET FROM $_GET['c']
    		)
    	),
    	'meta_query' => array(
    	array(
    		'key' => 'select_brand', // <-- WHAT DO I PUT HERE (THE CUSTOM FIELD VALUE, this is what I named it.)
    		'value' => $brand // <-- THIS I GET FROM $_GET['b']
    	)
    )
    
  • I was going to answer last night but then I thought the way you worded it that it might be another taxonomy.

    Anyway, you just need to add both a tax query and a meta query

    
    $args = array(
      'post_type' => 'product', // or whatever
      'tax_query' => array(
        array(
          'taxonomy' => 'category',
          'terms' => array($term_id) // must be an array of term IDs
        )
      ),
      'meta_query' => array(
        array(
          'key' => 'brand',
          'value' => 'some brand'
        )
      )
    )
    

    https://codex.wordpress.org/Class_Reference/WP_Query

  • It’s a select.

    What I would ideally like is a copy of the Category widget (just with Brands instead), but I can’t seem to get my head around it. So therefore I’m trying to use a custom field instead, but I’m having serious trouble figuring out how to retrieve post using multiple ‘parameters’.

    I’ve been trying something in this direction:

    
    $args_jewellery = array(
    	'post_type' => 'jewellery',
    	'category' => $category,
    	'numberposts' => -1,
    	'meta_query' => array(
    		array(
    			'key' => $brand
    			// 'compare' => 'EXISTS'
    			)
    	)
    );
Viewing 25 results - 11,101 through 11,125 (of 21,318 total)