Support

Account

Home Forums Search Search Results for 'taxonomy'

Search Results for 'taxonomy'

reply

  • One thing that you have to remember when using attributes is that attributes is a taxonomy and each “attribute” is also taxonomy. So you have nested taxonomies. If you add something like a length “attribute” then each “length” is a term in the “Length” taxonomy.

    So one determining factor is if you can do what you want using terms and all of the associated functionality that goes along with a taxonomy, and also to this withing the functionality provided by WC to work with these attributes.

    ACF field values are not terms they are post_meta and work entirely different to terms.

    Terms are great when you want to create pages that allow you to list all of the posts in a term. They can also be used to build an advanced search that can show posts based on values in 2 or more terms. or use a plugin built by a third party that is able to perform searches of this nature.

    ACF fields are not really good at creating pages devoted to a specific value. Yes, this can be done but it means that you must create custom pages with a customized search application to get it done and finding a plugin that will do this for you will be more difficult.

    As far is importing, if you use attributes then in most cases you can use the WC built in importer. There are also various plugins already available that work with various ERP systems.

    If you use ACF then you will need to get an importer (such as WP All Import Pro) that is aware of ACF fields and can be use to import these fields along with WC content. In all cases you’re going to need to build a custom import template for your data. Automated imports can be set up to run from a URL and you’d need to upload the import file to a URL on a regular bases.

    Another plus of using attributes is the fact that they are built into the WC product interface where ACF fields would be a separate area of the product admin page. This could be seen as more confusing to maintain by users.

    I don’t have an solid answer for you. For me it would depend on exactly what is needed and why. But in general when working with WC I try do as much within WC as possible and only using ACF when I must. The reason for this is that making changes to WC is more complicated than building ACF into some other type of CPT would be.

  • Thanks for the suggestion, I resolved this by merging the two queries.

    
    function get_post_primary_category($post_id, $term='category', $return_all_categories=false){
        $return = array();
    
        if (class_exists('WPSEO_Primary_Term')){
            // Show Primary category by Yoast if it is enabled & set
            $wpseo_primary_term = new WPSEO_Primary_Term( $term, $post_id );
            $primary_term = get_term($wpseo_primary_term->get_primary_term());
    
            if (!is_wp_error($primary_term)){
                $return['primary_category'] = $primary_term;
            }
        }
    
        if (empty($return['primary_category']) || $return_all_categories){
            $categories_list = get_the_terms($post_id, $term);
    
            if (empty($return['primary_category']) && !empty($categories_list)){
                $return['primary_category'] = $categories_list[0];  //get the first category
            }
            if ($return_all_categories){
                $return['all_categories'] = array();
    
                if (!empty($categories_list)){
                    foreach($categories_list as &$category){
                        $return['all_categories'][] = $category->term_id;
                    }
                }
            }
        }
    
        return $return;
    }
    
    $args = array (
        'post_type' => 'article',
        'orderby' => 'posted_date',
        'order' => 'DESC',
        'post_status' => 'publish',
        'posts_per_page' => $totalRecordsToDisplay,
        'meta_query' => array (
            array(
                'key' => 'authors',
                'value' => '"' . get_the_ID() . '"',
                'compare' => 'LIKE'
            )  
        )
    );
    
    $articles = new WP_Query( $args );
    
    if ($articles->post_count < $totalRecordsToDisplay ) {
    
        $new_TotalRecordsToDisplay = $totalRecordsToDisplay - $articles->post_count;
        $article_expertise = get_post_primary_category($post->ID, 'expertise');
        $primary_expertise = $article_expertise['primary_category'];
    
        //echo $primary_expertise->term_id;
        $args = array(
            'post_type' => 'article',
            'orderby' => 'posted_date',
            'order' => 'DESC',
            'post_status' => 'publish',
            'posts_per_page' => $new_TotalRecordsToDisplay,
            'tax_query' => array(
                array(
                    'taxonomy' => 'expertise',
                    'field' => 'term_id',
                    'terms' => $primary_expertise->term_id,
                    //'terms' => array(join(",", wp_list_pluck($expertises, 'term_id'))),
                    'include_children' => true
                )
            )
        );
        $more_query = new WP_Query( $args );
        $articles->posts = array_merge( $articles->posts, $more_query->posts );
        $articles->post_count = count( $articles->posts );
    }  
    
  • You would be better off using another custom taxonomy than trying to use a select field. You cannot use “IN” caparisons with a select field. See section 3. Multiple custom field values (array based values) of this document.

  • This requires giving permission to add terms for the taxonomy anyone that visits your site or to a specific user type that might not have this ability.

    The best, most secure way of doing this on the front end is to have another text field for people that do not already have permission to create a term and then create an acf/save_post action that does the term creation and addition to the post.

  • No, what you want to do is not possible. You cannot relate something to a row in a repeater.

    You should think about using a custom taxonomy for the activity types.

  • Thanks for the pointers.

    Maybe last thing… another way to maybe do it…

    Is there any way to create/store some kind of i) hidden text field and value or ii) field meta information against my Image field on the admin side… ?

    If I could add a “file path” value to the specific Image field itself (to the field “logo”), maybe I could use that to write in and store the value of the uploads child folder for that field (“company/logo”).

    I’m talking about alongside a field’s other standard ACF settings like sizes and “Required?”.

    If so, maybe this is a simpler way to do it than creating a new field group just to put it on edit-tags.php?taxonomy=company

    I see ACF Extended offers a Hidden field that include an admin-side box “Value
    – Default value in the hidden input”…

    But a) this is still separate from my Image field itself and b) it seems designed for accommodating hidden HTML input form fields (perhaps it just stores a piece of arbitrary “Value” that you can render as you like; I’m not sure yet).

  • Hmm…
    So, you can add a custom setting to a field, to all Images…
    I guess you’re saying I would hitch a new field to every Image field in the admin.

    I’m not sure if that would achieve the aim (would it?). Maybe I don’t want to work on *all* Images, ie. if the user has more than one Image field/purpose on-site…

    If the aim is to relocate images uploaded to the Image field in a group that sits on the term-edit page for all Company taxonomy terms… don’t I need to set a folder path value at the Company (taxonomy) level (ie. wp-content/uploads/{specific-folder}/{specific-subfolder}`)?

    I don’t currently have any “taxonomy” (Company) page on which I think a group/field could be added. Unless ACF can put anything on the listing page edit-tags.php?taxonomy=company … Yes, I note ACF Extended enables support for <a href=”https://www.acf-extended.com/features/field-groups/locations/taxonomy-list”>Taxonomy List</a>.

    Hmm, should I be putting a Text field on Taxonomy List for “Logo Image Directory” (eg. “/company/logos“)?

    If I then filter the actual uploads, I’d need some way to check against the above value to get the directory… ?

    Seems a bit convoluted and not necessarily like I could make it a neat plugin package… (?).

    Rather, maybe I shouldn’t bother with <em>specifying</em> an image directory and just <em>infer</em> it…

    ie. 1 /wp-content/uploads/{taxonomy_slug}/{acf_field_name}/{term_slug.ext} ?

    eg. 1 /wp-content/uploads/company/logo/microsoft.png ?

    Yes, though then I think I need to overcome two things…

    <strong>A. Application to other objects beside taxonomy…</strong>

    ie. 2. /wp-content/uploads/user/{acf_field_name}/{username.ext}

    eg. 2 /wp-content/uploads/user/avatar/billgates.png ?

    Achieved through some conditionals/check against object type? Not certain how, maybe the same way I’m already getting the URL parameters.

    <strong>B. Any way to avoid hard-coding/pre-specifying the field name and field key?</strong>

    Can “field_6140a6da30a17” and “avatar“/”logo" be auto-obtained, so that this could just work on anything, I wonder?

    Thinking in public.

  • Hi!
    It’s really old your post but I really would love to be able to make my CPT to act as post and taxonomy at the same time.
    Does this still works?

    Thanks!

  • WC attributes are nothing more than a built in taxonomy system. The attributes you create for example “Author” are child taxonomies.

  • Here’s an example of how we solved this problem:

    1. Pass Selected Parent Terms In AJAX
    You need a JS hook to filter the AJAX request of the child field, and add a new argument that contains the chosen parent term(s):

    
    <?php
    add_action('acf/input/admin_footer', 'support_acf_152814_filter_child_ajax');
    function support_acf_152814_filter_child_ajax()
    {
        global $post;
        //areas where this functionality is needed
        if (!$post || !isset($post->ID) || get_post_type($post->ID) != 'my_posy_type')
            return;
    
    ?>
        <script type="text/javascript">
           (function($){
    
    $(document).ready( function() {
        acf.add_filter('select2_ajax_data', function( data, args, $input, field, instance ){
            let parent_field_key = '<parent_field_key>'; // Parent Field
            let target_field_key = '<child_field_key>'; // Child Field
    
            if( data.field_key == target_field_key ){
                let parent = acf.getField(parent_field_key);
                if (parent) {
                    data['parent_terms'] = parent.val(); //This is an array if the parent field is multiselect
                }
            }
    
              return data;
    
        });
    
    });
    
    })(jQuery);
        </script>
    <?php
    
    }
    

    2. Filter Taxonomy Query of the Child Terms
    Here, you need to catch the argument we introduced above, and modify the query accordingly.
    What I do here, is that I get the ids of all children of the selected parents, and I use put them in the ‘include’ option of the terms query:

    
    add_filter('acf/fields/taxonomy/query/key=child_field_key', 'support_acf_152814_child_terms_filter', 10, 3);
    function support_acf_152814_child_terms_filter( $args, $field, $post_id ) {
        // Check if this is an ajax call
        $children_ids = [];
    	if (wp_doing_ajax()) {
    		if (isset($_POST['parent_terms'])) {
    			$parent_terms = $_POST['parent_terms'];
                if (is_array($parent_terms) && !empty($parent_terms)) {
                    foreach ($parent_terms as $parent_id) {
                        $new_children = get_term_children($parent_id, 'disease_categories');
                        if (!is_wp_error( $new_children )) {
                            $children_ids = array_merge($children_ids, $new_children);
                        }
                    }
                }
                $args['include'] = $children_ids;
    		}
    	
    	}
    
        return $args;
    }
    

    I tested the code above and it works in my case. Please @hube2 let me know if this can be improved further.

  • Thanks a lot John! This kind of input is so much of value, as it’s important to know what to do before figuring out how to do it.

    I discussed this with someone else as well and she suggested even a complete other approach by using WooCommerce attributes instead of CPT and instead of a custom taxonomy. Many opportunities out there.

    Thanks for explaining

  • I don’t see any difference one way or the other except for one thing.

    Lets say that in the future you wanted to group the authors by genre, Sci-Fi, Historical, Non Fiction, etc. It would be pretty much impossible to do this with authors being a taxonomy. On the other hand as a CPT you could add a genre taxonomy attached to the author as well as the books so that both authors and books could be sorted into these terms.

    As far as future proofing it I would go with a CPT.

  • WP does not natively support taxonomies for users. I’m not sure that terms are being saved for the user, but I could be wrong. I would check the DB that values are actually being saved.

    I can’t find any documentation online of making the count of terms on the admin page into a link to the user list page that only shows users with that term. The best thing I can find is this https://codebriefly.com/how-to-create-taxonomy-for-users-in-wordpress/

    I any case, for this to work you would need to use the pre_get_users hook to alter the user query, however WP_User_Query does not support a tax_query. So, even if ACF is saving the terms correctly this information will be useless on the user list page (/wp-admin/users.php). I would say that you could use a meta query here, but that will not work if you are using save/load terms because if this is set then the terms are not saved in post meta. The way I understand it ACF will only save values as object terms or as meta, but not both.

    What you need to find, which I cannot locate, is documentation on how to create an archive page based on a taxonomy that is assigned to users. This might give you an idea of how alter the user query in the admin, but as far as I can find this is not possible.

  • I see two possible issues.

    The first has to do with the taxonomy field. Is it set to save and load terms? If this is not the case then post terms are not set and you will need to instead use a meta query.

    The second issue has to do with

    
    include( locate_template( 'includes/loop.php', false, false ) );
    

    When using include() the global $post will not be defined in the included file. To have this global variable automatically defined in that template then you need to use get_template_part() or you need to manually define the variable in the file with global $post;

  • This is a question that is asked a lot. This is only possible by custom coding. I do not have any examples. This would require adding custom JavaScript to ACF and using the ACF JS API. It would require altering the AJAX values sent when populating the child taxonomy field. It would also require adding an action to the parent taxonomy field that causes ACF to reevaluate the child. As I said, there are no examples of this because no one has figured out how to do it.

    It is possible to use regular select fields, there are examples of doing this for post types, but not taxonomies, but the idea would be the same and you would need to modify them to use a taxonomy. Do a google search for “acf dynamic ajax select example”.

  • I am assigning an Agents taxonomy to Homes CPT. The Agents taxonomy has custom fields for the agents picture, name, email and phone.

    Agents

    I am limited to the themes abilities to return and show items in the grid. Right now it only allows me to show items of a taxonomy. Ideally I would create a CPT Agents and CPT Homes. Then somehow relate the two and show them in the grid.

    Its a cool idea though right? I mean if I can attach the Agents taxonomy to the Home CPT with custom fields for the taxonomy for agent picture, name, email and phone its an easy solution.

    Unfortunately I don’t have coding capabilities and was hoping you would be able to come up with a solution. (i.e. the shortcode)

  • What you are trying to do is beyond the scope and capability of the ACF shortcode. The reason, I assume by reading your problem, is to show values from a term that the post in a CPT is assigned to.

    If you want to do this with shortcodes then you must built your own shortcode that can get the post term in your custom taxonomy that is assigned to the post and get the fields from that term.

    https://developer.wordpress.org/reference/functions/add_shortcode/

  • So how would I go about adding agents to Homes CPT? I thought create Taxonomy called agents and add terms which are the agents names like Jane Doe and John Doe. I attached custom fields to the taxonomy and it all looks good. I just can’t return the agents email, phone, name associated with the Home.

  • You cannot add fields directly to a taxonomy. Fields are added to terms in that taxonomy. You need the term ID of the term where you are entering values into the ACF fields.

  • Where would I find the id for the taxonomy?

  • I have a custom taxonomy called Agents with taxonomy slug agents. I created a custom field group called Agents. The fieldnames for the fields in the group are agent_name, agent_phone, agent_email and agent_picture.

    I tried the above code like so:
    [acf field="agent_name" post_id="agents"]

    This did not return any values. How can I return custom fields attached to the agents taxonomy?

  • I do not have any code or examples or know where to find any.

    You will need to add custom JavaScript to ACF. You will need to alter the AJAX data by getting the value of the taxonomy field and adding it to the data. Then you will need to use that added data in the filter.

    On top of this you will need to figure out how to trigger ACF to refresh that list if the term selected in the taxonomy field is changed. I haven’t got a clue were to start for this. This would probably be the hardest part and I have never seen where anyone has accomplished it.

    If I had to do this I would use 2 dynamically populated select fields and do most of the coding for those fields myself, for instance using the values in the fields to get the correct terms/post. There is a basic example of doing this for posts here that would take some modification to use terms. https://github.com/Hube2/acf-dynamic-ajax-select-example/tree/master/dynamic-select-example

  • My working code filters Courses bases on $post_id of the School taxonomy, however in a case that involves repeaters $post_id would be an ID of my Blog post (as I understand).
    Is it possible to dynamically access the properties of School taxonomy inside of the repeater and if it is – how?

  • Wouldn’t that filter a single post_object field ?
    I basically want to filter each ‘Courses’ post object field by the corresponding ‘School’ taxonomy field
    Attached the image of my repeater structure. In my blog post I have (let’s say) 15 schools

Viewing 25 results - 851 through 875 (of 3,193 total)