WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Retun Taxonomy Terms (11 posts)

  1. Archie22is
    Member
    Posted 1 year ago #

    Hi,

    I am using a bones theme with the following setup:

    1. Custom taxonomy as below:

    add_action( 'init', 'register_taxonomy_provinces', 0 );
    
    function register_taxonomy_provinces() {
    
        $labels = array(
            'name' => _x( 'Provinces', 'provinces' ),
            'singular_name' => _x( 'Province', 'province' ),
            'search_items' => _x( 'Search Provinces', 'provinces' ),
            'popular_items' => _x( 'Popular Provinces', 'provinces' ),
            'all_items' => _x( 'All Provinces', 'provinces' ),
            'parent_item' => _x( 'Parent Province', 'provinces' ),
            'parent_item_colon' => _x( 'Parent Province:', 'provinces' ),
            'edit_item' => _x( 'Edit Province', 'provinces' ),
            'update_item' => _x( 'Update Province', 'provinces' ),
            'add_new_item' => _x( 'Add New Province', 'provinces' ),
            'new_item_name' => _x( 'New Province', 'provinces' ),
            'separate_items_with_commas' => _x( 'Separate provinces with commas', 'provinces' ),
            'add_or_remove_items' => _x( 'Add or remove Provinces', 'provinces' ),
            'choose_from_most_used' => _x( 'Choose from most used Provinces', 'provinces' ),
            'menu_name' => _x( 'Provinces', 'provinces' ),
        );
    
        $args = array(
            'labels' => $labels,
            'public' => true,
            'show_in_nav_menus' => true,
            'show_ui' => true,
            'show_tagcloud' => true,
            'hierarchical' => false,
            'rewrite' => true,
            'query_var' => true
        );
    
        register_taxonomy( 'provinces', array('add_listing'), $args );
    }

    2. My call back function in the php loop (template)

    <?php 
    
    // Display label
    	echo "Province/s: " . $provinces;
    	// Get the taxonomy province by post id (dynamic)
    	$myterms = get_term( $post->ID, 'provinces' );
    	print_r($myterms);
    	//echo
    ($myterms);								echo "ERROR!";
    ?>

    The only thing that displays in that case is the "ERROR!" output.

    Err, what am I doing wrong?

  2. wpismypuppet
    Member
    Posted 1 year ago #

    Hmmm... I'm not sure if there might be a typo in your code. Are you trying to get a list of all the terms for the 'provinces' taxonomy and list them? Or are you trying to get a list of all the 'provinces' taxonomy terms for the given post?

    In your second bit of code, you are using get_term(). This function takes the term id and the taxonomy the term resides in. You are passing the post id:

    $myterms = get_term( $post->ID, 'provinces' );

    Let me know what you are trying to do and I can offer more help. There are two totally different answers based on how you want to get the terms. For what it's worth, your registering of the taxonomy looks right :)

  3. Archie22is
    Member
    Posted 1 year ago #

    Hi Wpismypuppet!

    Basically I have a custom post type setup (using Pods). There a user can select a province from a list of checkboxes (so they can basically select more than one province).

    On the front-end, I want the list selected province/s taxonomy terms to display on the page, thus the:
    $myterms = get_term( $post->ID, 'provinces' );

    Am I doing anything wrong?

  4. Scott Kingsley Clark
    Member
    Posted 1 year ago #

    Are you using a normal relationship field or using the built-in Taxonomy support for Post Types under the Advanced Options section of the pod editor?

  5. Archie22is
    Member
    Posted 1 year ago #

    Hi Scott,

    Thank you for the reply. I have the following setup:
    - Field Type: Relationship
    - Related to: The custom taxonomy I created (in this case 'Provinces').

    That is the only settings I have touched, and I manually wrote the code for the Taxonomy list. Is there something I am missing?

    I am able to save the terms and they save (the are not lost when I want to edit the content over again).

  6. Scott Kingsley Clark
    Member
    Posted 1 year ago #

    Pods fields are stored in meta, if you go to the Pod editor, below the field list, open up the Advanced Options and go to Post Type Options. From there, go to near the bottom where the taxonomies are listed. Check off the ones you want to support. Then save the Pod.

    From there, you will then be able to save/use the function you're wanting to. Otherwise, just use $myterms = get_post_meta( $post->ID, 'provinces' ) (assuming your field name is provinces).

  7. Archie22is
    Member
    Posted 1 year ago #

    Scott,

    You are the man!! (Well, you have heard that one before). I am able to get the selected taxonomy terms but the code:

    $myterms = get_post_meta( $post->ID, 'province' );
    echo ( $myterms );

    But then the output is just: Province: Array

    A var_dump displays the following output:

    array (size=1)
      0 =>
        array (size=9)
          'term_id' => string '34' (length=2)
          'name' => string 'Mpumalanga' (length=10)
          'slug' => string 'mpumalanga' (length=10)
          'term_group' => string '0' (length=1)
          'term_taxonomy_id' => string '84' (length=2)
          'taxonomy' => string 'provinces' (length=9)
          'description' => string '' (length=0)
          'parent' => string '0' (length=1)
          'count' => string '0' (length=1)

    In that instance the province that matched the selected province instance is selected (Mpumalanga), but how do I display that province name after instead of the currently displaying (Array)?

  8. Archie22is
    Member
    Posted 1 year ago #

    I also tried the following:

    $myprovince = get_post_meta( $post->ID, 'province', true );
    echo ($myprovince->name); //aware of this
    print_r ($myprovince->name); //also aware

    No result. Nothing gets output to the website. What am I doing wrong?

    Can I scream?

  9. wpismypuppet
    Member
    Posted 1 year ago #

    If you're first block of code was giving you an array, simply loop through the array and echo out what you need.

    $myterms = get_post_meta( $post->ID, 'province' );
    foreach( $myterms as $term ) {
        echo ( $term->name );
    }

    You may need to add other HTML code to make it look how you want, of course.

  10. Archie22is
    Member
    Posted 1 year ago #

    Ah, you two geniuses :-)

    The following code worked:

    $myprovince = get_post_meta( $post->ID, 'province' );
    foreach( $myprovince as $term ) {
        echo ( $term [name]);
    }
  11. Archie22is
    Member
    Posted 1 year ago #

    #RESOLVED

Topic Closed

This topic has been closed to new replies.

About this Topic