What happens if you add $query->is_main_query()
to your if statement?
function allstar_buzzworthy_published_sort( $query ) {
// If not the admin section
if ( !is_admin() && $query->is_main_query()) {
// If on a taxonomy page
if ( is_tax() ) {
$query->set( 'meta_key', 'date_published' );
$query->set( 'orderby', 'meta_value' );
$query->set( 'order', 'DESC' );
}
}
}
add_action( 'pre_get_posts', 'allstar_buzzworthy_published_sort' );
Hi @Ron Willemse
I’m not 100% sure, but I think your code should be something like this:
<?php
// vars
$current_term = $wp_query->queried_object;
// use get_field to find the related taxonomy.
$related_term = get_field('field name from taxonomy shops ', $current_term->taxonomy . '_' . $current_term->term_id);
// Depending on the return type setting (ID / Object), you need to then use that data to load the field from.
$related_term_field = get_field('field_name', $related_term->taxonomy . '_' . $related_term->term_id );
echo $related_term_field;
?>
If that deosn’t look write, perhaps you could clearly define the steps needed to get the data you want from the current taxonomy term?
Thanks
E
Hi @shrimp144
The issue is that your if statement will never return true, but is also a bit odd.
if($docs = get_field('docs')):
This should juse be:
if($docs):
Using get_field(‘docs’) wont work, becuase you need to add the $post_id param. You have done this in the 1st half of your code, but then you forget to add it into your if statement.
That said, it doesn’t need to be in your if statement at all.
Thanks
E
Hi @don1967
The ACF plugin contains a field type called ‘taxonomy’. This will allow you to create a select field with all the terms and you don’t need to code a thing!
Thanks
E
Thnx, first of all i like to thank you for the ACF plugin. the second thing is i’m a frontend developer and not really good in php. Can you give me a example?
Hi – thanks for getting back to me.
Found the original issue, it was a ; instead of a :
But anyway, went back to docs and this is what I am now using.
<?php
// vars
$queried_object = get_queried_object();
$taxonomy = $queried_object->taxonomy;
$term_id = $queried_object->term_id;
// load content for this taxonomy term
$docs = get_field('docs', $taxonomy . '_' . $term_id);
var_dump($docs);
?>
<?php if($docs = get_field('docs')): ?>
<?php while($docs = has_sub_field('docs')): ?>
<p><?php the_sub_field('doc_title'); echo $docs; ?><br>
<?php the_sub_field('doc_file'); ?></p>
<?php endwhile; ?>
<?php endif; ?>
This works, as in, when using var_dump I can see the output. But it’s only the string.
Can’t seem to get it to display in the repeater code.
Any ideas?
Thanks,
Matt
//EDIT – The repeater group is called docs. The sub fields are doc_title and doc_file
Hi @shrimp144
I have moved this reply into it’s own topic as it was not a continuation of the previous topic.
I’m not sure exactly what the issue is here, but I’ll take a guess that it is:
the_sub_field('title1, $term->taxonomy.'_'.$term->term_id');
The the_sub_field functions only have 1 parameter, not 2.
Double check the docs again to see if you have missed anything else, and give it another go.
Also, echo $wpr;
wont work because it is an array. Use a var_dump function instead to print an array
Thanks
E
Hi @Ron Willemse
The above cannot be done in 1 single function. You will need to break it into 2 steps.
1. use get_field to find the related taxonomy.
2. Depending on the return type setting (ID / Object), you need to then use that data to load the field from.
Does that help?
Thanks
E
Hi @tomakun
Just for reference, can you please provide a link to the ‘taxonomy doc’ you speak of? Also, which parts of it were not helping you?
What is the return setting for your taxonomy field? ID or object?
Have you looked yet at the WP docs for getting the archive url for a taxonomy term?
Thanks
E
Umm, I think I may have fixed this but not entirely sure how. Will report back later after I’ve cleared my head!
Apologies for the confusion and thanks for helping out on this. I’ve removed the code that’s actually working from the example below and included the dump of what’s getting produced. It is getting the category ID from the ACF custom field.
Custom fields in Use (to get category ID):
site_cat_link = taxonomy field type (Taxonomy Add-on)
cat_id = Text field type (I’ve just manually added the categories I want to pass, just to see if this works).
Code:
<?php if( get_field('site_cats') ): ?>
<?php while( has_sub_field('site_cats') ): ?>
<?php
$variable = get_sub_field('site_cat_link');
var_dump($variable);
$category_link = get_category_link( $variable );
var_dump($category_link);
?>
<p><?php echo the_sub_field('site_cat_link'); ?></p>
<p><?php echo the_sub_field('cat_id'); ?></p>
<a href="<?php echo esc_url( $category_link ); ?>" >Category</a>
<?php endwhile; ?>
<?php endif; ?>
Output when using cat_id (text field):
Category string(3) “249” string(57) “http://www.mysite.com/inspiration-ideas/”
249
249
Category string(3) “346” string(50) “http://www.mysite.com/tips-advice/”
346
346
Output when using site_cat_link (taxonomy field):
Category array(1) { [0]=> string(3) “249” } string(43) “http://www.mysite.com/news/”
249
249
Category array(1) { [0]=> string(3) “346” } string(43) “http://www.mysite.com/news/”
346
346
What I can’t understand is that even though both are outputting the correct category id’s. The taxonomy addon then doesn’t seem to be able to pass that to the link.
Hi @greencode
Can you please clarify your abpove statement:
The three sub fields are working correctly i.e. title, description and link
You then say
‘get the category ID from the site_cat_link field to then produce the category link that I’m having issues.’
I can’t see any code which ‘gets the category ID’
In general, can you please debug your code. This is a neccessary part of web development. All this means is to use a function such as var_dump to dump out the variable or return value from a function. This will allow you to see what data you have.
Please debug each line and return with your findings.
Thanks
E
Apologies Elliot.
Here’s all of the code that I’m using
<?php while ( have_posts() ) : the_post(); ?>
<?php if( get_field('site_cats') ): ?>
<div class="clearfix" id="site-cats">
<?php while( has_sub_field('site_cats') ): ?>
<div class="cat-block g_6 clearfix">
<div class="image">
<?php echo wp_get_attachment_image(get_sub_field('site_cat_image'), 'half-img'); ?>
</div>
<div class="details">
<h2><?php echo the_sub_field('site_cat_title'); ?></h2>
<p><?php echo the_sub_field('site_cat_description'); ?></p>
<p><?php echo the_sub_field('site_cat_link'); ?></p>
<?php
$variable = get_sub_field('site_cat_link');
$category_link = get_category_link( $variable );
?>
<a href="<?php echo esc_url( $category_link ); ?>" title=""><?php echo the_sub_field('site_cat_title'); ?></a>
</div>
</div>
<?php endwhile; ?>
</div>
<?php endif; ?>
<?php endwhile; // end of the loop. ?>
The three sub fields are working correctly i.e. title, description and link are all pulling in their fields but it’s when I then attempt to get the category ID from the site_cat_link field to then produce the category link that I’m having issues.
p.s. I use ACF in a lot of my projects now and would like to donate each time I use it – do you have such an option?
Hi @greencode
Your code is missing the repeater field loop, so I can’t direct you much on this one without seeing whole picture.
Have you debugged your code line by line? Do you know which line is not producing the correct result?
Have you tested each variable?
Thanks
E
Hi @stueynet
I would advise that you checkout the relationship field documentation. It too saves an array of IDs as the value.
In the docs you will find an article which talks about a get_posts query using a LIKE search to find selected posts.
You can use the same technique to load posts with a selected term.
Thanks
E
Solved it. Here is how:
$myrows = $wpdb->get_results( "SELECT * FROM wp_postmeta WHERE meta_key = 'manufacturers' AND meta_value LIKE '%$manufacturer_id%'" );
foreach($myrows as $row){
$retailer_ids[] = $row->post_id;
}
Then run a WP_query with the post ids
$args = array(
'post_type' => 'where',
'post__in' => $retailer_ids
);
$retailers_query = new WP_Query($args);
The only reason I can see this having issues is if one of the serialized pointers is the same as a manufacturer ID. But there are ways to start them high so this workaround should be good for now.
Hi!
1. Yes, it’s ok
2. My loop works ok, i’m working right now in a local server, tomorrow I will upload it to an online server
3. A string with the taxonomy slug: “categoria_proyecto”
4. Both term_taxonomy_id and term_id are the same string
5. Yes: “categoria_proyecto_10”
6. Thanks!
Now, something strange happened, I downloaded the 4.3.0 ZIP from wordpress site and copied the files directly to the plugins folder, and everything worked again fine. I couldn’t reproduce the bug again.
It was very strange. This morning, after I updated using wordpress back-end, the problem happened. I couldn’t reproduce the bug right now, but maybe it can occur only after updating from 4.2.2 to 4.3.0?
Thanks for the report. Before we look into the source code, can you address these issues with your code?
1. Have you validated the data of $terms
2. Does your loop run correctly. Can you show this?
3. What is $taxonomyName
4. I think $term->term_taxonomy_id; should be $term->term_id;
5. Have you tested $field_query
6. echo (the_field is not needed, the_field will already echo the value.
Thanks
E
After some fiddling and testing, there are two things that would make it all super for me, and that’d probably make a good edition to the plugin:
Are these ideas useful and doable? Would they be considered to be part of ACF or should I make my own extensions?
Sure thing. I’m going to take them in reverse order. 🙂
3. Repeater Field and Flexible Content Field doesn’t really do what I need
I want to group large sections of fields inside one Field Group. The users should not be able to create multiple values for each field.
2. with repetitive conditional logic applied to them, and no wrapper.
In lack of a Group Field (or something similar), I can’t wrap all the “Cars” subfields and only set conditional logic on that group, I need to set the logic on each field, which gets quite fiddly, although it’s not the biggest issue.
I can’t find a way of wrapping a set of fields in a HTML wrapper with the regular ACF.
1. I want it to render directly below the category
Category-specific fields needs to appear right below the category field in the form. This is what the form look like (all are ACF fields):
[Title]
[Description]
—————
[Category]
(Wrapped in a div:) Category-specific fields: [field 1], [field 2], …
—————
…the rest of the form…
With one Field Group for each category, I could have render two forms on the front-end: the main post form and the category subfields form, but this is not an option. I made the conclusion that in order to keep all fields in the same form, all fields need to be in the same Field Group. Am I wrong? Is there a clean way of including fields from one Field Group in another Field Group’s form?
UPDATE: I could set 'form' => false
when calling acf_form()
for all per-category Field Groups. That would render them either at the top bottom of the form, but that’s solvable jQuery-wise. Still prefer spitting out ordered HTML, so if there is a better way, shoot!
Conditional logic and category
Another problem is that I can’t set a field to depend on the value of a taxonomy field. Is there an add-on, official or not, to solve this?
Thanks
Hi @panmac
To query a sub field value, especially if it is an array (checkbox) value, you are going to have to understand completely the SQL going on and be able to debug the code line by line to find any issues. If this is looking to be in the ‘too hard’ barsket, I highly encourage you to hire a freelancer to take a look at this part of your project.
However, it is possible to write the code you are seeking. Once you have got the LIKE SQL working, you will need to change the line:
AND meta_value = %s
to:
AND meta_value LIKE %s
and that should allow you to search for values within the array!
Thanks for being a good sport.
Thanks
E
Hi @moabi
I wonder if you are using the taxonomy ‘category’ or another type of taxonomy. Perhaps this is why the code does not work?
Hi Elliot – thanks for your answer – Yes, I had read over that tutorial, but since the code example was basically exactly what I was looking for, I was hoping it would work easily since I’m not advanced enough in PHP to know how to implement the tutorial and take into account my CPT – “by joining the wp_postmeta table to the wp_posts table on the post_id columns.” (and yes, I googled how to join tables in sql, etc, but everything just left me with a blank page so clearly I’m not doing it correctly).
The other factor is that I realized I need the sub_field to be checkboxes and not a single select, so that adds another layer of complexity that I don’t think I understand.
Not sure at all what I’m missing. Sorry for being such a noob.
Sorry about that – I believe we have the correct use of the variable now, but it doesn’t seem to be working correctly. Here’s everything I know:
'name' => 'select_waiting_list',
with 'choices' => array(),
in functions.php.function get_waiting_lists( $field ) {
$field['choices'] = array();
// Query Waiting Lists Products
$args = array(
'post_type' => 'memberpressproduct',
'orderby' => 'name',
'order' => 'ASC',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'topics',
'field' => 'id',
'terms' => '22',
'operator' => 'IN'
),
)
);
$lists_array = get_posts( $args );
foreach ( $lists_array as $list ) {
$choices[$list->ID] = $list->post_title;
}
$field['choices'] = $choices;
wp_reset_postdata();
return $field;
}
add_filter('acf/load_field/name=select_waiting_list', 'get_waiting_lists');
array(1) { ["choices"]=> array(2) { [449]=> string(26) "Waiting List - Millionaire" [623]=> string(29) "Waiting List - Small Business" } }
<select id="acf-field-connect_waiting_list" class="select" name="fields[field_5272b350a4105]"><option value="null">- Select -</option><option value="choices"></option></select>
Thank you again, so much, for your help sorting this out. You guys have such great support.
– Ross
Hi @panmac
Have you read over this article:
http://www.advancedcustomfields.com/resources/tutorials/querying-the-database-for-repeater-sub-field-values/
To query a sub field value, you need to add in a few more steps than the normal meta_query args.
Thanks
E
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.