按类别插件查询自定义帖子类型

时间:2021-03-09 作者:Cello Nascimento

我试图按类别slug查询自定义帖子类型,但它根本不起作用

我的自定义帖子类型定义:

add_action( \'init\', \'custom_post_albuns\' );
 
// The custom function to register a movie post type
function custom_post_albuns() {
 
  // Set the labels, this variable is used in the $args array
  $labels = array(
    \'name\'               => __( \'Albuns\' ),
    \'singular_name\'      => __( \'Album\' ),
    \'add_new\'            => __( \'Add New Album\' ),
    \'add_new_item\'       => __( \'Add New Album\' ),
    \'edit_item\'          => __( \'Edit Album\' ),
    \'new_item\'           => __( \'New Album\' ),
    \'all_items\'          => __( \'All Albuns\' ),
    \'view_item\'          => __( \'View Album\' ),
    \'search_items\'       => __( \'Search Albuns\' ),
    \'featured_image\'     => \'Featured Image\',
    \'set_featured_image\' => \'Add Featured Image\'
  );
 
  // The arguments for our post type, to be entered as parameter 2 of register_post_type()
  $args = array(
    \'labels\'            => $labels,
    \'description\'       => \'m6 Records Albuns\',
    \'public\'            => true,
    \'menu_position\'     => 6,
    \'supports\'          => array( \'title\', \'editor\', \'thumbnail\', \'excerpt\', \'comments\', \'custom-fields\' ),
    \'has_archive\'       => true,
    \'show_in_admin_bar\' => true,
    \'show_in_nav_menus\' => true,
    \'has_archive\'       => true,
    \'menu_icon\'         => \'dashicons-album\',
    \'query_var\'         => \'album\'
  );
 
  // Call the actual WordPress function
  // Parameter 1 is a name for the post type
  // Parameter 2 is the $args array
  register_post_type( \'album\', $args);
}

add_action( \'init\', \'create_album_taxonomies\', 0 );

function create_album_taxonomies() {
    $labels = array(
        \'name\'              => _x( \'Categories\', \'taxonomy general name\' ),
        \'singular_name\'     => _x( \'Category\', \'taxonomy singular name\' ),
        \'search_items\'      => __( \'Search Categories\' ),
        \'all_items\'         => __( \'All Categories\' ),
        \'parent_item\'       => __( \'Parent Category\' ),
        \'parent_item_colon\' => __( \'Parent Category:\' ),
        \'edit_item\'         => __( \'Edit Category\' ),
        \'update_item\'       => __( \'Update Category\' ),
        \'add_new_item\'      => __( \'Add New Category\' ),
        \'new_item_name\'     => __( \'New Category Name\' ),
        \'menu_name\'         => __( \'Categories\' ),
    );

    $args = array(
        \'hierarchical\'      => true, // Set this to \'false\' for non-hierarchical taxonomy (like tags)
        \'labels\'            => $labels,
        \'show_ui\'           => true,
        \'show_admin_column\' => true,
        \'query_var\'         => true,
        \'rewrite\'           => array( \'slug\' => \'albuns\' ),
    );

    register_taxonomy( \'albuns_categories\', array( \'album\' ), $args );
}
和我的查询:

$argsPost = array(
                    \'post_type\'=>\'album\', 
                    \'orderby\'=>\'date\',
                    \'order\'   => \'DESC\',
                    \'posts_per_page\'=>\'4\',
                    \'tax_query\' => array(
                        array(
                            \'taxonomy\' => \'albuns_categories\',
                            \'field\' => \'slug\', 
                            \'terms\' => array( \'ANYTERM\' ) 
                        )
                    )
                  );
而且我无法提供albuns列表(如果我删除tax\\u查询部分,它会返回所有“albuns”自定义帖子类型)

谢谢

1 个回复
SO网友:Cello Nascimento

我在另一个论坛上得到了一个对我很有效的答案,所以如果有人遇到类似的问题,我会在这里发布

$argsPost = array(
    \'post_type\'=>\'album\', 
    \'orderby\'=>\'date\',
    \'order\'   => \'DESC\',
    \'posts_per_page\'=>\'4\',
    \'tax_query\' => array(
        array(
            \'taxonomy\' => \'albuns_categories\',
            \'field\' => \'slug\', 
            \'terms\' => array( \'ANYTERM\' ),
            \'operator\' => \'IN\'
        )
    )
);
=D

相关推荐

使用新的WP-Query()从循环中过滤后期格式;

嗨,我目前正在为我的博客构建一个主题。下面的代码指向最新的帖子(特色帖子)。因为这将有一个不同的风格比所有其他职位。然而我想过滤掉帖子格式:链接使用我在循环中定义的WP查询,因为它给我带来了更多的灵活性。我该怎么做呢? <?php $featured = new WP_Query(); $featured->query(\'showposts=1\'); ?> <?php while ($featured->have_post