Support

Account

Forum Replies Created

  • You are correct that get_field() will not automatically work because you are on the admin side, however, it is possible to pass the post_id you want the value from as a parameter:

    global $post;
    get_field('some_field', $post->id);

    There is however still an issue with that global post. Especially on post creation this might not work properly and another issue is that you could only see results by saving your changes and then refreshing the page.

    My advice is to load some custom javascript in the admin and add/change/remove classes on the admin body using jQuery (or just vanilla).

  • I have experimented a bit and I can confirm the following works for regular categories:

    $GLOBALS['my_query_filters'] = array(
    	'field_1' => 'id_category_mercator',
    );
    
    add_action('pre_get_terms', 'my_pre_get_terms', 10, 1);
    function my_pre_get_terms( $query ) {
        
    	// bail early if is in admin
    	if( is_admin() ) return;
    	
    	// bail early if not main query
    	// - allows custom code / plugins to continue working
    	//if( !$query->is_main_query() ) return;
    
    	// get meta query
    	$meta_query = $query->meta_query;
    	
    	// loop over filters
    	foreach( $GLOBALS['my_query_filters'] as $key => $name ) {
    		
    		// continue if not found in url
    		if( empty($_GET[ $name ]) ) {
    			continue;
    		}
    		
    		// get the value for this filter
    		// eg: https://mywebsite/wp-json/wc/v3/products/category?id_category_mercator=merca1
    		$value = explode(',', $_GET[ $name ]);
    		
    		
    		// append meta query
        	$meta_query->queries[] = array(
                'key'		=> $name,
                'value'		=> $value,
                'compare'	=> 'IN',
            );
            
    	} 
    	
    	// update meta query
    	$query->meta_query = $meta_query;
    }

    You might want to figure something out to replace the commented out line of code:
    //if( !$query->is_main_query() ) return;

    But I don’t know what other functionality your project has that could cause/suffer conflicts.

  • I believe my earlier example could work, I have modified the code from the acf-article and I believe this should work:

    $GLOBALS['my_query_filters'] = array( 
    	'field_1' => 'id_category_mercator',
    );
    
    add_action('pre_get_terms', 'my_pre_get_terms', 10, 1);
    function my_pre_get_terms( $query ) {
        
    	// bail early if is in admin
    	if( is_admin() ) return;
    	
    	
    	// bail early if not main query
    	// - allows custom code / plugins to continue working
    	if( !$query->is_main_query() ) return;
    	
    	
    	// get meta query
    	$meta_query = $query->get('meta_query');
    
    	
    	// loop over filters
    	foreach( $GLOBALS['my_query_filters'] as $key => $name ) {
    		
    		// continue if not found in url
    		if( empty($_GET[ $name ]) ) {
    			continue;
    		}
    		
    		
    		// get the value for this filter
    		// eg: https://mywebsite/wp-json/wc/v3/products/category?id_category_mercator=merca1
    		$value = explode(',', $_GET[ $name ]);
    		
    		
    		// append meta query
        	$meta_query[] = array(
                'key'		=> $name,
                'value'		=> $value,
                'compare'	=> 'IN',
            );
            
    	} 
    	
    	
    	// update meta query
    	$query->set('meta_query', $meta_query);
    
    }

    All I did was change the $GLOBALS variable at the top to use your acf fieldname, and then I changed the names of the filter and the function.

    I don’t know if this will work because I’m not sure if ACF data on categories is really stored in the term_meta table. But you could add this to your functions.php and try to query the category endpoint with ?id_category_mercator=merca1 we could find out.

  • Is hardcoding your array of options a viable solution?

    $project_scope = array(
    	'key1' => __('value1', 'text-domain'),
    	'key2' => __('value2', 'text-domain'),
    	'key3' => __('value3', 'text-domain'),
    )

    WPML string translation could scan all values wrapped in __() and you can easily translate them.

    If you still want these values to be available in an ACF select field, you can dynamically populate that field using the same source:

    add_filter('acf/load_field/name=project_scope', 'my_project_scope_options');
    function my_project_scope_options( $field ) {
    	global $project_scope;
        $field['choices'] = $project_scope; //You can do this because it already is an associative array
        return $field;   
    }
  • I suspect you are using a third party addon called “Justified Image Grid”?

    What your current code does is replace the image IDs that woocommerce uses to generate the gallery with IDs provided by an ACF field.

    The “Justified Image Grid” field is a field that outputs different content. Even if you managed to get WooCommerce to use the images selected in that field you wouldn’t get the grid that I assume you’re after.

    What you could try however:

    //Remove the default product gallery entirely
    remove_action( 'woocommerce_before_single_product_summary', 'woocommerce_show_product_images', 20 );
    
    //Output the justified image grid instead
    add_action( 'woocommerce_before_single_product_summary', 'my_justified_image_grid', 20 );
    function my_justified_image_grid() {
        the_field('proimages');
    }

    I don’t know if this will work out-of-the-box.

  • Ok I think I understand what you are trying to do, but correct my if I’m wrong.

    You want to retrieve TERMS from the taxonomy CATEGORY, but only those terms that have a specific value filled out in a custom field.

    There is an article in the ACF documentation describing how to do this if you were querying posts instead of taxonomy-terms: https://www.advancedcustomfields.com/resources/creating-wp-archive-custom-field-filter/

    However, I believe the same principle can be applied to pre_get_terms (instead of pre_get_posts).
    The filter pre_get_terms is called inside WP_Term_Query so it should work for api requests: https://developer.wordpress.org/reference/classes/wp_term_query/get_terms/

  • Could you give us some more information?

    I take it you have created a new ACF Field type via building your own plugin?
    Did you use the ‘acf-field-type-template’ ACF hosts on GitHub?

    Could you log/var_dump some information about what is happening on the server at both load_value() and update_value()?

  • I’m not sure I completely understand, but would something like this work?

    function append_url_to_field($new_input) {
      $current_values = get_field('list_of_urls', 'option')?:'';
      if($current_values) {
        $current_values .= ',';
      }
      $current_values .= $new_input;
    }

    You create a simple ‘text’ field on an options page/or other, and simply append it the new value to the existing value.

    Whenever you want to work with the domains you can use explode(',', $field_value) to get an array of urls.

  • This should definitely be possible and I don’t know why your first example shouldn’t work, but I guess that there are 2 possible causes:

    Possibility 1: Is it possible that when/where you call wp_localize_script the $post object is not (yet) available?

    You could check this by doing this:

    wp_localize_script( 'handle', 'object_name', array(
    	'jsVar' => $post->ID
    ));

    If the ‘jsVar’ is not set with the right postID, you have found your problem.

    In that case you could get it to work by doing this for example:

    global $post;
    wp_localize_script( 'handle', 'object_name', array(
    	'jsVar' => get_field('field_key', $post->ID)
    ));

    If that doesn’t work the post might not yet be available at the moment you call wp_localize_script. You should try changing what action you hook this function into. (Since you mention acf_register_block_type() I think you might be calling it directly from the template/render_callbak?)

    You could try this:

    add_action( 'wp_enqueue_scripts', function(){
      'jsVar' => get_field('field_key', $post->ID)
    });

    Possibility 2: Are you 100% sure that the post has a field with field_key?

  • I was going to ask this exact same question, but then I used the search function and found this topic.

    Did you ever find a solution to this? I want to create a block with a sidebar where editors can add some “cards”; a CTA, a related post etc. But I don’t want them to add these cards directly to the editor.

  • Nvm, I think I got it:

    I had already tried adding the class to the property:
    $acf_object_attributes['wpClassName']

    (as you can see in my original post)

    But I had forgotten to also use output this value in the template: $block[‘wpClassName’], rather than expecting $block[‘className’] to have changed.

  • Because your in the editor, the current/global post isn’t the post your editing.

    I use this function (paste to functions.php) to get the current post ID.

    function maybe_editor_post_id() {
    	if ( is_admin() && function_exists( 'acf_maybe_get_POST' ) ) :
    		return intval( acf_maybe_get_POST( 'post_id' ) );
    	else :
    		global $post;
    		return $post->ID;
    	endif;
    }

    I found this code somewhere else on this forum but I forgot where.

    Once you have the actual current post, you can find it’s parent by using wp_get_post_parent_id() or whatever method you prefer.

  • Is this what you are looking for?

    
    acf_get_block_types();
    
    
Viewing 13 posts - 1 through 13 (of 13 total)