Show term meta on custom post type
Support › MB Term Meta › Show term meta on custom post typeResolved
- This topic has 7 replies, 2 voices, and was last updated 4 years, 3 months ago by
Long Nguyen.
-
AuthorPosts
-
January 15, 2021 at 9:58 PM #24045
Akram Ul haq
ParticipantHi,
I hope you are fine and doing great and thanks for the awesome product you developed 🙂
Well, I got a strange query from my one of client and I am stuck first time since using metabox io and could not find how to get out of this.
Here is full scene:
My custom post name is product: and my custom taxonomy name is item: well my taxonomy has terms say item1 ,item2 and item3
Now each item term can have term meta data like say
item1 has meta data as array( 'red', 'blue', 'green')
and item2 has meta data as array( 'red', 'orange', 'purple' )
and item3 has meta term data as array( 'orange', 'white', 'pink' )
I hope you got so far what I meant ..
Now I just want to show those colors array as multi select checkbox in my custom post product.
Say all three terms are being shown to choose from on product post. Once I choose item1 then its colors array( 'red', 'blue', 'green') should show checkbox list items to choose from as a metabox field.
In same way if I choose item2 then its array( 'red', 'orange', 'purple' ) should show a checkbox list field
In short the term I choose its term meta should be shown as checkbox list.
Can you please give me some code to make it work I could not make it work so far after wasting huge time on it 🙁
Waiting for the response
Thank You
January 16, 2021 at 12:39 PM #24052Long Nguyen
ModeratorHi,
I think this question is beyond the scope of supporting Meta Box and extensions. It would take a lot of code to do with PHP and JavaScript (AJAX).
So I recommend using the child term to show the parent and child term on the CPT by using the field taxonomy or taxonomy_advanced to create select options for users to select item (parent) and colors red, orange, purple (child term) with the field type
select_tree
orcheckbox_tree
.January 16, 2021 at 2:09 PM #24053Akram Ul haq
ParticipantHi,
But my query is term meta not child term ... Kindly have a look again on my question. I am asking to show meta data of the selected term as checkbox list. Help me in that way if you can please ?
Thanks
January 18, 2021 at 1:26 PM #24080Akram Ul haq
ParticipantHi,
Any update for me please ?
Thank You
January 19, 2021 at 12:54 PM #24092Long Nguyen
ModeratorHi,
I think you can use the filter rwmb_{$field_id}_end_html to use the field value and add some HTML code after the field appearance. For simple example, I have the custom field
post_meta_taxonomy
type taxonomy for the post and a custom fieldcategory_meta_text
for the taxonomy Category. The code below would help you to show the term meta base on the term ID selected.add_filter( 'rwmb_post_meta_taxonomy_end_html', function( $end, $field, $meta ) { $term_meta = rwmb_meta( 'category_meta_text', array( 'object_type' => 'term' ), $meta ); if( !empty( $term_meta ) ) { $end .= '<h4 style="color: red">Category Meta Text: ' . $term_meta. '</h4>'; } return $end; }, 10, 3 );
See a short screen record https://share.getcloudapp.com/YEuQxzny.
January 19, 2021 at 12:59 PM #24093Long Nguyen
ModeratorYou can also use the AJAX in WordPress to get the term meta right after select the term. Example in this artilce.
January 19, 2021 at 5:33 PM #24095Akram Ul haq
ParticipantHi,
Thanks for the details as I am sure now you got me right 🙂 first of all again huge respect to your team as you guys even made me developer to a person who even has no knowledge in programming 🙂 huge respect ad I bow down.
Well, you are showing this meta exactly fine I can see it, but I want it not to show up on page load. I just want to show up as soon as user selects that taxonomy, then below that this term meta data shown as selectbox list field so user can choose his/her desired item and then can save/update post.
I am sure we are close enough now but a little tweak is needed can you please bring me in there as well ... huge respect and thanks in advance for your efforts to keep this product growing.
Thanks again
January 19, 2021 at 7:44 PM #24096Long Nguyen
ModeratorHi,
At first, please follow this documentation to know how to use AJAX in WordPress https://codex.wordpress.org/AJAX_in_Plugins, then you can use this sample code to show the term meta on selecting the field taxonomy.
add_action( 'admin_footer', function() { ?> <script> jQuery(document).ready(function($) { $('.rwmb-field').on('change', function() { var that = $(this); // Get the term ID on select var term_id = that.find('select[name=post_meta_taxonomy').val(); // Create a div to show the response that.append('<div id="meta-taxonomy-response"></div>'); var data = { 'action': 'my_action', 'term_id': term_id, }; jQuery.post(ajaxurl, data, function(response) { $('#meta-taxonomy-response').html(response); }); }) }); </script> <?php } ); add_action( 'wp_ajax_my_action', 'my_action' ); function my_action() { $term_id = $_POST['term_id']; if( isset( $term_id ) ) { $term_meta = rwmb_meta( 'category_meta_text', array( 'object_type' => 'term' ), $term_id ); echo '<h4 style="color: red">Category Meta Text: ' . $term_meta. '</h4>'; } else { echo "Not found"; } wp_die(); // this is required to terminate immediately and return a proper response }
Screen record https://share.getcloudapp.com/5zudxE49.
-
AuthorPosts
- You must be logged in to reply to this topic.