Hello,
I’m hoping to get my hands on a custom query that will show only WooCommerce products in a category that are assigned to a User using the ACF User field.
For instance, there is a Product category named “Category X,” and in the User custom field that appears on its Product category settings page, I selected “User X,” creating a relationship between the “Category X” and “User X.”
Now I need a query to show only the products (in “Category X”) for “User X” when they are logged in. This query needs to work for any particular user (I will be assigning different product categories to different users).
I’m trying to use Dynamic.ooo to set this query, but unfortunately the options don’t allow it, so it must be custom.
Preferred Query ID: user_category
ACF Settings:
Field Type: User
Field name: category_user
Filter By Role: Customer
Return Format: User Array (or whatever works best)
Show this field group if: Taxonomy is equal to Category (product_cat)
I tried the below but it is not working:
$args = array(
'posts_per_page' => -1,
'post_type' => 'product',
'taxonomy' => 'product_cat',
//Tries to show products from categories where current user ID is the same as the category_user field
'meta_query' => array(
array(
'key' => 'category_user',
'value' => $current_user->ID,
'compare' => 'LIKE'
)
)
);
Hello,
I’m an ACF pro customer and I’m hoping to get my hands on a custom query that will show only WooCommerce products in a category that are assigned to a User using the ACF User field.
For instance, there is a Product category named “Category X,” and in the User custom field that appears on its Product category settings page, I selected “User X,” creating a relationship between the “Category X” and “User X.”
Now I need a query to show only the products (“Category X) for “User X” when they are logged in. This query needs to work for any particular user (I will be assigning different product categories to different users).
I’m trying to use Dynamic.ooo to set this query, but unfortunately the options don’t allow it, so it must be custom.
Preferred Query ID: user_category
ACF Settings:
Field Type: User
Filter By Role: Customer
Return Format: User Array (or whatever works best)
Show this field group if: Taxonomy is equal to Category (product_cat)
Hello all, I am currently wondering about a conditional InnerBlock based on an ACF true/false field.
I’ve got a block with two child blocks, cards picker (relationship) and cards query (taxonomy / post type). These are then rendered in a parent block (cards) where the true/false will conditionally display one of the child blocks (picker or query)
I know you can’t use two InnerBlocks in a block but for the use case of true/false this seems to work, if you switch the query_type (e.g. query, picker) you lose all populated values on switch, but within the new state it saves fine, so this would be acceptable for my use case. I could just create two blocks and probably will, but it struck me that this might be a useful pattern?
I’m trying to figure out if I should do this, it works, it saves, it renders, and it feels like it would be more useful to the editor to be able to just have one block and chose the type rather than two…..but is there any downsides to this approach in people’s opinion
`
$block_template_picker = array(‘core/acf/cards-picker’)
$block_template_query = array(‘core/acf/cards-query’)
if ( get_field( ‘query_type’ ) == ‘query’ ) :
<InnerBlocks template=”$block_template_picker” />
else :
<InnerBlocks template=”$block_template_query” />
endif;
`
I am trying to show image of taxonomy for custom post type, it is returning “h”
please help me to fix this
below is my code
<?php
$object = 'directory';
$output = 'objects';
$taxonomies = get_object_taxonomies( $object, $output );
$exclude = array('locations');
if ( $taxonomies ) {
foreach ( $taxonomies as $taxonomy ) {
if( in_array( $taxonomy->name, $exclude ) ) {
continue;
}
$terms = get_terms( array(
'taxonomy' => $taxonomy->name,
'hide_empty' => false,
) );
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ) {
foreach ( $terms as $term ) {
$term_image = get_field('image', 'category_' . $term->term_id);
$term_list .= '<div class="cat-col"><a href="' . esc_url( get_term_link( $term ) ) . '" ><figure><img src="' . $term_image['url'] . '"></figure>';
$term_list .= '<div class="cat-box"><div class="cat-box-top"><div class="icon"></div>';
$term_list .= '<h3>' . $term->name . '</h3></div>';
$term_list .= '<div class="cat-box-bottom"><span class="cat-count"><span>0 </span> <span>listings</span></span></div></div></a></div>';
}
echo $term_list;
}
}
}
?>
Hi,
i have an option page with a repeater. In this repeater i have a field taxonomy type with multiple choises.
I need to get values and save in a same field type but on a custom post type.
It works for all other fields but not for this type.
When i enter in the custom post type, i see the correct taxonomy, but it’s not correct infact using wp_query, value is ignored. If i delete that taxonomy and choose it again inside the custom post type, then wp_query works.
When i create custom post type by code and use update_field(“match_valevole_per_titolo”,$match_valevole_per_titolo,$id_post_creato); i obtain in the db this : a:1:{i:0;i:26;}
When i delete the taxonomy from custom post type and choose it again from backend, i obtain in the db : a:1:{i:0;s:2:”26″;}
what i’m doing wrong?
Hi,
I have a form with a drop-down (multi-select) which populates from a taxonomy. The user chooses one or more options from this list, then has to choose a primary option in a second single-select drop down.
How can I populate the second drop down with the selected values from the first? I’ve tried a few JS examples which seem to use repeaters, but couldn’t get any to work.
Many thanks in advance.
Hi,
I need to create a custom post query loop from various custom taxonomy terms set per page with taxonomy fields.
As I am not a senior WP PHP developper, and as I need to move forward on other aspects of the project, I kindly ask you for your help to finish building the query. I almost have it but I am blocking on mixing taxonomies, put relationnal and hierarchical conditions.
The context is this: Website is for a real estate agency, with listing custom posts and custom taxonomies for offer rent/sale (categorie_offre), property type (type_bien) and city (ville). Property type has child taxonomies for Houses, appartments, garage, 1 room, 2 rooms, etc.
In single page editor, I have my taxonomies ACF fields for the 3 taxonomies where I decide what term must filter the listing grid.
In single page template, for now I have this code, that work for only one taxonomy, but not for multiple (I didn’t put cities for now), I think array_merge is not the good solution:
<?php
// Liste d’annonces pages SEO
$post_type = ‘annonce’;
$location_vente = get_field( ‘location_ou_vente’ );
$type_bien = get_field( ‘type_de_bien’ );
$termis = array_merge($location_vente,$type_bien);
foreach( $termis as $termi ) :
$argsi = array(
‘post_type’ => $post_type,
‘posts_per_page’ => 5,
‘tax_query’ => array(
‘relation’ => ‘AND’,
array(
‘taxonomy’ => ‘categorie_offre’,
‘field’ => ‘term_id’,
‘operator’ => ‘OR’,
‘terms’ => $location_vente
),
array(
‘taxonomy’ => ‘type_bien’,
‘field’ => ‘term_id’,
‘operator’ => ‘OR’,
‘terms’ => $type_bien
),
),
);
endforeach;
$annonces = new WP_Query( $argsi );
// Output posts, etc.
while ( $annonces->have_posts() ) : $annonces->the_post();
the_title();
the_excerpt();
echo ‘<br />’;
endwhile;
wp_reset_query();
?>
I am thinking of using the idea of this tread but if some senior developper could give me a hand to achieve it quicker I would be very very very thankfull:
https://wordpress.stackexchange.com/questions/371737/how-to-get-posts-that-contain-multiple-terms-from-multiple-taxonomies
Thanks
Roland
Within Flexible content, I have a taxonomy field type, but this returns ALL categories, which is fine if you only have a few categories of the same type.
But when you have lots of categories that are grouped, showing all the categories is unnecessary and confusing.
So, in my case, I have several categories under “membership levels”, but also other categories for news. In the flexible content field, I want the user to select from a list that only includes the membership levels – having all the “news” categories listed makes no sense.
Is there a way to natively filter this field so it shows a list of taxonomies that includes ONLY the children of a parent category?
Thank you
I have a bunch of post taxonomies for the site I am working on. They are: actors, studios, shows, films, directors, and franchises. They are created with the Custom Post Type UI plugin.
I created an ACF record to display when editing a show taxonomy that would allow a site admin to select actors, so that if viewing a show, you could see stories about the actor in the show. All well and good.
However, I am running into a WP_Query issue. In my custom taxonomy-show.php template, I have a normal WordPress post loop, and underneath, I query ACF get_field(‘actors’) which gives me the list of actors. However, the second I assign a show to a post, the WP_Query blows away the data from ACF, and when looking at the db queries, WP is setting post_meta from the posts in the forloop and causing the ACF field to render blank.
I’ve tried wp_reset_postdata and wp_reset_query on both the have_post forloop and also the ACF query, but to no avail. The second I remove a post from that taxonomy, then the actors list re-appears.
Template is below:
<?php get_header(); ?>
<h1 class="entry-title"><?php single_term_title(); ?></h1>
<div class="archive-meta"><?php if ( '' != the_archive_description() ) { echo esc_html( the_archive_description() ); } ?></div>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<article class="post">
<?php if ( has_post_thumbnail() ) : ?>
<div class="thumb">
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_post_thumbnail(); ?></a>
</div>
<?php endif; ?>
<div class="content">
<h3><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" rel="bookmark"><?php the_title(); ?></a></h3>
<p class="excerpt-entry"><?php echo strip_tags( get_the_excerpt() ); ?></p>
</div>
</article>
<?php endwhile; endif; wp_reset_postdata(); wp_reset_query(); ?>
<h2>Actors</h2>
<ul>
<?php $queried_terms = get_field('actors'); if ($queried_terms): echo 'actors'; foreach($queried_terms as $query_term): setup_postdata($query_term); ?>
<li><a href="<?php echo esc_url( get_term_link( $query_term ) ); ?>"><?php echo esc_html( $query_term->name ); ?></a></li>
<?php endforeach; endif; wp_reset_postdata(); wp_reset_query(); ?>
</ul>
<?php get_footer(); ?>
Has anyone run into this before? I suspect I need to somehow terminate the forloop query to allow ACF to work, but the normal elements recommended by WP aren’t working. I’m trying to build a tag page with posts and other useful information, so I assume it’s possible, but maybe I’m doing it wrong.
I have ACF Fields setup for Post Type while using WooCommerce. Each single Product has unique ACF Fields such as {“location”} value and {“brochure”} value.
I would like to use WPForms’Dynamic Post Type Source’ to create a dropdown list that autopopulates dynamic choices based on the values of these ACF Fields, And ideally only for Products that are In-Stock to reduce size of dropdown list.
DROPDOWN LIST:
Location 1 – Brochure URL
Location 2 – Brochure URL
Location 3 – Brochure URL
Location 4 – Brochure URL
etc
I understand within Advanced settings, Dynamic Choices can be selected for Post Type or Taxomny, but I’m not sure how this can be achieved with ACF Field Values.
The only workaround I have found so far is by adding custom WordPress taxonomy that is only related to products. It would act and show just like categories and tags do and show up on the Products page, which would allow the ability to select it from the Dynamic Choices inside WPForms. However, the major limitation here is that I would need to add the taxonomy to every single existing product, and it doesn’t consider stock status.
function add_custom_taxonomies() {
// Add new "Locations" taxonomy to Posts
register_taxonomy('location', 'product', array(
// Hierarchical taxonomy (like categories)
'hierarchical' => true,
// This array of options controls the labels displayed in the WordPress Admin UI
'labels' => array(
'name' => _x( 'Locations', 'taxonomy general name' ),
'singular_name' => _x( 'Location', 'taxonomy singular name' ),
'search_items' => __( 'Search Locations' ),
'all_items' => __( 'All Locations' ),
'parent_item' => __( 'Parent Location' ),
'parent_item_colon' => __( 'Parent Location:' ),
'edit_item' => __( 'Edit Location' ),
'update_item' => __( 'Update Location' ),
'add_new_item' => __( 'Add New Location' ),
'new_item_name' => __( 'New Location Name' ),
'menu_name' => __( 'Locations' ),
),
// Control the slugs used for this taxonomy
'rewrite' => array(
'slug' => 'locations', // This controls the base slug that will display before each term
'with_front' => false, // Don't display the category base before "/locations/"
'hierarchical' => true // This will allow URL's like "/locations/boston/cambridge/"
),
));
}
add_action( 'init', 'add_custom_taxonomies', 0 );
Hi, I scanned the whole web and found some threads about this, but none of those solutions worked for me.
For example here https://support.advancedcustomfields.com/forums/topic/exclude-taxonomy-terms-from-frontend-display/
I added many variants of this code
add_filter('acf/fields/taxonomy/wp_list_categories', 'my_taxonomy_args', 10, 2);
function my_taxonomy_args( $args, $field )
{
$args['exclude'] = array(16, 15, 17); //the IDs of the excluded terms
return $args;
}
also with specific field name
add_filter('acf/fields/taxonomy/wp_list_categories/name=test_tax2', 'my_taxonomy_args', 10, 2);
function my_taxonomy_args( $args, $field )
{
$args['exclude'] = array(64,65,66); //the IDs of the excluded terms
return $args;
}
and with the query function instead of wp_list_categories
But I always end up with all the terms in the taxonomy field.
Is there any workaround to this?
Thanks!
I tried for hours now to apply a filter to acf/fields/taxonomy/* to have my checkbox taxonomy list with 200 terms split over 3 columns instead of the one that is default. Whatever I tried, nothing worked. How can I achieve it? Thanks.
Hi. I have a client who asked me, if it was possible to have a different UI for the Taxonomy Terms field.
So instead of using the SELECT2 UI, but instead to use the built-in WP UI.
The screenshot shows how the UI is in ACF, but maybe it would be nice to have the option to have the wordpress UI instead, as an option?
https://snipboard.io/9D4gWF.jpg
Hi,
When a taxonomy field is displayed on a page, it is accompanied by a tag link.
I would like to remove the link and only have the text.
I want the value of the field in a data attribute, but i get something like :
data-faq-tag=’Loans, FinancialSupport‘
What i would like is :
data-faq-tag=’Loans FinancialSupport’
how can i do that ?
Hi,
I have a lot of tags and some custom taxonomies.
My question is:
Is it possible to limit the tags selectable in the backend based on the category chosen?
Example:
Let’s say I have a category “books” and a custom taxonomy called “amount”.
If the category “books” is selected I can show the custom taxonomy called “amount” in my backed via the location rules.
But is it possible the show only certain tags from the custom taxonomy “amount” as a checkbox?
Let’s say I also have a category “music album” and the custom taxonomy “amount” contains tags like “100-200 pages”, “10-15 songs”.
If the category “books” is selected I only want to show the tags from the custom taxonomy “amount” which are relevant to the category “books”.
Basically, I not only want to define which custom taxonomys are shown once a category is selected I also want to define which tags from this custom taxonomy can be chosen.
I hope this makes sense.
Best regards,
Alina
The duplicate layout button bypasses the “max” layout limit and allows users to duplicate the layout as many as they want. In my case, I have some layouts that can be added only once per page, like image sliders or custom taxonomy queries, and if duplicated, it breaks the page.
Thanks.
Exist there a hook to change the tax query for the relationship field? This was in some old threads dicussed but only for the resulting query – not for the list in the dropdown.
The workaround would be to get all terms at “load_field” and and this to the taxonomy array – but it would be more easy and more quick to use a hook – or perhaps with an update of the field there should be an option 'hide_empty' => true
;
i tried to change the filter text with some hints here in the forum but it did not work – my new appproch and working solution is this is:
$('select[data-filter="taxonomy"]').each(function() {
$(this)[0][0].text = acf.__('taxonomyfilter'); // allows me to translate the text in php
});
Perhaps there exist a better solution it would be great – BUT another question…..
Why it would not be possible from ACF to adjust the field source:
// vars
$post_type = acf_get_array( $field['post_type'] );
$taxonomy = acf_get_array( $field['taxonomy'] );
$filters = acf_get_array( $field['filters'] );
// filters
$filter_count = count( $filters );
$filter_post_type_choices = array();
$filter_taxonomy_choices = array();
// post_type filter
if ( in_array( 'post_type', $filters ) ) {
$filter_post_type_choices = array(
'' => __( 'Select post type', 'acf' ),
) + acf_get_pretty_post_types( $post_type );
}
// taxonomy filter
if ( in_array( 'taxonomy', $filters ) ) {
$term_choices = array();
$filter_taxonomy_choices = array(
'' => __( 'Select taxonomy', 'acf' ),
);
By adding:
$filtertext = acf_get_array( $field['filtertext'] );
and some if checks..
it would be possible to define the text in PHP and get a more quick solution than to change with jquery. In my project i have some pages with more than 10 relationship fields in a repeater and the “look and feel” is not so nice.
I’m missing a step in a loop. I want to display a the images and links from a custom post type of “Partners”. The CPTs are displayed according to a taxonomy field which returns ‘Field ID’.
The loop returns the right Partner’s but I can’t get the images and URLs to pull through.
Any advice on how I need to modify $the_query portion of my code would be gratefully received.
<div id="partners" class="association-partners" data-aos="fade-up" data-aos-duration="4000" data-aos-easing="ease-in-out">
<h2><?php the_field('pg_title'); ?></h2>
<ul class="partners-list">
<?php
$pgcat = get_field('partner_category');
$args = array(
'post_type' => 'partners',
'posts_per_page' => '-1',
'category__in' => $pgcat,
'orderby' => 'rand',
);
$the_query = new WP_Query ( $args );
?>
<?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<li>
<a href="<?php the_field('youtube_link'); ?>" target="_blank"><p><?php the_title(''); ?></p>
<?php
$image = the_field('partner_logo');
if( !empty( $image ) ): ?>
<img src="<?php echo esc_url($image['url'], $pgcat); ?>" alt="<?php echo esc_attr($image['alt']); ?>" />
<?php endif; ?></a>
</li>
<?php
// Reset the global post object so that the rest of the page works correctly.
wp_reset_query(); ?>
<?php endwhile; endif; ?>
</ul>
</div>
Hello,
I use ACF v. 6.0.6 and I have a strange problem, which occurs when using the ‘posts_join’ and ‘posts_where’ hook in combination with a parameter in the URL.
I have a custom taxonomy “height” where I retrieve posts of a custom post type “person”. The URL looks like this: https://example.com/height/180cm/. Each post has a lot of custom fields. Everything works fine, and the custom fields are displayed correctly.
Now I would like to filter the posts by gender (male and female persons) using a parameter in the URL -> https://example.com/groesse/180cm/?g=Er (respectively ?g=Sie).
For this I have the following code in functions.php (for test purposes I only filtered the men):
add_filter('posts_join','custom_join');
function custom_join($join){
global $wpdb;
$table = $wpdb->prefix."postmeta";
if (is_tax()) {
$join .= "LEFT JOIN $table ON $wpdb->posts.ID = $table.post_id";
}
return $join;
}
add_filter('posts_where', 'gender' );
function gender ( $where )
{
if (isset($_GET['g'])) {
if ($_GET['g'] === 'Er') {
global $wpdb;
$where .= " AND $wpdb->postmeta.meta_key = 'gender' AND $wpdb->postmeta.meta_value = 'He' ";
}
}
return $where;
}
In fact, the posts are now correctly filtered into male and female, here is the query from Query Monitor:
SELECT wp_posts.ID
FROM wp_posts
LEFT JOIN wp_postmeta
ON wp_posts.ID = wp_postmeta.post_id
WHERE 1=1
AND wp_posts.post_type = 'acf-field'
AND ((wp_posts.post_status = 'publish'))
AND wp_posts.post_name = 'field_5f400266755b3'
AND wp_postmeta.meta_key = 'geschlecht'
AND wp_postmeta.meta_value = 'Er'
ORDER BY wp_posts.menu_order ASC, wp_posts.post_title ASC
LIMIT 0, 1
However, custom fields are no longer loaded now! What can this be related to?
Thank you very much in advance for your help!
Andreas
I have a custom post of Business and each Business has a primary category(parent) and secondary category(child) with taxonomy fields using Advanced Custom Fields. I’m going to be importing into a fresh install that doesn’t have any categories set yet and I’m importing from a spreadsheet. So my question is how do I identify or create the parent/child relationship? My spreadsheet has a column for the primary category(parent) and one for the secondary category(child). But how do I indicate that the secondary category is a child of the primary category(parent)?
Hey,
I am currently developing a system with several custom post types, which all have their own taxnomies. For example: CPT “accomodations” with taxonomies “guest house”, “hotel”, “private room” etc.
The posts are going to have a map field (openstreetmap or google – dependes on where this is going) To display all information and e.g. the openstreetmap on the post page works perfectly fine. I also have an archive page running with taxonomy filtering – works fine too.
I was now thinking about integrating a map of all accomodations with filtering. At the moment there don’t seem to exist any map plugins which can work with custom taxonomies.
It looks I’ll have to built that myself and wanted to ask if anybody could help me out? I searched a lot and did not find exactly what I was looking for.
In an absolute ideal world the code for the map should run in the functions.php and put out a shortcode which I then can use in the backend.
I would be glad for any hints on this topic!
Cheers, Markus
Hi, i have a problem, i create any fields, but each field have a condition by taxonomy.
But i need show this fields in response wp-json…
I make this;
function get_config_clients( $post ){
$group = get_terms(
array(
'taxonomy' => 'cat_notice',
'object_ids' => $post['id'],
)
);
foreach ($group as $key => $value) {
$id = $value->term_id;
if($value->slug === 'arquivo-bdm-express'){
$field = get_field('field_63b424484fa35', $value->term_id);
var_dump($field);
}
}
//return $group;
}
But dont working
How make show in rest_api because not show for my
/wp-json/wp/v2/noticias/88/?acf_format=standard please help me
Based on actual changes with custom post types there exist based on this post: https://support.advancedcustomfields.com/forums/topic/default-value-for-the-taxonomy-field-type/
a new solution:
public function pf_exhibition_index_load( $field ) {
$tax = get_taxonomy( 'pf_exhibition_template' );
$slug = $tax->default_term['slug'];
if ( $term = get_term_by( 'slug', $slug, 'pf_exhibition_template' ) ) {
$field['default_value'] = $term->term_id;
}
return $field;
}
Registering a custom taxonomy allowes to define a default term – with this solution and the option to translate your term a default term in ACF is possible.
$args = array(
'labels' => $labels,
'default_term' => array(
'name' => _x( 'Default', 'Default template taxonomy name', 'picture-frame' ),
'slug' => _x( 'default', 'Default template taxonomy slug', 'picture-frame' ),
'description' => __( 'This is the default template.', 'picture-frame' ),
),
Now the select2 field which is ACF-AJAX powered couldnt use our “default_value”. Therefore i tried a new approach by using a second AJAX function:
The JS-Part:
acf.addAction('show_field/key=field_pf_exhibition_template', function() {
var pf_template = acf.getField('field_pf_exhibition_template');
data = pf_acf_fetch_data('pf_get_exhibition_template_standard', id);
$.when(data).then(function(data){
pf_template.select2.addOption({
id: data.id,
text: data.name,
selected: true
});
});
});
and the corresponding PHP part:
add_action( 'wp_ajax_pf_get_exhibition_template_standard', array( $this, 'pf_ajax_get_exhibition_template_standard' ) );
public function pf_ajax_get_exhibition_template_standard() {
if ( !wp_verify_nonce( $_POST['nonce'], 'acf_nonce' ) ) {
die();
}
$tax = get_taxonomy( 'pf_exhibition_template' );
$slug = $tax->default_term['slug'];
$term = get_term_by( 'slug', $slug, 'pf_exhibition_template');
if ( $term = get_term_by( 'slug', $slug, 'pf_exhibition_template' ) ) {
$data = array( 'id' => $term->term_id, 'name' => $term->name );
}
else {
$data = false;
}
echo json_encode($data);
exit;
}
Now it works like a charm.
Hello everyone, I want to ask a few questions because no option works for me, I have the acf for dokan and I have done all the fields fields I need so that when they upload their products can choose the taxonomies that I already have as brand, model, year etc.. the question is that I see the taxonomies in the dokan page where the product is edited but when I put everything and give it to save product on the dokan page does not save me the selector2 boxes of taxonomies does anyone know if there is a way to save it? greetings. and thanks.