Wordpress: automatically convert custom field to custom taxonomy? - wordpress

I have this custom post type called "listing". People can use a front-end form to create a new listing. This front-end form has a custom field called "app_city". Now when someone submits the form, I would like this custom field value to be converted automatically to a term in the custom taxonomy "Cities" (under my cystom post type "listing").
Is such a thing possible to accomplish?
Thanks.

Yes it is possible through wp_insert_term(). But you have to digg your front-end form that handle the app_city custom field data. Just Use the wp_insert_term() after everything verified from the form.

Ok, so this worked for me.
Thanks!
$parent_term = term_exists( 'Cities' ); // array is returned if taxonomy is given
$parent_term_id = $parent_term['term_id']; // get numeric term id
$city = $_POST['app_plaatsnaam'];
wp_insert_term(
$_POST['app_city'], // the term
'Cities', // the taxonomy
array(
'parent'=> $parent_term_id
)
);
wp_set_object_terms( $listing_id, $city, 'Cities', false);

Related

Wordpress how to get custom taxonomies which are used with a custom post type?

I have 2 custom taxonomies.
Each of them are related with few post types to be used with.
For example, taxonomy 'topic' is related with custom post type 'product'.
taxonomy 'source' is related with custom post type 'product' and 'program'.
Now I want to get the all taxonomies related to custom post type 'product'.
Following is what I tried to do.
$args=array(
'object_type' => array('product')
);
$output = 'objects';
$taxonomies= get_taxonomies($args, $output);
The problem is that the get_taxonomies() function returns 'topic' taxonomy only.
But I want to get all of 2 taxonomies: 'topic' and 'source'.
Any advice?
You can try get_object_taxonomies
I have no environment right now so this code is not tested. Just give it a try and let me know:
$taxonomy_objects = get_object_taxonomies( 'product', 'objects' );
print_r( $taxonomy_objects); // debug
More info in Wordpress Codex

Add a user along with one custom post type

I am developing this plugin that admin can add a user in the backend and when user is created, plugin can automatically generate one custom post which I have added to the theme. The custom post will store user ID that is just created (or if it is possible make that user an author of the post)
I wonder if what I have mentioned above is possible practically. If anybody has any better advice, I am open for any suggestions.
Thank you in advance
I'm not sure that a unique custom post type per user is the best way to implement what you're wanting to achieve. If you have 100 users, you will have 100 custom post types making the wp-admin a nightmare as the left menu would grow with so many menu-items.
Wouldn't it be easier to just use a normal post type and then have the page that shows the user's dashboard filter the posts to only show posts where the user is the post_author? You could then add a hook to catch when a user registers and create the example post, you could modify the code below and add it to your functions.php:
add_action( 'user_register', 'myplugin_registration_save', 10, 1 );
function myplugin_registration_save( $user_id ) {
$userPostsCategory = 3;
// Create post object
$my_post = array(
'post_title' => 'Sample Story' ),
'post_content' => 'You can edit this or create a new story',
'post_status' => 'publish',
'post_author' => user_id,
'post_category' => array( $userPostsCategory )
);
// Insert the post into the database
wp_insert_post( $my_post );
}
This method will lower the number of customisations you'd have to do to your themes and make management of the posts a little easier.
Further reading on this:
User registration hook
Inserting a post using wp_insert_post

Custom-Field option not found in custom post type WP 4.6.1

I have created a custom post type call 'Movie'.Now,I want to add custom meta field in this call Movie Reviews.I have following code for custom post type.
function Create_Movies_Posttype()
{
register_post_type('Movies',
array(
'labels'=>array('name'=>__('Movies'),'singular_name'=>__('Movie')),
'public'=>true,
'has_archive'=>true,
'rewrite'=>array('slug'=>'movies'),
'support'=>array('title','custom-fields','edit'),
)
);
}
add_action('init','Create_Movies_Posttype');
Added Meta Box
function adding_custom_meta_boxes( $post ) {
add_meta_box(
'my-meta-box',
__( 'My Meta Box' ),
'render_my_meta_box',
'post',
'normal',
'default'
);
}
add_action( 'add_meta_boxes_post', 'adding_custom_meta_boxes' );
Any one please help me with this custom field.
You need to use add_meta_boxes() action to register metaboxes for a post type.
add_meta_box() function to define the box, and save_post action to save it.
You'll find everything you need by reading example on Codex add_meta_boxes
You need also to add supports parameter to the register post type arguments and add custom-field in the array defining the supports for it, all the native supports can be found here

Creating pages from Ninja form data

I've created a WordPress page with a Ninja form on it that collects miscellaneous data about a product, including some uploaded images. The page with the form is accessible from the main menu by clicking the "Input" item, so the user doesn't need to access the backend to upload their product data.
I now want to put this data into a custom post type called "Listing." There will eventually be thousands of these data sets and so thousands of "Listing" pages, as people come to the site, click Input in the main menu to get to the page with the Ninja form and fill it out.
Could someone tell me how they would go about now building these listing pages from the data the form has collected?
I'm running Ninja's Front-End Post option which supposedly will create a page from the form data. This plugin has some Post creation settings where you can select the post type to create, but this isn't working for me. I would expect the submitted form data to show up under dashboard | Listings, but there's nothing there after submitting the form.
Has anyone gotten this to work?
Thanks for your help.
I think you can use only Ninja Forms without extensions, and hook directly in 'ninja_forms_after_submission' that fires after submission and allow you to use data submitted and perform actions.
This is a starter codebase to achieve your result, but needs to be customized on your needs and your form structure.
add_action( 'ninja_forms_after_submission', 'create_page_from_ninjaform' );
function create_page_from_ninjaform( $form_data ){
// your fields data
$form_fields = $form_data[ 'fields' ];
// !!! this is an example, it depends form fields in your form
$title = $form_fields[ 1 ][ 'value' ];
$content = $form_fields[ 2 ][ 'value' ];
$sample_meta_field = $form_fields[ 3 ][ 'value' ];
$new_post = array(
'post_title' => $title,
'post_content' => $content,
'post_status' => 'publish',
'post_type' => 'listing', // be sure this is the post type name
);
$new_post_id = wp_insert_post( $new_post );
update_post_meta( $new_post_id, 'your_meta_key', $sample_meta_field );
}
This code should be copied in functions.php file
Not tested of course.
Good luck ;)
The Ninja Forms Front-end Posting extension isn't really meant for displaying form submission data on the front end.
From: https://ninjaforms.com/extensions/front-end-posting/
"The Ninja Forms Front-end Posting extension gives you the power of the WordPress post editor on any publicly viewable page you choose."
If you want to show Ninja Forms submission data on the front end, you will have to retrieve them from the database with code in functions.php or by writing a plugin (recommended). You could then parse and manipulate them and create a shortcode that would allow you to insert your formatted submission data easily in Wordpress posts or pages.
Here's a link to a feature request, asking for the same thing. The author of that request posted a link to a plugin (click Download as Plugin) they wrote which may do what you want or give you further insights as to how you could implement this.
https://github.com/wpninjas/ninja-forms/issues/892
If you do not mind paying a little money for a plugin I would recommend using gravity forms rather then ninja forms for more advanced stuff like this.
I manually create a custom post type "oproep" and used a gravityforms plugin to create a custom post from type oproep when an user submits the form.
Because you use custom post type archive pages www.mysite.com/oproep will be automatically created so you already have a list of "Listings". The singe pages www.mysite.com/oproep/title will also be created for you by default, you could override these templates as well if you would like depending on your theme.
The only thing you have to do is add a few php lines to your functions.php (or write your own plugin) that adds the custom post type. The rest all works automatically.
I went so far as writing code to make users able to edit their submissions, read custom taxonomy tags in dropdowns etc. You got lots and lots of more options using gravity forms.
FrancescoCarlucci's answer is correct, but just adding an additional comment: in case you want to specify by form field IDs which fields should go where in your post, NinjaForms passes the ID as a number (in my case for example, I needed field 136 for my post title). It may have been obvious but I racked my brain for a while until I figured it out.
function create_post($form_data) {
$form_fields = $form_data[ 'fields' ];
$post_fields = array(
'post_content' => '',
'post_content_filtered' => '',
'post_title' => '',
'post_excerpt' => '',
'post_status' => 'pending',
'post_type' => 'post',
);
foreach ($form_fields as $field) {
$field_id = $field[ 'id' ];
$field_key = $field[ 'key' ];
$field_value = $field[ 'value' ];
if ($field_id == 136) {
$post_fields['post_title'] = $field_value;
}
}
wp_insert_post($post_fields, true);
}

Wordpress- Get custom post type associated with a custom taxonomy

According to the WordPress Codex, the get_post_types() function has a 'taxonomies' parameter, but there is no explanation of exactly how it works.
$post_args = array(
'public' => true,
'taxonomies' => 'genre',
'show_ui' => true
);
$post_types = get_post_types($post_args);
This above code doesn't return anything even though genre is a taxonomy that is registered to the 'book' post type.
If i have a custom post type of 'books' with some custom taxonomies of say, 'author' and 'genre'... I'd like to use get_post_type() to return the post type associated with 'genre'... obviously 'books'. I'm trying to make something that will help set the post_type parameter in get_posts().. since that only searches posts by default and not custom post types. Obviously I could just set the post_type parameter, but I want to make it forward compatible with any future post types I might use in my different themes.
Although I'm a bit late to the party on this one:
taxonomies isn't a valid argument for get_post_types(), so at best it will simply ignore the argument and return a list of all registered post types.
Probably your best bet is the get_taxonomy() object:
$currentTaxonomy = get_query_var('taxonomy');
// Or: $currentTaxonomy = 'genre';
if ($currentTaxonomy) {
$taxObject = get_taxonomy($currentTaxonomy);
$postTypeArray = $taxObject->object_type;
}
This will give you an array of all post types that taxonomy is registered to.
Might want to use a combination of both: get_posts() and then pass in your post_type. Something like below might help get you started.
$post_types = get_post_types();
if ( is_category() || is_tag()) {
$post_type = get_query_var('article');
if ( $post_type )
$post_type = $post_type;
else
$post_type = $post_types;
$query->set('post_type', $post_type);
return $query;
}
}
You would NOT want to use is_category and is_tag, you would use something like is_in_taxonomy(). What exactly are you trying to do? I think you're trying to do the same thing as me which is...
on archive page for custom post type use a custom loop for ALL taxonomies of certain custom post type. like the following:
taxonomy-[MY-CPT].php
vs.
taxonomy-[MY-CUSTOM-TAXONOMY].php
I'm actually trying to do the same as we speak, I'll let you know what I come up with.
NOTE there I just made up the is_in_taxonomy()

Resources