In tags i have added custom meta as popular. Where admin select tag is popular or not according to that it will show on website.
Here is my code for adding & saving tags meta field:-

// Add term page
function pippin_taxonomy_add_new_meta_field() {
    // this will add the custom meta field to the add new term page
    ?>
    <div class="form-field">
        <label for="term_meta[custom_term_meta]"><?php _e( 'Popular', 'pippin' ); ?></label>
        <input type="checkbox" name="term_meta[popular]" id="term_meta[popular]" value="popular">
        <select name="term_meta[popular]" id="term_meta[popular]">
            <option value="">Select option</option>
            <option value="yes">Yes</option>
            <option value="no">No</option>
        </select>
        <p class="description"><?php _e( 'Please checked this if this is popular tag','pippin' ); ?></p>
    </div>
<?php
}
add_action( 'post_tag_add_form_fields', 'pippin_taxonomy_add_new_meta_field', 10, 2 );

// Edit term page
function pippin_taxonomy_edit_meta_field($term) {

    // put the term ID into a variable
    $t_id = $term->term_id;

    // retrieve the existing value(s) for this meta field. This returns an array
    $term_meta = get_option( "taxonomy_$t_id" ); ?>
    <tr class="form-field">
    <th scope="row" valign="top"><label for="term_meta[custom_term_meta]"><?php _e( 'Popular', 'pippin' ); ?></label></th>
        <td>
            <select name="term_meta[popular]" id="term_meta[popular]">
                <option value="">Select option</option>
                <option value="yes" <?php if($term_meta['popular']=='yes') { ?> selected <?php } ?>>Yes</option>
                <option value="no" <?php if($term_meta['popular']=='no') { ?> selected <?php } ?>>No</option>
            </select>
            <p class="description"><?php _e( 'Please checked this if this is popular tag','pippin' ); ?></p>
        </td>
    </tr>
<?php
}
add_action( 'post_tag_edit_form_fields', 'pippin_taxonomy_edit_meta_field', 10, 2 );

function save_taxonomy_custom_meta( $term_id ) {
    if ( isset( $_POST['term_meta'] ) ) {
        $t_id = $term_id;
        $term_meta = get_option( "taxonomy_$t_id" );
        $cat_keys = array_keys( $_POST['term_meta'] );
        foreach ( $cat_keys as $key ) {
            if ( isset ( $_POST['term_meta'][$key] ) ) {
                //echo $key;
                $term_meta[$key] = $_POST['term_meta'][$key];
            }
        }
        //exit();
        // Save the option array.
        update_option( "taxonomy_$t_id", $term_meta );
    }
}  

So When i fetching data of popular tags i am not getting that data. Here is code for fetching popular tags data :-

<?php 
                            $args = array(
                                'taxonomy'   => 'post_tag',
                                'meta_query' => array(
                                     array(
                                        'key'       => 'popular',
                                        'value'     => 'yes',
                                        'compare'   => '='
                                     )
                                )
                            );

                            $popular_tags = get_terms($args);
                            //print "<pre>";
                            //print_r($popular_tags);
                            foreach($popular_tags as $ptag)
                            {
                                echo '<a href="'.get_tag_link($ptag->term_id).'" rel="tag" class="badge badge-pill badge-default">'.$ptag->name.'</a>';
                            }
                        ?>

Can you help me why i am not getting data from this code?

Read more here: Fetching tags from custom meta field but not getting any data


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: