• Resolved Sany

    (@sannny)


    Hello,

    I need your help.

    I’ve added a custom field (selection) to my taxonomy page in order to choose a value which simualates a term status.

    • taxonomy name = productcategory
    • field name = term_status -> you can choose publish or archiv

    Then I’ve added a new meta box for list all terms of my taxonomy which looks like a standard taxonomy meta box. I used wp_terms_checklist and wp_popular_terms_checklist to list the terms.
    But I only want to list terms with term_status = publish there.
    Can I combine wp_terms_checklist/wp_popular_terms_checklist with a meta query or something like that?
    Does someone has an idea how to implement this?

    Best regards
    saNNNy

    ————————

    My code for adding the custom meta box:

    <?php
    // add the new box
    function add_custom_categories_box() {
    	add_meta_box('productcategorydiv', 'productcategory TEST', 'custom_post_categories_meta_box', 'post', 'side', 'default', array( 'taxonomy' => 'productcategory' ));
    }
    add_action('admin_menu', 'add_custom_categories_box');
    
    /**
     * Display CUSTOM post categories form fields.
     *
     * @since 2.6.0
     *
     * @param object $post
     */
    function custom_post_categories_meta_box( $post, $box ) {
    
    	$defaults = array('taxonomy' => 'productcategory');
    	if(!isset($box['args']) || !is_array($box['args'])) {
    		$args = array();
    	} else {
    		$args = $box['args'];
    	}
    	extract(wp_parse_args($args, $defaults), EXTR_SKIP);
    	$tax = get_taxonomy($taxonomy);
    
    	?>
    	<div id="taxonomy-<?php echo $taxonomy; ?>" class="categorydiv">
    
    		<!-- Tabs -->
    		<ul id="<?php echo $taxonomy; ?>-tabs" class="category-tabs">
    			<li class="tabs">
    				<a href="#<?php echo $taxonomy; ?>-all" tabindex="3"><?php echo $tax->labels->all_items; ?></a>
    			</li>
    			<li class="hide-if-no-js">
    				<a href="#<?php echo $taxonomy; ?>-pop" tabindex="3"><?php _e('Most Used'); ?></a>
    			</li>
    		</ul>
    
    		<div id="<?php echo $taxonomy; ?>-pop" class="tabs-panel" style="display: none;">
    			<ul id="<?php echo $taxonomy; ?>checklist-pop" class="categorychecklist form-no-clear" >
    				<?php $popular_ids = wp_popular_terms_checklist($taxonomy); ?>
    			</ul>
    		</div>
    		
    		
    		<!-- Checklist -->
    		<div id="<?php echo $taxonomy; ?>-all" class="tabs-panel">
    			<?php
    			$name = ( $taxonomy == 'productcategory' ) ? 'post_category' : 'tax_input[' . $taxonomy . ']';
    			echo "<input type='hidden' name='{$name}[]' value='0' />"; // Allows for an empty term set to be sent. 0 is an invalid Term ID and will be ignored by empty() checks.
    			?>
    			<ul id="<?php echo $taxonomy; ?>checklist" class="list:<?php echo $taxonomy?> categorychecklist form-no-clear">
    				<?php 
    				/**
    				 * This is the one line we had to change in the original function
    				 */
    				wp_terms_checklist($post->ID, array('taxonomy' => $taxonomy, 'popular_cats' => $popular_ids, 'checked_ontop' => TRUE)); 
    				?>
    				
    				
    			</ul>
    		</div>
    	<?php if ( !current_user_can($tax->cap->assign_terms) ) : ?>
    	<p><em><?php _e('You cannot modify this taxonomy.'); ?></em></p>
    	<?php endif; ?>
    	<?php if ( current_user_can($tax->cap->edit_terms) ) : ?>
    			<div id="<?php echo $taxonomy; ?>-adder" class="wp-hidden-children">
    				<h4>
    					<a id="<?php echo $taxonomy; ?>-add-toggle" href="#<?php echo $taxonomy; ?>-add" class="hide-if-no-js" tabindex="3">
    						<?php
    							/* translators: %s: add new taxonomy label */
    							printf( __( '+ %s' ), $tax->labels->add_new_item );
    						?>
    					</a>
    				</h4>
    				<p id="<?php echo $taxonomy; ?>-add" class="category-add wp-hidden-child">
    					<label class="screen-reader-text" for="new<?php echo $taxonomy; ?>"><?php echo $tax->labels->add_new_item; ?></label>
    					<input type="text" name="new<?php echo $taxonomy; ?>" id="new<?php echo $taxonomy; ?>" class="form-required form-input-tip" value="<?php echo esc_attr( $tax->labels->new_item_name ); ?>" tabindex="3" aria-required="true"/>
    					<label class="screen-reader-text" for="new<?php echo $taxonomy; ?>_parent">
    						<?php echo $tax->labels->parent_item_colon; ?>
    					</label>
    					<?php wp_dropdown_categories( array( 'taxonomy' => $taxonomy, 'hide_empty' => 0, 'name' => 'new'.$taxonomy.'_parent', 'orderby' => 'name', 'hierarchical' => 1, 'show_option_none' => '&mdash; ' . $tax->labels->parent_item . ' &mdash;', 'tab_index' => 3 ) ); ?>
    					<input type="button" id="<?php echo $taxonomy; ?>-add-submit" class="add:<?php echo $taxonomy ?>checklist:<?php echo $taxonomy ?>-add button category-add-sumbit" value="<?php echo esc_attr( $tax->labels->add_new_item ); ?>" tabindex="3" />
    					<?php wp_nonce_field( 'add-'.$taxonomy, '_ajax_nonce-add-'.$taxonomy, false ); ?>
    					<span id="<?php echo $taxonomy; ?>-ajax-response"></span>
    				</p>
    			</div>
    		<?php endif; ?>
    	</div>
    	<?php
    }
    ?>
    • This topic was modified 8 years, 1 month ago by Sany.
    • This topic was modified 8 years, 1 month ago by Sany.
Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator bcworkz

    (@bcworkz)

    The functions you are using use get_terms() to get the terms to list. You can alter the terms returned through the “get_terms” filter. You can even rerun the terms query, this time specifying meta key and value as part of the query. This is rather inefficient because you are discarding the initial results and making a new query.

    You can go deeper into the process and hook the “pre_get_terms” action to set your desired query vars before the query is run the first time. If you are familiar with the pre_get_posts action when altering a posts query, it’s the same concept applied to term queries. To avoid inappropriately altering other term queries, add your action hook just before you make the call to either of the term listing functions. Have your callback remove itself from the action stack just before returning.

    Thread Starter Sany

    (@sannny)

    Thanks for your answer.
    Unfortunately, I can’t handle it ??
    I think there should be something like the following query. But I dont know how to tell wp_terms_checklist etc. how to take these results or to add pre_get_terms action.

    get_terms( 
      array('
        'taxonomy' => 'productcategory',
        'meta_key' => 'term_status',
        'meta_value' => 1
       )
    );
    • This reply was modified 8 years, 1 month ago by Sany.
    Moderator bcworkz

    (@bcworkz)

    An experienced WP developer could implement what I’ve described fairly quickly, in other words it shouldn’t be too expensive to hire some help. Such help can be found at jobs.wordpress.net.

    Thread Starter Sany

    (@sannny)

    thanks.
    Can someone else help?

    Thread Starter Sany

    (@sannny)

    Hi,
    I copied the source of wp_terms_checklist and added meta_query in get_terms. Not a beautiful solution but it works.
    Thanks again.

    Issue closed.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Exclude Results (Term Meta) from Term Checklists’ is closed to new replies.