Support

Account

Home Forums Search Search Results for 'taxonomy'

Search Results for 'taxonomy'

reply

  • Are you sure what you want to get is the category? I think that WooCommerce uses a custom post type and custom taxonomy for products, but I’m not sure what those are right now.

    You might want to try get_the_terms() https://codex.wordpress.org/Function_Reference/get_the_terms

  • You are setting a custom field on a term. For this you need to include the taxonomy as part of the $post_id. I have altered the update_field() call of your code

    
    function my_create($term_id, $tt_id, $taxonomy) {
      // gets term_data for Post
      $term = get_term($term_id, 'interpret');
      // Create the Post and Save ID to $post_id
      $post = array(
          'post_title' => $term->name,
          'post_content' => $term->slug,
      );
      $post_id = wp_insert_post($post);
      // if Post was created
      if ($post_id) {
        // get Post by ID
        $link_to = get_post($post_id);
        // Post->ID
        $link = $link_to->ID;
        update_field('field_558e8544d5660', $link, 'interpret_'.$term_id);
      }
    }
    
  • Thi is how I solved it:

    <div class="dest-box">
    <div class="rk-row">
    <?php 
    $counter=1;
    $terms = get_terms( 'region' );
     if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
         echo '<div">';
         foreach ( $terms as $term ) {
    		 if ($term->parent > 0) { continue; }
    	   $term_link = get_term_link( $term );
    	   $image = get_field('region_image', $term->taxonomy . '_' . $term->term_id);
    	   $size = 'homepage-thumb';
    	   $thumb = $image['sizes'][ $size ];
    	$width = $image['sizes'][ $size . '-width' ];
    	$height = $image['sizes'][ $size . '-height' ];
    	   echo '<div class="col-md-6">';
    	   echo '<div class="destination-loop">';
    	   echo '<img src="' . $thumb . '"/>';
           echo '<h3><a href="' . esc_url( $term_link ) . '">' . $term->name . '</a></h3>';
    	   echo '<p>' . $term->description . '</p>';
    	   echo '</div></div>'; 
    	   
    	   
    		   if ($counter  == 2) {
      echo '<div class="clearfix"></div></div><div class="rk-row">'; 
      $counter=0;
    
    		} 
    			
         $counter++;        
         
    	 
    	 }
    	 
    	 
     }
     
     ?>
     <div class="clearfix">
    </div></div>
    
    </div>

    Thanks very much mate !

  • You can either change to an image object or ID for return values. My preference is ID but it depends on if I need more than one image size.

    If i’m only going to need one size I use ID and then wp_get_attachment_image_src() http://codex.wordpress.org/Function_Reference/wp_get_attachment_image_src.

    If I’m going to use multiple sizes the I have ACF return the image object.

    For using the image object you would use something like this

    
    $image = get_field('region_image', $term->taxonomy . '_' . $term->term_id);
    $src = $image['sizes'][$size];
    echo '<img src="' . $src .'"/>';
    

    You can see the entire image object to help you work on it by doing this:

    
    $image = get_field('region_image', $term->taxonomy . '_' . $term->term_id);
    echo '<pre>'; print_r($image); echo '</pre>';
    
  • (EDIT: This solution stopped working for me, but I found a new one with Elliot’s help. See my new comment.)

    PROBLEM SOLVED!!

    I found a solution.

    It’s not very pretty, but it certainly works. None of the filters I found in the source code offered a way to extend the arguments sent in a select2 ajax request.

    However, there is a filter to modify the select2 arguments. The annoying part is that the select2 arguments are actually a callback to an ACF function. So the magic here is that we replace that function with a “wrapper” function. This new function still calls the old function, then it adds the “Make” to the results. This data is then sent via ajax, and our filter can now show the correctly models!

    Here is the JavaScript, hard-coded by field ID for the MAKE:

    // Update the ajax arguments for select2 objects to include the Make value
    // This is done by replacing the default ajax.data function with a wrapper, which calls the old function and then appends "vehicle_make" to the results.
    acf.add_filter(
        'select2_args',
        function( args ) {
    
            if ( typeof args.ajax.data == 'function' ) {
                var old_data_func = args.ajax.data; // We'll keep this for maximum compatibility, and extend it.
    
                args.ajax.data = function(term, page) {
                    var default_response = old_data_func( term, page ); // Call the old, default function.
    
                    default_response.vehicle_make = function() {
                        // Add the vehicle make to the ajax function. This happens for all select 2 objects, even if it's blank.
                        return jQuery('#acf-field_55890984e822f').val();
                    };
    
                    // Return the default args with our vehicle_make function.
                    return default_response;
                }
            }
    
            return args;
        }
    );

    Here is the PHP Filter which limits the MODEL by term parent:

    function oss_filter_model_dropdown_admin( $args, $field, $post_id ) {
        // Look for the vehicle make in the AJAX request.
        $make = isset($_REQUEST['vehicle_make']) ? (int) $_REQUEST['vehicle_make'] : false;
    
        // If not found, use the previous vehicle's value.
        if ( $make === false ) $make = get_field( 'make_term', $post_id );
    
        // If no make has been selected, do not show any terms (-1 will never give results)
        // Otherwise, use the make as the parent term. Models are always a child of a make.
        if ( !$make ) $args['parent'] = -1;
        else $args['parent'] = (int) $make;
    
        return $args;
    }
    add_filter( 'acf/fields/taxonomy/query/name=model_term', 'oss_filter_model_dropdown_admin', 20, 3 );

    Here is a video demonstrating the end result, showing that the “Model” field’s options are directly tied to the “Make”.

    http://gfycat.com/RevolvingSleepyBarasingha

  • Thanks for the attempt John. I’ve already got the javascript file included properly, though I did not mention this.

    The problem with the second piece of code is that it does fire, but it fires after you select an option.

    Here is a demonstration: http://gfycat.com/IcyBaggyDore

    In that recording, I expect the console to log the event when the ajax data is prepared (as the filter name describes). This would allow me to say “hey ajax call, why don’t you take the value of MAKE with you!”.

    The problem though, is that it doesn’t fire immediately. It fires after you select an option.

    The reason this is a problem is because I want to filter what shows up in the dropdown, to do this I need to send that value when you click on the dropdown – when it is loading the results – so that I can access that value in my PHP filter (acf/fields/taxonomy/query/name=model_term)

    Here’s what SHOULD happen

    1. User clicks on dropdown
    2. Value of “Make” is added to ajax request
    3. Ajax request is fired, return a list of options
    4. Dropdown is populated with the returned options
    5. User selects one of the “Models” displayed, which are direct descendents of the “Make” from step 2.

    And here is what is CURRENTLY happening:

    1. User clicks dropdown
    2. The make is absent from AJAX request
    3. Ajax request is fired, but we cannot filter by the make
    4. Dropdown is populated with either ALL terms, or terms from the PREVIOUSLY SAVED MAKE.
    5. User is confused because either the wrong models are appearing, or a tremendous list (1300 items) is loaded.

  • 
    <?php 
    $args = array(
        'taxonomy'     => 'sock-club',
        'hide_empty'   => '0'
          );
    $terms = get_terms('sock-club', $args);
    if ($terms) {
        foreach ($terms as $index => $term) {
            $terms[$index]->star_rating = get_field('star_rating', $taxonomy.'_'$term_id);
          
        }
        usort($terms, 'my_sort_function');
        
        // create the list after the terms are sorted
        // need to loop through them again
        $term_list = '';
        foreach ($terms as $term) {
            $term_list .= '<a href="' . get_term_link( $term ) . '" title="' . sprintf( __( 'View all post filed under %s', 'my_localization_domain' ), $term->name ) . '">' . $term->name . '</a>';
        }
          
    }
    
    ?>
    
  • Hi @timothy_h

    Yep, looks like WP only searches the post_title and post_content.
    https://core.trac.wordpress.org/browser/tags/4.2.2/src/wp-includes/query.php#L2091

    You may need to hook into a filter to add a custom search to the SQL string. I believe WP contains some filters to modify the SQL, but I’m not sure off the top of my head.

    The get_terms function seems to search for name and slug, so this explains the difference. https://core.trac.wordpress.org/browser/tags/4.2.2/src/wp-includes/taxonomy.php#L1899

    Hope you can find a solution to extend the WP posts search and I hope to heave cleared up the difference

    Thanks
    E

  • Thank you John,

    As a beginner I am still not able to get it working this way. This is the code I have:

    In my functions.php file:

        function my_sort_function($a, $b) {
            if ($a->star_rating == $b->star_rating) {
                return 0;
            } elseif ($a->star_rating < $b->star_rating) {
                return -1;
            } else {
                return 1;
            }
        }

    On my home page:

    <?php 
    $args = array(
        'taxonomy'     => 'sock-club',
        'hide_empty'   => '0'
          );
    $terms = get_terms('sock-club', $args);
    if ($terms) {
        foreach ($terms as $index => $term) {
            $terms[$index]->star_rating = get_field('star_rating', $taxonomy.'_'$term_id);
        
            $term_list .= '<a href="' . get_term_link( $term ) . '" title="' . sprintf( __( 'View all post filed under %s', 'my_localization_domain' ), $term->name ) . '">' . $term->name . '</a>';
          
        }
        usort($terms, 'my_sort_function');
    }
    
    ?>

    Can you see what is wrong with my code?

  • You can’t order terms by a custom field.

    orderby for get_terms() only accepts one of the argument listed on the codex page https://codex.wordpress.org/Function_Reference/get_terms

    In order to do this you need to get the terms, then loop through the terms, get your custom field and add it to each term object and then build a PHP function to order them.

    I’ve done this a few times

    
    function my_sort_function($a, $b) {
        if ($a->star_rating == $b->star_rating) {
            return 0;
        } elseif ($a->star_rating < $b->star_rating) {
            return -1;
        } else {
            return 1;
        }
    }
    
    $terms = get_terms($taxonomy, $args);
    if ($terms) {
        foreach ($terms as $index => $term) {
            $terms[$index]->star_rating = get_field('star_rating', $taxonomy.'_'$term_id);
        }
        usort($terms, 'my_sort_function');
    }
    
  • Yes, the id that gets pulled in as a parenthetical is the slug of a taxonomy term. But the parenthetical on the post object is the post_name of the post.

  • W0000t?!? :O

    Its a taxonomy field and looks like this

    array (
    	'key' => 'field_55411b948f60d',
    	'label' => 'Select Story',
    	'name' => 'story_post',
    	'prefix' => '',
    	'type' => 'taxonomy',
    	'instructions' => '',
    	'required' => 0,
    	'conditional_logic' => 0,
    	'wrapper' => array (
    		'width' => '',
    		'class' => '',
    		'id' => '',
    	),
    	'taxonomy' => 'story',
    	'field_type' => 'select',
    	'allow_null' => 1,
    	'add_term' => 1,
    	'load_save_terms' => 1,
    	'return_format' => 'id',
    	'multiple' => 0,
    ),

    Can you share your example?

  • I just tried setting this up something similar to test and it’s working correctly with the code that you originally supplied. The only thing I’m not sure of is the field type at this point. Is this with a Taxonomy field or something else?

  • Been playing around and so far this is my code that works, it gets the value of the checkbox but only on the selected/saved one. The code below gives me “southern 360” where 360 is the value of the saved/selected checkbox and southern is the correct region if based on the 360 ID. This is the same on all checkbox divs though and what I need is for each checkbox div to show the ID value of its respective checkbox.

    How can I change it so that it will add the value of the checkbox per checkbox and not the selected one?

    function filter_region_taxonomy() { ?>
    <script type="text/javascript">
    (function($) {
    // Document Ready
    $(document).ready(function() {
    $('#acf-standard_region select').trigger('change');
    });
    $('#acf-standard_region .acf-taxonomy-field select').live('change', function() {
    var value = $(this).val();
    $( "#acf-standard_company:checkbox" ).addClass("compclass");
    
    if( value == "7" ) {
    $( "#acf-standard_company ul.acf-checkbox-list li ul.children li" ).removeClass( "southern no-class" ).addClass(("<?php $terms = get_the_terms( get_the_ID(), 'company'); if( !empty($terms) ) { $term = array_pop($terms); $term_id = $term->term_id; $regionVal = get_field('jobsite_region', 'company_' . $term_id ); echo $regionVal . ' ' . $term_id; } ?>"); 
    $('#acf-standard_company').show();
    $('ul.acf-checkbox-list li').show();
    }
    else if( value == "9" ) {
    
    $( "#acf-standard_company ul.acf-checkbox-list li ul.children li" ).removeClass( "northern no-class" ).addClass(("<?php $terms = get_the_terms( get_the_ID(), 'company'); if( !empty($terms) ) { $term = array_pop($terms); $term_id = $term->term_id; $regionVal = get_field('jobsite_region', 'company_' . $term_id ); echo $regionVal . ' ' . $term_id; } ?>");
    $('#acf-standard_company').show();
    $('ul.acf-checkbox-list li').show();
    }
    else {
    $( "#acf-standard_company ul.acf-checkbox-list li ul.children li" ).removeClass( "southern northern" ).addClass( "no-class" );
    $('#acf-standard_company').hide();
    $('ul.acf-checkbox-list li').hide();
    }
    
    });
    
    })(jQuery);
    </script>
    
    <?php }
    
    add_action('acf/input/admin_head', 'filter_region_taxonomy');
  • Hi @timothy_h

    Is it possible that the taxonomy term in question contains the string ‘133’ somewhere in it’s description, slug, name or any other DB data?

  • Here are screenshots of the sub-fields. I’m adding a parenthetical to the search results for the taxonomy and post object fields. In the taxonomy field, you can see the underlining indicating that the parenthetical was included in the search term. This is not happening for post objects.

  • Hi @cjf9

    You need to do the same for the tax field as with all your others and set the second parameter the same way.

    
    <?php 
    $queried_object = get_queried_object(); 
    $taxonomy = $queried_object->taxonomy;
    $term_id = $queried_object->term_id;  
    $related_terms = get_field('quick_links', $taxonomy . '_' . $term_id);
    if( $related_terms ): ?>
    
    	<ul>
    
    	<?php foreach( $related_terms as $related_term ): ?>
    
    		<a href="<?php echo get_term_link( $related_term ); ?>"><?php echo $related_term->name; ?></a>
    
    	<?php endforeach; ?>
    
    	</ul>
    
    <?php endif; ?>
    
    
  • The best solution to this is not to use a custom field but to use custom taxonomy.

    For information on taxonomies see https://codex.wordpress.org/Taxonomies

    Create a custom taxonomy named “health-disorder” then create a term for each disorder. Then you can assign your products to one or more terms and this will allow sorting and display by the terms.

  • Yeah, I wanted a dropdown list of posts from a given post category. I was able to accomplish it by using the Field Type Post Object.

    I had it set to filter by post type (Post) and then filter by Taxonomy (Specific Category)

    Then I created a set of custom fields for that specific category for other information I wanted to display/output.

    All set. Thanks for attempting to assist.

  • If type_metier is a taxonomy field set to display as checkboxes,

    $terms = get_field('type_metier');
    if ( $terms ) {
      foreach ( $terms as $t) {
        $term = get_term( $t, 'taxonomy' );
        echo $term->name . '<br>';
      }
    }

    If you’re trying to just print values from checkboxes, it would be something like,

    $values = get_field('type_metier');
    if ( $values) {
      foreach ( $values as $v) {
        echo $v . '<br>';
      }
    }
  • Thanks! That filter works great for taxonomies, but I noticed a little inconsistency between the result filter for taxonomy fields and the one for post object fields. When I’m searching within select2, whatever I’ve appended to the result via this filter is searched and included. But with post objects the search is done before the result filter is called.

    So if I had “Some entity (P012)” in both a taxonomy field and post object field–the parenthetical would be added by the result filter–searching for “P012” would only return this result in the taxonomy field, not in the post object field. Is this expected?

  • Hi @Jonathan.

    There is no difference.
    When I export the working page_link element from the GUI I get this code:

    array (
        'key' => 'field_557ff199ab6a8',
        'label' => 'Link_aus_GUI',
        'name' => 'link_aus_gui',
        'type' => 'page_link',
        'instructions' => '',
        'required' => 0,
        'conditional_logic' => 0,
        'wrapper' => array (
            'width' => '',
            'class' => '',
            'id' => '',
        ),
        'post_type' => array (),
        'taxonomy' => array (),
        'allow_null' => 0,
        'multiple' => 0,
    ),
  • Hi @timothy_h

    Yep, this filter already exists for taxonomy:
    acf/fields/taxonomy/result

  • Hi @timothy_h

    In ACF PRO (not sure what version I added the filter in), you can modify the ‘text’ of each result via the filter:
    acf/fields/taxonomy/result

    This is how ACF uses the filter:

    
    // filters
    		$title = apply_filters('acf/fields/taxonomy/result', $title, $term, $field, $post_id);
    		$title = apply_filters('acf/fields/taxonomy/result/name=' . $field['_name'] , $title, $term, $field, $post_id);
    		$title = apply_filters('acf/fields/taxonomy/result/key=' . $field['key'], $title, $term, $field, $post_id);
    

    Hope that helps

    Thanks
    E

  • I’m not exactly sure what it is that you’re asking.

    If you are still looking for help with this could you elaborate further on what you’re doing? Do you want to hide a field, like conditional logic, if it is for a top level term in a taxonomy?

    ~JH

Viewing 25 results - 2,501 through 2,525 (of 3,191 total)