$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>custom taxonomy - Dynamically tax_query terms|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

custom taxonomy - Dynamically tax_query terms

matteradmin9PV0评论

To display all the categories name with checkbox from custom taxonomy called ct_category I have the following code:

$terms = get_terms('ct_category');
foreach ( $terms as $term ) {
echo '<li class='.$term->term_id.'><label > <input class="taxonomy_category" type="checkbox" name="taxonomy_category['.$term->term_id.']" value ="'.$term->term_id.'" />'.$term->name.'</label ></li>';          
}                                             

I would like to display content only from checked categories. I tried following that didn't work:

$args = array(
'post_type' => 'cpt',
'tax_query' => array(
            array(
               'taxonomy' => $ct_category,
               'field' => 'term_id',
               'terms' => $_POST['taxonomy_category']
                )
              )
           ); 
$loop = new WP_Query( $args );

I can guess that the issue is in 'terms' => $_POST['taxonomy_category']. If name attribute taxonomy_category['.$term->term_id.'] can be displayed as array in 'terms' =>, the issue would be fixed. Spent plenty of times searching google but couldn't find any solution.

Here is the full code

<?php

function add_meta_box() {
    add_meta_box( 'ct_metabox', 'CT', 'meta_box_content_output', 'cpt', 'normal' );
}
add_action( 'add_meta_boxes', 'add_meta_box' ); 


function meta_box_content_output ( $post ) {

    wp_nonce_field( 'save_meta_box_data', 'meta_box_nonce' );

    $taxonomy_category =  get_post_meta( $post->ID, 'taxonomy_category', true );

    function categories_checkbox(){
        $terms = get_terms('ct_category');

        foreach ( $terms as $term ) {
        echo '<li class='.$term->term_id.'><label > <input class="taxonomy_category" type="checkbox" name="taxonomy_category['.$term->term_id.']" value ="'.$term->term_id.'" />'.$term->name.'</label ></li>';          
        }
    }
<?

<ul>
    <li>
        <?php categories_checkbox() ?>
    <li>
</ul>         

<?php
}

function save_meta_box_data( $post_id ) {

    // Check if our nonce is set.
    if ( ! isset( $_POST['meta_box_nonce'] ) ) {
        return;
    }

    // Verify that the nonce is valid.
    if ( ! wp_verify_nonce( $_POST['meta_box_nonce'], 'save_meta_box_data' ) ) {
        return;
    }

    // If this is an autosave, our form has not been submitted, so we don't want to do anything.
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
        return;
    }

    // Check the user's permissions.
    if ( isset( $_POST['post_type'] ) && 'page' == $_POST['post_type'] ) {

        if ( ! current_user_can( 'edit_page', $post_id ) ) {
            return;
        }

    } else {

        if ( ! current_user_can( 'edit_post', $post_id ) ) {
            return;
        }
    }



    $taxonomy_category_value = "";

   if(isset($_POST["logo_taxonomy_category"])) {
        $taxonomy_category_value = sanitize_text_field( $_POST["logo_taxonomy_category"] );
    }   
    update_post_meta($post_id, "logo_taxonomy_category", $taxonomy_category_value);

}
add_action( 'save_post', 'save_meta_box_data' );
?>

<?php
$args = array(
  'post_type' => 'cpt',
  'tax_query' => array(
        array(
           'taxonomy' => $ct_category,
           'field' => 'term_id',
           'terms' => $taxonomy_category
            )
          )
       ); 
$loop = new WP_Query( $args );
?>

To display all the categories name with checkbox from custom taxonomy called ct_category I have the following code:

$terms = get_terms('ct_category');
foreach ( $terms as $term ) {
echo '<li class='.$term->term_id.'><label > <input class="taxonomy_category" type="checkbox" name="taxonomy_category['.$term->term_id.']" value ="'.$term->term_id.'" />'.$term->name.'</label ></li>';          
}                                             

I would like to display content only from checked categories. I tried following that didn't work:

$args = array(
'post_type' => 'cpt',
'tax_query' => array(
            array(
               'taxonomy' => $ct_category,
               'field' => 'term_id',
               'terms' => $_POST['taxonomy_category']
                )
              )
           ); 
$loop = new WP_Query( $args );

I can guess that the issue is in 'terms' => $_POST['taxonomy_category']. If name attribute taxonomy_category['.$term->term_id.'] can be displayed as array in 'terms' =>, the issue would be fixed. Spent plenty of times searching google but couldn't find any solution.

Here is the full code

<?php

function add_meta_box() {
    add_meta_box( 'ct_metabox', 'CT', 'meta_box_content_output', 'cpt', 'normal' );
}
add_action( 'add_meta_boxes', 'add_meta_box' ); 


function meta_box_content_output ( $post ) {

    wp_nonce_field( 'save_meta_box_data', 'meta_box_nonce' );

    $taxonomy_category =  get_post_meta( $post->ID, 'taxonomy_category', true );

    function categories_checkbox(){
        $terms = get_terms('ct_category');

        foreach ( $terms as $term ) {
        echo '<li class='.$term->term_id.'><label > <input class="taxonomy_category" type="checkbox" name="taxonomy_category['.$term->term_id.']" value ="'.$term->term_id.'" />'.$term->name.'</label ></li>';          
        }
    }
<?

<ul>
    <li>
        <?php categories_checkbox() ?>
    <li>
</ul>         

<?php
}

function save_meta_box_data( $post_id ) {

    // Check if our nonce is set.
    if ( ! isset( $_POST['meta_box_nonce'] ) ) {
        return;
    }

    // Verify that the nonce is valid.
    if ( ! wp_verify_nonce( $_POST['meta_box_nonce'], 'save_meta_box_data' ) ) {
        return;
    }

    // If this is an autosave, our form has not been submitted, so we don't want to do anything.
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
        return;
    }

    // Check the user's permissions.
    if ( isset( $_POST['post_type'] ) && 'page' == $_POST['post_type'] ) {

        if ( ! current_user_can( 'edit_page', $post_id ) ) {
            return;
        }

    } else {

        if ( ! current_user_can( 'edit_post', $post_id ) ) {
            return;
        }
    }



    $taxonomy_category_value = "";

   if(isset($_POST["logo_taxonomy_category"])) {
        $taxonomy_category_value = sanitize_text_field( $_POST["logo_taxonomy_category"] );
    }   
    update_post_meta($post_id, "logo_taxonomy_category", $taxonomy_category_value);

}
add_action( 'save_post', 'save_meta_box_data' );
?>

<?php
$args = array(
  'post_type' => 'cpt',
  'tax_query' => array(
        array(
           'taxonomy' => $ct_category,
           'field' => 'term_id',
           'terms' => $taxonomy_category
            )
          )
       ); 
$loop = new WP_Query( $args );
?>
Share Improve this question edited Mar 6, 2016 at 9:32 Babu asked Mar 6, 2016 at 2:14 BabuBabu 332 silver badges8 bronze badges 0
Add a comment  | 

2 Answers 2

Reset to default 2

If $taxonomy_category is already an array, then:

'terms' => array($taxonomy_category)

should just be:

'terms' => $taxonomy_category

where $taxonomy_category is equal to whatever was submitted from your form as $_POST['taxonomy_category'] or $_GET['taxonomy_category']

It is an advanced question that would take me time to debug. However I believe the 'id' value for 'field' is not right. From the documentation,

field (string) - Select taxonomy term by. Possible values are 'term_id', 'name', 'slug' or 'term_taxonomy_id'. Default value is 'term_id'.

I think you want term_id...

Try 2:

Click into your custom taxonomy called 'ct_category' and get the slug for your taxonomy. You do this in the backend probably a submenu of a custom post type.

set up your tax_query

$tax_args = array(
    array('taxonomy' => 'ct_category', 
    'field' => 'slug', 
    'terms' => 'term_slug', 
    'operator' => 'IN')
); 

Set up your args

$args = array('post_type' => 'cpt',
    'post_status' => 'publish', 
    'posts_per_page'=>-1, 
    'tax_query'=>$tax_args); 

$query = new WP_Query($args);

See if you can get that working...then work on getting it to work with term_id.

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far