提取所有分类的查询,而不是我指定的分类

时间:2016-05-09 作者:Taylor Foster

我正试图从一个自定义的帖子类型中随机抽取一篇帖子,我给它一个分类法。出于某种原因,它正在发布任何帖子,无论是哪一类。

这是我的帖子类型

if (!function_exists(\'register_recipe_post_type\')):

//Registering Post Type

function register_recipe_post_type() {
    $labels = array(
        \'name\'                => __(\'Recipes\'),
        \'singular_name\'       => __(\'Recipe\'),
        \'menu_name\'           => __(\'Recipes\'),
        \'parent_item_colon\'   => __(\'Parent Recipe:\'),
        \'all_items\'           => __(\'All Recipes\'),
        \'view_item\'           => __(\'View Recipe\'),
        \'add_new_item\'        => __(\'Add New Recipe\'),
        \'add_new\'             => __(\'Add New Recipe\'),
        \'edit_item\'           => __(\'Edit Recipe\'),
        \'update_item\'         => __(\'Update Recipe\'),
        \'search_items\'        => __(\'Search Recipes\'),
        \'not_found\'           => __(\'No Recipes found.\'),
        \'not_found_in_trash\'  => __(\'No Recipes found in trash.\')
    );

    $rewrite = array(
        \'slug\'                => \'recipe\',
        \'with_front\'          => false,
        \'pages\'               => true,
        \'feeds\'               => true,
    );

    $args = array(
        \'label\'               => __(\'Recipe\'),
        \'description\'         => __(\'Recipes\'),
        \'labels\'              => $labels,
        \'taxonomies\'          => array(\'vodka_type\'),
        \'supports\'            => array(\'title\',\'editor\', \'thumbnail\', \'excerpt\'),
        \'hierarchical\'        => false,
        \'public\'              => true,
        \'show_ui\'             => true,
        \'show_in_menu\'        => true,
        \'show_in_nav_menus\'   => false,
        \'show_in_admin_bar\'   => true,
        \'menu_position\'       => 5,
        \'menu_icon\'           => \'dashicons-palmtree\',
        \'can_export\'          => true,
        \'has_archive\'         => false,
        \'exclude_from_search\' => false,
        \'publicly_queryable\'  => true,
        \'rewrite\'             => $rewrite,
        \'capability_type\'     => \'post\'
    );

   register_post_type(\'recipe\', $args);

   flush_rewrite_rules();
}
add_action(\'init\', \'register_recipe_post_type\');

endif;
这是我的自定义分类法

// Register Custom Taxonomy
function vodka_type_taxonomy() {

$labels = array(
    \'name\'                       => _x( \'Vodka Types\', \'Taxonomy General Name\', \'text_domain\' ),
    \'singular_name\'              => _x( \'Vodka Type\', \'Taxonomy Singular Name\', \'text_domain\' ),
    \'menu_name\'                  => __( \'Vodka Type\', \'text_domain\' ),
    \'all_items\'                  => __( \'All Items\', \'text_domain\' ),
    \'parent_item\'                => __( \'Parent Item\', \'text_domain\' ),
    \'parent_item_colon\'          => __( \'Parent Item:\', \'text_domain\' ),
    \'new_item_name\'              => __( \'New Vodka Type\', \'text_domain\' ),
    \'add_new_item\'               => __( \'Add New Vodka Type\', \'text_domain\' ),
    \'edit_item\'                  => __( \'Edit Vodka Type\', \'text_domain\' ),
    \'update_item\'                => __( \'Update Item\', \'text_domain\' ),
    \'view_item\'                  => __( \'View Item\', \'text_domain\' ),
    \'separate_items_with_commas\' => __( \'Separate items with commas\', \'text_domain\' ),
    \'add_or_remove_items\'        => __( \'Add or remove Vodka Type\', \'text_domain\' ),
    \'choose_from_most_used\'      => __( \'Choose from the most used\', \'text_domain\' ),
    \'popular_items\'              => __( \'Popular Items\', \'text_domain\' ),
    \'search_items\'               => __( \'Search Vodka Types\', \'text_domain\' ),
    \'not_found\'                  => __( \'Not Found\', \'text_domain\' ),
    \'no_terms\'                   => __( \'No items\', \'text_domain\' ),
    \'items_list\'                 => __( \'Items list\', \'text_domain\' ),
    \'items_list_navigation\'      => __( \'Items list navigation\', \'text_domain\' ),
);
$args = array(
    \'labels\'                     => $labels,
    \'hierarchical\'               => true,
    \'public\'                     => true,
    \'show_ui\'                    => true,
    \'show_admin_column\'          => true,
    \'show_in_nav_menus\'          => true,
    \'show_tagcloud\'              => true,
);
register_taxonomy( \'vodka_type\', array( \'post\' ), $args );

}
add_action( \'init\', \'vodka_type_taxonomy\', 0 );
这里是我正在执行的查询,它提取任何分类法,而不是我指定的分类法

// WP_Query arguments
$args = array (
\'post_type\'              => array( \'recipe\' ),
\'post_status\'            => array( \'publish\' ),
\'orderby\'                => \'rand\',
\'posts_per_page\'         => \'1\',
\'tax_query\'              => array(
    \'taxonomy\' => \'vodka_type\',
    \'field\' => \'slug\',
    \'terms\' => \'origin\', // <-- This is the tax. it\'s supposed to be
),
);
// The Query
$query = new WP_Query( $args );
if($query->have_posts()) :
  while ($query->have_posts()) : $query->the_post();
   // Do Stuff With The Post
  endwhile;
  wp_reset_query();
endif; 

1 个回复
SO网友:Taylor Foster

找到了答案。解决方案是将税务查询数组包含在另一个数组中:

// tax_query needs to be an Array of an Array
\'tax_query\'  => array(
    array(
      \'taxonomy\' => \'vodka_type\',
      \'field\' => \'slug\',
      \'terms\' => $type,
    ),
),