1

I was able to add a extra custom field, named sticky, to a custom taxonomy, using the wp taxonomy meta plugin, like this:

function YOUR_PREFIX_register_taxonomy_meta_boxes()
{
    // Make sure there's no errors when the plugin is deactivated or during upgrade
    if ( !class_exists( 'RW_Taxonomy_Meta' ) )
        return;

    $meta_sections = array();

    // First meta section
    $meta_sections[] = array(
        'title'      => 'Sticky',             // section title
        'taxonomies' => array('tvr_amenity'), // list of taxonomies. Default is array('category', 'post_tag'). Optional
        'id'         => 'sticky',                 // ID of each section, will be the option name

        'fields' => array(                             // List of meta fields
            array(
                'name' => 'Show in home filters',
                'id'   => 'sticky',
                'type' => 'checkbox',
            ),
        ),
    );
    foreach ( $meta_sections as $meta_section )
    {
        new RW_Taxonomy_Meta( $meta_section );
    }
}

enter image description here

Now I'm trying to get all the taxonomies that has this value checked, like this:

$types = $types = get_terms( 'tvr_amenity', array(
    'parent'    => '0',
    'hide_empty' => 1,
    'sticky' => 1
 ) );

But the filter is ignored (all the parent taxonomies are shown), it returns the exact same than:

$types = $types = get_terms( 'tvr_amenity', array(
        'parent'    => '0',
        'hide_empty' => 1
     ) );

Any idea what I'm missing, here?

1

OK, got it

<?php
    $types = get_terms( 
        'tvr_amenity', 
        array(
            'parent'    => '0',
            'hide_empty' => 1

         )
    );
    foreach( $types as $type ) :
            $myname = trim($type->name);
            $meta = get_option('amenity_sticky');
            if (empty($meta)) $meta = array();
            if (!is_array($meta)) $meta = (array) $meta;
            $meta = isset($meta[$type->term_id]) ? $meta[$type->term_id] : array();
            $value = $meta['is_sticky'];
            if(!$value) continue; /* skip term if is_sticky not checked */
?>
    <p>
    <input type='checkbox' name="apartment_amenity[]" value='<?php echo $type->term_id ?>' class='tvr_amenity'> <?php echo $myname ?>
    </p>
<?php endforeach;   ?>
0

first change this:

// First meta section
$meta_sections[] = array(
    'title'      => 'Sticky',             // section title
    'taxonomies' => array('tvr_amenity'), // list of taxonomies. Default is array('category', 'post_tag'). Optional
    'id'         => 'amenity_sticky',                 // ID of each section, will be the option name

    'fields' => array(                             // List of meta fields
        array(
            'name' => 'Show in home filters',
            'id'   => 'is_sticky',
            'type' => 'checkbox',
        ),
    ),
);
foreach ( $meta_sections as $meta_section )
{
    new RW_Taxonomy_Meta( $meta_section );
}

To get the info in the front end I think this could be the solution:

            $terms = get_terms( 'tvr_amenity', array(
                'hide_empty' => 0,
            ) );
            foreach( (array) $terms as $term) {
                $meta = get_option('tvr_amenity');
                if (empty($meta)) $meta = array();
                if (!is_array($meta)) $meta = (array) $meta;
                $meta = isset($meta[$term->term_id]) ? $meta[$term->term_id] : array();
                $term_link = get_term_link( $term, 'tvr_amenity' );
                $is_sticky = $meta['is_aticky'];
                $is_sticky_id = false;
                if (is_array($is_sticky)) {
                    foreach ($is_sticky as $att) {
                        $is_sticky_id = $att;
                    }
                } ?>
                <a class="term-link" href="<?php echo $term_link; ?>" class="<?php echo $is_sticky; ?>">
                <?php echo sprintf(__('%s', 'udla'), $term->name); ?>
                </a>
                <p><?php echo $term->description; ?></p>
            <?php
            }
  • Hi there, thank you very much for your answer, but this sill also return all the items (even if is_sticky is not checked), any idea? – Toni Michel Caubet Nov 1 '13 at 13:45
  • Actually I made print_r($term); and print_r($meta) and there's no reference about is_sticky – Toni Michel Caubet Nov 1 '13 at 13:54
  • add an 'include' => array(), in the get_terms with the IDs you want to filter, or include you new meta field in the foreach :P sorry can´t help more – lonchbox Nov 1 '13 at 16:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.