0
votes

We all know that when creating a new custom post type, if we want to query the database on that particular custom post type, we must specify the name within:

One Custom Post Type:

'post_type' => 'NAMECPT_ONE',

Two or more Custom Post Type

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

now I wanted to know if a method existed in order to create a new custom post type, instead of modifying all the queries by adding NAMECPT_THREEcould pull out the whole list of custom post types and insert it dynamically in post_type

it's possible?

Method create new 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
can you tell me which method used for create post type?Jinesh
@Jinesh i m create a new plugin, see my question i update the methoddcer

1 Answers

1
votes

You can use like this

/*
 * 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);
}

Now when you want to show post you can use like this

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

   $loop = new WP_Query( $args );