0

I am trying to get all the Custom Post Types Registered either by theme or plugin, but no method is working.

$postargs = array(
   'public'   => true,
   '_builtin' => false
);

$postoutput = 'names';
$postoperator = 'and';    
$post_types = get_post_types( $postargs, 'names', 'and' ); 
/// Main Loop Start
foreach ($post_types as $post_type){  
//and then i want to Get all the Custom Taxonomies for those post types in a loop
$taxonomies = get_object_taxonomies('$post_type', 'objects');    
// Loop 
foreach ($taxonomies as $taxonomy){
// Do Stuff with taxonomy
}    // Loop




    }    /// Main Loop Ends

But the problem is only WordPress Defaults Post Types are showing up if i do

'_builtin' => true
  • post
  • page
  • attachment

attachment

This is the Code to Registered Post Type I don't get any post type, i currently have 8 Custom Post Types in the theme. the post is registered with this code...

$args = array(            
            'labels' => array(
                'name' => __( 'Faq', 'yit' ),
                'singular_name' => __( 'Faq', 'yit' ),
                'plural_name' => __( 'Faqs', 'yit' ),
                'item_name_sing' => __( 'Faq', 'yit' ),
                'item_name_plur' => __( 'Faqs', 'yit' ),
                'add_new' => __( 'Add New Faq', 'yit' ),
                'add_new_item' => __( 'Add New Faq', 'yit' ),
                'edit' => __( 'Edit', 'yit' ),
                'edit_item' => __( 'Edit Faq', 'yit' ),
                'new_item' => __( 'New Faq', 'yit' ),
                'view' => __( 'View Faq', 'yit' ),
                'view_item' => __( 'View Faq', 'yit' ),
                'search_items' => __( 'Search Faqs', 'yit' ),
                'not_found' => __( 'No Faqs', 'yit' ),
                'not_found_in_trash' => __( 'No Faqs in the Trash', 'yit' ),
            ),            
            'hierarchical' => false,
            'public' => true,
            //'menu_position' => 30,
            //'icon_menu' => ,
            'has_archive' => 'faq',
            'rewrite' => array( 'slug' => apply_filters( 'yit_faqs_rewrite', 'faq' ) ),
            'supports' => array( 'title', 'editor', 'cats'),
            'description' => "Faq"

        );
        register_post_type('faq', $args);   
4
  • Are you sure the custom post types are properly registered? Are they public? What do you get if you just use get_post_types() with no arguments? Commented Mar 20, 2014 at 10:54
  • Yes, i tried woocommerce plugin which adds custom post types, i tried few themes which have custom post types, still same results. only above 3 post type show up if i do '_builtin' => true Commented Mar 20, 2014 at 11:07
  • You should be setting '_builtin' => false otherwise you will only be showing the built in post types (Quote Codex: If true, will return WordPress default post types. Use false to return only custom post types.)
    – MBL
    Commented Mar 20, 2014 at 11:13
  • I would register your FAQ post type on the init hook. When are you attempting to query the post types? Any time after init should be fine. I would suggest you disable all plugins until you sort this out. Commented Mar 20, 2014 at 16:36

3 Answers 3

0

Try this (tested) code below. I believe the issue was the passing of the $post_type parameter to get_object_taxonomies (i.e. you passed it in quotes as '$post_type'. If uncomment the two var_dump's you'll see what information is outputted.

<?php
    $postargs = array(
       'public'   => true,
       '_builtin' => false
    );

    $post_types = get_post_types( $postargs, 'names', 'and' ); 

    // Main Loop Start
    foreach ($post_types as $post_type){  
        //var_dump($post_type); 
        $taxonomies = get_object_taxonomies($post_type, 'objects');    
        foreach ($taxonomies as $taxonomy){
                //var_dump($taxonomy);

                // Do Stuff with taxonomy here


            }//foreach taxonomy
    }//foreach post_type
?>

Note: I've tested this with custom post types registered by a theme, but not with custom post types registered by plugins, but this should work for those too

5
  • Please check the modified Answer, i still don't get the Custom Post Types. Currently nothing show up unlit i do builtin true. Commented Mar 20, 2014 at 13:57
  • What do you get if you do $post_types = get_post_types(); var_dump($post_types);?
    – MBL
    Commented Mar 20, 2014 at 14:44
  • Nothing,, its all empty... Commented Mar 23, 2014 at 8:26
  • What do you get if you try the codex example to output a list of all public custom post types? If that shows up null you'll need to check your theme/plugin post types are created correctly
    – MBL
    Commented Mar 24, 2014 at 0:02
  • yes it shows null, tried it with official woocommerce plugin, but nothing show up. Commented Mar 24, 2014 at 11:16
0

Okay i found the problem was var_dump was executed before wordpress gets to any registered post type. so i added 199 in its priority and it worked. :) Thank you all for your help...

function test(){
var_dump( get_post_types(array('_builtin' => false)));
}
add_action ('admin_init','test', 199);
0

I found the problem is not by using the get_post_types function, but where we call this function. I'll describe it, clearly.

Create a function:

function get_all_custom_post_types() {
    $args = array( 'public' => true, '_builtin' => false );
    $output = 'objects'; //'names'; // names or objects, note names is the default
    $operator = 'and'; // 'and' or 'or'
    $custom_post_types = get_post_types( $args, $output, $operator );           
    $post_types = array();
    foreach ( $custom_post_types as $k => $post_type ) {
        $post_types = $post_type;
    }           
    return $post_types ;
}

Use this custom function where you want to call this, but use only in any WordPress action or filter callback function, e.g. when creating a callback function for add_metabox action, then call this function:

add_action( 'add_meta_boxes', 'add_mb_option' );
function add_mb_option() {  
    add_meta_box( 'mb-option', 'custom metabox detail', 'add_mb_option_element',
        'custom_metabox', 'normal', 'high' );
}
function add_mb_option_element() {
    $cpts = get_all_custom_post_types();
    // More custom code will go here
}

Very simple to use.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.