Wordpress-wp_query中的动态自定义帖子类型

时间:2019-10-03 11:22:02

标签: wordpress custom-post-type

我们都知道,在创建新的自定义帖子类型时,如果要查询该特定自定义帖子类型的数据库,则必须在以下范围内指定名称:

一个自定义帖子类型:

'post_type' => 'NAMECPT_ONE',

两个或多个自定义帖子类型

'post_type' => array('NAMECPT_ONE, NAMECPT_TWO'),

现在,我想知道是否存在一种方法来创建新的自定义帖子类型,而不是通过添加NAMECPT_THREE来修改所有查询,而是可以提取出整个自定义帖子类型列表,然后将其动态插入post_type

有可能吗?

方法创建新的cpt:

/*
 * CREZIONE CUSTOM POST TYPE - SUITE
 */
add_action('init', 'crea_eventi_suite');
function crea_eventi_suite() {
    $labels = array(
        'name'               => __('Suite' , 'suite-plugin'),
        'singular_name'      => __('Suite' , 'suite-plugin'),
        'add_new'            => __('Aggiungi Evento', 'suite-plugin'),
        'add_new_item'       => __('Aggiungi Nuovo Evento' , 'suite-plugin'),
        'edit_item'          => __('Modifica Evento', 'suite-plugin'),
        'new_item'           => __('Nuovo Evento', 'suite-plugin'),
        'all_items'          => __('Tutti gli eventi Suite', 'suite-plugin'),
        'view_item'          => __('Vedi Eventi Suite' , 'suite-plugin'),
        'search_items'       => __('Cerca Evento Suite' , 'suite-plugin'),
        'not_found'          => __('Evento Non Trovato', 'suite-plugin'),
        'not_found_in_trash' => __('Evento Non Trovato nel cestino', 'suite-plugin'),
    );
    $args = array(
        'labels'             => $labels,
        'public'             => true,
        'rewrite'            => array('slug' => 'eventi-suite'),
        'has_archive'        => true,
        'hierarchical'       => true,
        'menu_position'      => 22,
        'menu_icon'          => 'dashicons-welcome-write-blog',
        'supports'           => array(
            'title',
            'editor',
            'thumbnail',
            'excerpt',
            'page-attributes'
        ),
    );
    register_post_type('eventi-suite', $args);
}

1 个答案:

答案 0 :(得分:1)

您可以这样使用

/*
 * CREZIONE CUSTOM POST TYPE - SUITE
 */
add_action('init', 'crea_eventi_suite');
function crea_eventi_suite() {
    $labels = array(
        'name'               => __('Suite' , 'suite-plugin'),
        'singular_name'      => __('Suite' , 'suite-plugin'),
        'add_new'            => __('Aggiungi Evento', 'suite-plugin'),
        'add_new_item'       => __('Aggiungi Nuovo Evento' , 'suite-plugin'),
        'edit_item'          => __('Modifica Evento', 'suite-plugin'),
        'new_item'           => __('Nuovo Evento', 'suite-plugin'),
        'all_items'          => __('Tutti gli eventi Suite', 'suite-plugin'),
        'view_item'          => __('Vedi Eventi Suite' , 'suite-plugin'),
        'search_items'       => __('Cerca Evento Suite' , 'suite-plugin'),
        'not_found'          => __('Evento Non Trovato', 'suite-plugin'),
        'not_found_in_trash' => __('Evento Non Trovato nel cestino', 'suite-plugin'),
    );
    $args = array(
        'labels'             => $labels,
        'public'             => true,
        'rewrite'            => array('slug' => 'eventi-suite'),
        'has_archive'        => true,
        'hierarchical'       => true,
        'menu_position'      => 22,
        'menu_icon'          => 'dashicons-welcome-write-blog',
        'supports'           => array(
            'title',
            'editor',
            'thumbnail',
            'excerpt',
            'page-attributes'
        ),
    );
   update_option('customer_postquery',get_option('customer_postquery')+',new-post_type');

    register_post_type('eventi-suite', $args);
}

现在,当您想显示帖子时,可以这样使用

 $args = array(  
       'post_type' => get_option('customer_postquery'),
       'post_status' => 'publish',
       'posts_per_page' => 8,
       ‘orderby’ => ‘title’,
       ‘order’ => ‘ASC’,
   );

   $loop = new WP_Query( $args );