get_terms that have custom sticky field checked

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

Read More

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?

Related posts

2 comments

  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;   ?>
    
  2. 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
                }
    

Comments are closed.