1

I have some problem with taxonomy and custom post saved from form in front-end.

I have custom post with attached taxonomy to it, and three terms in this taxonomy.

Taxonomy was defined in functions.php, plugin and as mu-plugin. And there is no way to access this taxonomy from other plugin that generates form and sends POST data to

wp_set_object_terms( $newPostId, intval(14), 'custom_taxonomy');

it's not working - It throws "invalid taxonomy" so I checked

taxonomy_exists('custom_taxonomy');

And it throws [0] it means that there is no [custom_taxonomy]

How to make taxonomy visible for plugins? Is there some hooks, actions, init sequence for that?

Please help.

Im setting taxonomy with Types plugin, and by my self in functions.php. Now in plugin in folder mu-plugin, so it should be set before other plugins. There is no change. Taxonomy is not visible durning sending post from front-end.

// Register custom taxonomy

function add_taxonomy_kategorie_ofert()
{

$labels = array(
'slug' =>'kategorie_ofert',
'name' => _x('Kategoria Ofert', 'Taxonomy General Name'),
'singular_name' => _x('Kategoria', 'Taxonomy Singular Name'),
'menu_name' => __('Kategorie'),
'all_items' => __('Wszystkie'),
'parent_item' => __('Rodzic'),
'parent_item_colon' => __('Rodzic:'),
'new_item_name' => __('Nowa nazwa'),
'add_new_item' => __('Dodaj'),
    'edit_item' => __('Edytuj'),
    'update_item' => __('Aktualizuj'),
    'separate_items_with_commas' => __('Oddziel przecinkami'),
    'search_items' => __('Szukaj'),
    'add_or_remove_items' => __('Dodaj lub usuń'),
    'choose_from_most_used' => __('Wybierz z popularnych'),
    'not_found' => __('Brak'),
);
$rewrite = array(
    'slug' => 'praca',
    'with_front' => true,
    'hierarchical' => true,
);
$args = array(
    'labels' => $labels,
    'hierarchical' => true,
    'public' => true,
    'show_ui' => true,
    'show_admin_column' => true,
    'show_in_nav_menus' => true,
    'show_tagcloud' => false,
    'rewrite' => $rewrite,
);
register_taxonomy('kategorie_ofert', array('oferty_pracy'), $args);

}


// Register Custom Post Type
function praca_post_type()
{

$labels = array(
'name' => _x('Oferty', 'Post Type General Name'),
    'singular_name' => _x('Oferta pracy', 'Post Type Singular Name'),
    'menu_name' => __('Oferty pracy'),
    'parent_item_colon' => __('Parent Item:'),
    'all_items' => __('Wszystkie'),
    'view_item' => __('Podgląd'),
    'add_new_item' => __('Dodawanie oferty'),
    'add_new' => __('Dodaj'),
    'edit_item' => __('Edytuj'),
    'update_item' => __('Aktualizuj'),
    'search_items' => __('Szukaj'),
    'not_found' => __('Nie znaleziono'),
    'not_found_in_trash' => __('Nie znaleziono w koszu'),
);
$rewrite = array(
    'slug' => 'oferty_pracy',
    'with_front' => true,
    'pages' => true,
    'feeds' => true,
);
$args = array(
    'label' => __('Oferty pracy'),
    'description' => __('Oferty pracy BiL'),
    'labels' => $labels,
    'supports' => array('title', 'editor', 'excerpt', 'thumbnail'),
    'taxonomies' => array('kategorie_ofert'),
    'hierarchical' => true,
    'public' => true,
    'show_ui' => true,
    'show_in_menu' => true,
    'show_in_nav_menus' => true,
    'show_in_admin_bar' => true,
    'menu_position' => 5,
    'can_export' => true,
    'has_archive' => true,
    'exclude_from_search' => false,
    'publicly_queryable' => true,
    'query_var' => 'oferty_pracy',
    'rewrite' => $rewrite,
    'capability_type' => 'post',
);
register_post_type('oferty_pracy', $args);

}
add_action('init', 'add_taxonomy_kategorie_ofert', 0);
add_action('init', 'praca_post_type', 0);
register_taxonomy_for_object_type( 'kategorie_ofert',   'oferty_pracy');

This is how I get POST data in plugin, and sending it to new custom post.

$new_post = array(
    'post_title' => sanitize_text_field($_POST["title"]),
    'post_content' => $_POST["description"],
    'post_excerpt' => $_POST["excerpt"],
    'post_name' => slugify($Post_Title),
    'post_category' => array(0),
    'tags_input' => sanitize_text_field($_POST["tags"]),
    'post_status' => $Post_Status,
    'post_type' => 'oferty_pracy',
    'ping_status' => $Post_Ping,
    'comment_status' => $Post_Comment,
    'post_author' => $_POST["author"],
    'tags'=> tagsToArray($_POST["tags"])

);

$New_PID = wp_insert_post($new_post);

And then I was trying to add object meta, but throws errors. So I decide to make

function hook_header()  {
 add_terms_to_customPost_tax('kategorie_ofert',$New_PID,$Post_Category,$Post_Tags);
}
add_action('wp_head','hook_header');

And function [add_terms_to_customPost_tax] is stored in functions.php or in plugin in folder [mu-plugin] and is active.

function add_terms_to_customPost_tax($taxonomyName, $postId,     $terms,$tags){



wp_set_object_terms( $postId, $tags, 'post_tag');
$temp = wp_set_object_terms( $postId, intval(14), 'kategorie_ofert');

}

I was trying all options, thats why I wrote about there places. And plugin witch saves the post data have init and other hooks like after 'muplugins_loaded' and after 'wp_loaded'. It doesn't work. I even if I define in the same plugin my taxonomy, without init so I think that it should be visible to that plugin- but not. I think if i write in my plugin

add_action( 'wp_loaded', 'my_front_end_function'); 

function my_front_end_function() { 
wp_set_object_terms( $postId, $tags, 'post_tag');
} 

should be fired after init witch fires my taxonomy.

  • 1
    We need more context to answer- What arguments do you register your taxonomy with? Where are you POSTing data? When do you call wp_set_object_terms or taxonomy_exists? – Milo Dec 5 '16 at 2:34
  • It's still not entirely clear when things happen. It's irrelevant where your taxonomy is registered- an mu plugin, a regular plugin, or functions.php- they all should be hooked to init, so registration happens at exactly the same time in each case. One issue that I see immediately is that you call register_taxonomy_for_object_type before the taxonomy or post type are registered. And your code which inserts your post is also missing context- if it runs when the plugin file is loaded, that is far too early, it needs to be hooked to run after everything is set up, usually init action. – Milo Dec 5 '16 at 17:20
  • I was trying all options, thats why I wrote about there places. And plugin witch saves the post data have init and other hooks like after 'muplugins_loaded' and after 'wp_loaded'. It doesn't work. I even if I define in the same plugin my taxonomy, without init so I think that it should be visible to that plugin- but not. I think if i write in my plugin 'add_action( 'wp_loaded', 'my_front_end_function'); function my_front_end_function() { wp_set_object_terms( $postId, $tags, 'post_tag'); }' should be fired after init witch fires my taxonomy. – Zbigniew Adam Karski Dec 5 '16 at 20:08
  • Nothing, wordpress.stackexchange.com/questions/95757/… And this too. At this point I'm gonna send back in POST data to form and on success I will save term in taxonomy. ONLY THIS WORKS. One more Bug in wordpress that takes one day to resolve. – Zbigniew Adam Karski Dec 5 '16 at 23:51
  • If you can post complete code that I can actually try, I can help you fix it, but as-is your code makes very little sense to me. It's still not clear how you are receiving POST data and when you are trying to insert the post. – Milo Dec 6 '16 at 3:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.