My goal is to inside a plugin pull all the posts/pages/custom post types in the project and create a custom taxonomy if it does not exist. Whenever the plugin goes inside of the if statement the php stops running on the page. As you can see in the foreach statement, I echo the post type name and the taxonomy associated with it.
I just want to create a custom taxonomy on a publicly available post type if the taxonomy does not exist.
I am able to run the taxonomy code that is inside the if statement inside the functions.php file so long as I replace the variables with the proper post types.
I also tried to instead of using add_action( 'init', 'custom_taxo_cpt_taxonomy', 1 ); just call the function directly via custom_taxo_cpt_taxonomy() on the same line as add_action( 'init', 'custom_taxo_cpt_taxonomy', 1 );
The echos/var_dumps are able to give me the post types and taxonomies associated without issue on the page so I know that is being pulled fine on the page.
<?php
$args = array(
'public' => true,
);
$output = 'names';
$operator = 'and';
$post_types = get_post_types( $args, $output, $operator );
foreach ( $post_types as $post_type ) {
$cpt_taxo_ar = get_object_taxonomies($post_type);
$cpt_taxo_ar = array_shift($cpt_taxo_ar);
echo '<p>' . $post_type . ' category: ' . $cpt_taxo_ar . '</p>';
if($cpt_taxo_ar != $post_type . '_custom_taxo'){
echo $post_type . '_custom_taxo';
// Register CustomTaxo Tags Taxonomy
function custom_taxo_cpt_taxonomy() {
$labels = array(
'name' => _x( 'CustomTaxo Tags', 'CustomTaxo Tags', 'custom_taxo_domain' ),
'singular_name' => _x( 'CustomTaxo Tag', 'CustomTaxo Tag', 'custom_taxo_domain' ),
'menu_name' => __( 'CustomTaxo Tags', 'custom_taxo_domain' ),
'all_items' => __( 'All Tags', 'custom_taxo_domain' ),
'parent_item' => __( 'Parent Tag', 'custom_taxo_domain' ),
'parent_item_colon' => __( 'Parent Tag:', 'custom_taxo_domain' ),
'new_item_name' => __( 'New Tag Name', 'custom_taxo_domain' ),
'add_new_item' => __( 'Add New Tag', 'custom_taxo_domain' ),
'edit_item' => __( 'Edit Tag', 'custom_taxo_domain' ),
'update_item' => __( 'Update Tag', 'custom_taxo_domain' ),
'view_item' => __( 'View Tag', 'custom_taxo_domain' ),
'separate_items_with_commas' => __( 'Separate items with commas', 'custom_taxo_domain' ),
'add_or_remove_items' => __( 'Add or remove tags', 'custom_taxo_domain' ),
'choose_from_most_used' => __( 'Choose from the most used', 'custom_taxo_domain' ),
'popular_items' => __( 'Popular tags', 'custom_taxo_domain' ),
'search_items' => __( 'Search tags', 'custom_taxo_domain' ),
'not_found' => __( 'Not Found', 'custom_taxo_domain' ),
'no_terms' => __( 'No items', 'custom_taxo_domain' ),
'items_list' => __( 'Tags list', 'custom_taxo_domain' ),
'items_list_navigation' => __( 'Tags list navigation', 'custom_taxo_domain' ),
);
$args = array(
'labels' => $labels,
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_admin_column' => true,
);
register_taxonomy( $post_type . '_custom_taxo', 'page', $args );
} // end taxo function
add_action( 'init', 'custom_taxo_cpt_taxonomy', 1 );
} //end for loop
}
?>
,on your arrays. All example I have found use0as priority foradd_action. Maybe try this :add_action( 'init', 'custom_taxo_cpt_taxonomy', 0 );. Maybe these can help you create-custom-taxonomies-wordpress and here wordpress-custom-post-type-cpt-and-taxonomy-hello-world-tutorial-tips-and-tricks - Shim-Saoerror_reporting(E_ALL); ini_set('display_errors', 1);- Shim-Sao$custom_taxo_cpt_taxonomy = function () { }andadd_action( 'init', $custom_taxo_cpt_taxonomy, 1 );according to the documentationadd_action( string $tag, callable $function_to_add, int $priority = 10, int $accepted_args = 1 )- Shim-Sao