Hi @Glyn
You will need to write the JS yourself unfortunately.
Good luck
Cheers
E
Thanks Elliot,
I don’t think you misread the question – but I may not have explained it clearly enough 🙂
I can use the filter as you described to update the query on first page load, but I’d like to do this dynamically based upon changing the value of a dropdown field on the page (a taxonomy field). In effect creating a kind of conditional logic as per the standard options.
Is there a way to force the field to be redrawn (and hence go through the filter again) using javascript?
Thanks
Hi @studio34
I don’t believe this underscore is an issue at all. There is no code in ACF which is exploding the $post_id string by the ‘_’ cahracter. Instead it looks in the wp_options table for a value with a options_name of:
'product_cat_' . $cat_id . '_' . $field_name
Perhaps you are having another issue but have jumped to this conclusion?
Let me know if I can be of any other help.
Perhaps you could test on another taxonomy just to rule this out?
Hi @Glyn
I may have miss-read your question, but the filter ‘http://advancedcustomfields/resources/filters/acf-fields-post_object-query/’ does contain a $post parameter which you can use to load the taxonomy value from with get_field
Good luck
Thanks
E
Hi @Glyn
I think the filter ‘acf/fields/post_object/query’ will work just fine, however the taxonomy will not be passed in as a parameter. This means you need to find another way to find the taxonomy.
I would use a function such as get_current_screen
(http://codex.wordpress.org/Function_Reference/get_current_screen)
This function will return lots of information about the current edit page.
Does this help?
Thanks
E
Hi Kyle, I recreated the situation over here and have some verified code. Here’s a complete, but simplified page template (‘single-project.php’):
<?php get_header(); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<h1><?php the_title(); ?></h1>
<?php the_content(); ?>
<?php
// Grab all of the 'category' terms that have been applied to this 'project' and
// go through and output the category title and any posts that fall under the category.
$categories = get_the_terms( $post->ID, 'category' );
if ( $categories && ! is_wp_error( $categories ) ) : ?>
<?php foreach ( $categories as $category ) : ?>
<h3><?php echo $category->name; ?>:</h3>
<?php // Generate a custom query using the category's slug
$relatedPosts = new WP_Query( array('category_name' => $category->slug, 'posts_per_page' => -1) );
if ( $relatedPosts->have_posts() ) : ?>
<ul>
<?php while ( $relatedPosts->have_posts() ) : $relatedPosts->the_post(); ?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php endwhile; ?>
</ul>
<?php endif;
// Restore the global $post variable to the current post in the main query
wp_reset_postdata(); ?>
<?php endforeach; ?>
<?php endif; ?>
<?php endwhile; endif; ?>
<?php get_footer(); ?>
As I was setting this up, I realized that it’s possible that the earlier snippet may not have worked for you because of the way the options may be set in the custom field definition. When adding the ‘taxonomy’ custom field, make sure that the field is set to return the ‘Term Object’ instead of the ‘Term ID’ so that it will work with the above code block.
OK, let me know how this works.
Thanks,
Jeff
Well, if you have ideas, please do feel free to throw them out and I’ll provide feedback. I was up late last night trying a few more things, but I wasn’t successful.
Thanks,
~Kyle~
p.s. if I could award you “reputation” points, I would. I appreciate your help.
Hi Kyle,
Sounds good. Let me know if you need me to take another shot at it.
Thanks,
Jeff
I believe the extra underscore interferes with the parsing of the taxonomy_id string. This is the first time I have had to deal with a taxonomy that had an underscore in it and it is the first time that get_field has not worked for me.
I’m trying to rule out issues. I’ve already checked spelling. Not sure what else to try.
Hmmm, nope @jeffvls, that didn’t seem to work. But I’ll take what you wrote and try to build off of it.
Thanks!
Hi Kyle,
Thanks for the additional explanation. Drop this into the code (within the loop) and give this a shot real quick to see if this outputs the category name and slug correctly. If so, you can then take the ‘category slug’ to generate a custom WP Query to grab the 5 most recent posts by category. If this doesn’t work it’s because I grabbed it from my code and attempted to modify it for your purposes without actually testing… : )
Just let me know if it doesn’t work and I’ll see what I can do. Here’s the code snippet:
<?php
$categories = get_the_terms( $post->ID, 'category' );
if ( $categories && ! is_wp_error( $categories ) ) : ?>
<ul>
<?php foreach ( $categories as $category ) : ?>
<li>Category Name: <?php echo $category->name; ?> / Category Slug: <?php echo $category->slug; ?></li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
Thanks for the hint on the var dumps, @jeffvls. As for the scope, let me see how I can explain this a bit clearer. So, I’m a doctoral student and I want folks to know what projects I’m working on. To do so, I’ve created a ‘projects’ custom post type. I will be writing posts that are categorized with a category that is similar if not the same title to the project I’m working on. So, I’ve used the taxonomy field to pick the category to attach to the individual project.
There are two things I want from this category relationship:
1) I want to echo the title of the category and then link to the category’s archive of posts;
2) I want to list maybe the last 5 posts from that category
@elliot, does that give you a bit more information as well?
I’m appreciative of any help you can provide.
I see, I’ll the simply need to create the relation from the Post manually, not such a big deal would just be much more elegant to just do it once. Thanks for looking at it, keep up the great work!
Hi @aplusplus
Thanks for the info. Now I understand the issue.
Unfortunately, because of te way that term data is saved (in the wp_options table because term_meta does not yet exist in WP), this kind of query will not be possible at all…
Perhaps you can remove the relationship field and use another taxonomy to link together products?
Sorry I can’t be of much help on this one.
Thanks
E
Hi @studio34
Why is the ‘extra underscore’ a problem, I don’t understand.
Sorry if I wasn’t clear, I know about the default WP behaviour and I need something different. This is how it is set up:
This Relationship field:
array (
'key' => 'field_5231bd6773a84',
'label' => 'Product',
'name' => 'category_product',
'type' => 'relationship',
'return_format' => 'object',
'post_type' => array (
0 => 'product',
),
'taxonomy' => array (
0 => 'product_cat:3',
),
'filters' => array (
0 => 'search',
),
'result_elements' => array (
0 => 'post_type',
1 => 'post_title',
),
'max' => 1,
)
Is attached to the default Categories:
'location' => array (
array (
array (
'param' => 'ef_taxonomy',
'operator' => '==',
'value' => 'category',
'order_no' => 0,
'group_no' => 0,
),
),
array (
array (
'param' => 'ef_taxonomy',
'operator' => '==',
'value' => 'post_tag',
'order_no' => 0,
'group_no' => 1,
),
),
)
My question is how to query the category on the product post type template.
Hope I was clear and thanks so much for your time!
Hi @aplusplus
I am again a bit confused.
Can you please explain clearly what fields have been added to which post_type / taxonomy. I need to understand where the data is being saved.
Also, do you understand what a taxonomy term is normally for? Did you know that you don’t need to use a relationship field to connect a post object to a taxonomy term as this is standard WP behaviour?
<?php if( get_field('developer') ): ?>
<?php $args = array('taxonomy' => 'game_developer'); ?>
<?php $tax_menu_items = get_categories( $args );
foreach ( $tax_menu_items as $tax_menu_item ):?>
<p>Developer:<a href="<?php echo get_term_link($tax_menu_item,$tax_menu_item->taxonomy); ?>"> <?php echo $tax_menu_item->name; ?></a></p>
<?php endforeach; ?><?php endif; ?>
Got it working with above code 😀 Now few tweaks and full steam ahead.
To further expand:
the ‘Taxonomy field’ is a select type field.
So for exmaple a Toxonomy is made for a publisher. Post creation has a nice dropdown menu based on those Toxonomys.
Any data that is stored in the Image/Date Picker/Text Area fields all display correctly.
Only the Toxonomy Fields return Id’s and not the actual data.
There are no documents explaining the purpose of the ‘Toxonomy Field’ or correct usage. But as far as seeing where the data IS it works fine. It just does not show the ACTUAL data itself 🙁
Hi Elliot,
the relationship relates to a custom post type “Product” with a Category.
Imagine the Category is a Magazine Issue which has Posts (default WP functionality) and then this Magazine Issue Category is connected to a Product to buy the printed Issue. I now want to link to the Category which is connected to the Product from the Product Page. Does this make sense?
The Code would run on the Template displaying the Products (CPT) where I’d try to query: “Get the Category ID of the Category related to this Product ID”.
Does this make it clearer?
No was a total error. Been grinding for 3 days with zero luck getting data. Put messy code together just to show it to find out why only ID’s where called. Just updated with you recommended document code again.
<?php $post_object = get_field('get_game_details');
if( $post_object ):
// override $post
$post = $post_object;
setup_postdata( $post ); ?>
<?php get_template_part( 'game_details', 'index' ); ?>
<?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
<?php endif; ?>
However I still cannot figure out why only ID's are called and not the display of the data.
For Example – 47 = Pokemon X & Y
To further explain. When viewed from normal post type all data is fully visible. However when viewed from my custom post type 'Games Information' (that also uses custom Toxonomys), all the data is simple ID's.
add_action( 'init', 'register_cpt_game_information' );
function register_cpt_game_information() {
$labels = array(
'name' => _x( 'Games Information', 'game_information' ),
'singular_name' => _x( 'Game Information', 'game_information' ),
'add_new' => _x( 'Add New', 'game_information' ),
'add_new_item' => _x( 'Add New Game Information', 'game_information' ),
'edit_item' => _x( 'Edit Game Information', 'game_information' ),
'new_item' => _x( 'New Game Information', 'game_information' ),
'view_item' => _x( 'View Game Information', 'game_information' ),
'search_items' => _x( 'Search Games Information', 'game_information' ),
'not_found' => _x( 'No games information found', 'game_information' ),
'not_found_in_trash' => _x( 'No games information found in Trash', 'game_information' ),
'parent_item_colon' => _x( 'Parent Game Information:', 'game_information' ),
'menu_name' => _x( 'Games Information', 'game_information' ),
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
'supports' => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'trackbacks', 'custom-fields', 'comments', 'revisions', 'page-attributes' ),
'taxonomies' => array( 'category', 'post_tag' ),
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 5,
'show_in_nav_menus' => true,
'publicly_queryable' => true,
'exclude_from_search' => false,
'has_archive' => true,
'query_var' => true,
'can_export' => true,
'rewrite' => true,
'capability_type' => 'post'
);
register_post_type( 'game_information', $args );
}
add_action( 'init', 'register_taxonomy_game_title' );
function register_taxonomy_game_title() {
$labels = array(
'name' => _x( 'Game Title', 'game_title' ),
'singular_name' => _x( 'Games Title', 'game_title' ),
'search_items' => _x( 'Search Game Title', 'game_title' ),
'popular_items' => _x( 'Popular Game Title', 'game_title' ),
'all_items' => _x( 'All Game Title', 'game_title' ),
'parent_item' => _x( 'Parent Games Title', 'game_title' ),
'parent_item_colon' => _x( 'Parent Games Title:', 'game_title' ),
'edit_item' => _x( 'Edit Games Title', 'game_title' ),
'update_item' => _x( 'Update Games Title', 'game_title' ),
'add_new_item' => _x( 'Add New Games Title', 'game_title' ),
'new_item_name' => _x( 'New Games Title', 'game_title' ),
'separate_items_with_commas' => _x( 'Separate game title with commas', 'game_title' ),
'add_or_remove_items' => _x( 'Add or remove game title', 'game_title' ),
'choose_from_most_used' => _x( 'Choose from the most used game title', 'game_title' ),
'menu_name' => _x( 'Game Title', 'game_title' ),
);
$args = array(
'labels' => $labels,
'public' => true,
'show_in_nav_menus' => true,
'show_ui' => true,
'show_tagcloud' => true,
'show_admin_column' => false,
'hierarchical' => true,
'rewrite' => true,
'query_var' => true
);
register_taxonomy( 'game_title', array('game_information'), $args );
}
Hello,
Yes, the custom taxonomy is visible and usable on each custom post edit page. It can be queried just fine everywhere else on the site.
Could this be a problem with custom capabilities assigned to my custom post type (nothing spectacular, just renames “edit_posts” to “edit_cpts” and so on)?
EDIT:
Read through the core plugin code and noticed this line of code in the function that populates the select choices (get_taxonomies_for_select
in acf.php
) for the taxonomy filter:
if(!is_taxonomy_hierarchical($taxonomy)) continue;
I have my taxonomy as non-hierarchical and this seems to be the problem. I can’t seem to remember any notice of this on the field’s documentation page?
Luckily my taxonomy will work about the same even if it is not non-hierachical, but will there be an update to allow filtering with these taxonomies too?
Hi @ojrask
The fact that you post_object field is within a repeater should make no difference. Also the location rule should not matter either. You can test this by creating a new post_object field as a root level field in the same field group.
When you edit one of your post_type posts, do you see the correct WP metabox for your taxonomy? Does the Taxonomy appear in the sidebar under your post_type?
Is your taxonomy public?
When you say “I need to pull a number of the values and create variables out of them in order to build some specific URLs and also to develop a query that pulls the latest posts from this particular category.”, I don’t understand what you are asking.
Can you be very clear and specific?
Thanks
E
Hi @DavidS
Currently, this is not possible. But will be added in the near future.
Perhaps you could look at manually including the select2 JS library and using a taxonomy field + select2 to make it sortable
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.