Adding postmeta default values after adding post - wordpress

I am using Wordpress XMLRPC to add posts to my blog.
However, after running this.
$data = array(
'title' => $title,
'description' => $content,
'post_type' => 'post',
'categories' => array($category),
'post_status' => 'publish'
);
$addedPostReturn = $this->_client->query('metaWeblog.newPost',
array(0,$username,$password,$data,1));
This adds the post fine but doesn't add the postmeta information.
If i open the post, click Update, all the default postmeta gets updated. However, I would like to add all the default postmeta information with my php script instead of manually (or else it kinda defeats the purpose).
Is there anyway, either with xmlrpc or regular wordpress functions to create the postmeta custom fields by using their default values? If not, is there a way to have the list of all the custom fields i need to manually add using the custom fields section of the metaWeblog.newPost function? (I don't want to add some but not others. I rather do a complete job)
Thanks in advance... any help is appreciated! :)
EDIT:
$postUpdateContent = array();
$postUpdateContent['ID'] = $newPostId;
$postUpdateContent['post_content'] = $sameContent;
wp_update_post( $postUpdateContent );
didn't seem to do the trick...

Related

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

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 ACF import by script needs save for Repeater field

I just made a script for importing a JSON feed to ACF custom fields pro.
When I insert a field which is just a normal field a 'save' action by click is not needed after import.
update_field('rating', $route['rating'], $post_id);
Everything goes well.
However when its a repeater field I use this code:
$startlocation = array();
$startlocation[] = array(
'lat' => $route['startingSpot']['location']['lat'],
'lng' => $route['startingSpot']['location']['lng'],
'description' => $route['startingSpot']['nl'],
'direction' => $route['startingSpot']['direction']
);
update_field('startlocation', $startlocation, $post_id );
It needs a click after the creation of the imported post. When I run the import again, the fields are filled in.
Does anyone know if i need a different script, or give a programmed click of the save, to add all the custom field, after a created post by script?
Thanks in advance,
Peter
For a repeater field of act you have to use the field_key not the name.
This was the answer to my question.
$startlocation = array();
$startlocation[] = array(
'lat' => $route['startingSpot']['location']['lat'],
'lng' => $route['startingSpot']['location']['lng'],
'description' => $route['startingSpot']['nl'],
'direction' => $route['startingSpot']['direction']
);
update_field('field_dhjawhdwkwkhd', $startlocation, $post_id );

wordpress count image attachments

I'm looking for some help, for some reason this code isn't working when trying to display total image attachments on the main index.php page.
// Get all the attachments
$attachments = get_posts ( array(
'numberposts' => -1,
'post_type' => 'attachment',
'post_mime_type' => 'image',
'post_parent' => get_the_ID(),
'post_status' => 'inherit',
) );
// Count all the attachments
$total = count( $attachments );
The problem is, the loop is in the main index.php page, but it's calling a post.php template so I have placed this code there instead (I'm assuming that's still in the loop?) I am then calling $total just below it where I want to show "View all # images".
Any ideas why this is just displaying the number as 0 even though I have added an image gallery to the post using the basic Wordpress gallery media library?
Thanks
The attachment count only increases if it's attached to a post. You can force the count by adding this:
$wp_taxonomies['category']->update_count_callback = '_update_generic_term_count';
in a function in your functions.php something like this:
function change_category_arg() {
global $wp_taxonomies;
if ( ! taxonomy_exists('category') )
return false;
$wp_taxonomies['category']->update_count_callback = '_update_generic_term_count';
}
add_action( 'init', 'change_category_arg' );
Explanation:
This is significant in the case of attachments. Because an attachment
is a type of post, the default _update_post_term_count() will be used.
However, this may be undesirable, because this will only count
attachments that are actually attached to another post (like when you
insert an image into a post). This means that attachments that you
simply upload to WordPress using the Media Library, but do not
actually attach to another post will not be counted. If your intention
behind associating a taxonomy with attachments was to leverage the
Media Library as a sort of Document Management solution, you are
probably more interested in the counts of unattached Media items, than
in those attached to posts. In this case, you should force the use of
_update_generic_term_count() by setting '_update_generic_term_count' as the value for update_count_callback.
from Wordpress Codex on register_taxonomy

Wordpress search custom fields

I have a custom field with meta_name is product_id. I am trying to determine how to search that field when using the wordpress search.
I know the wordpress search results page uses query_string which contains the "s" value for the keyword searched I just don't know how to change my code below to search the custom field mentioned.
$loop = new WP_Query("$query_string . '&posts_per_page=-1&post_type=product'"); ?>
think this section in the codex should have the answers you need with regards to adding in custom field parameters to you query:
http://codex.wordpress.org/Function_Reference/WP_Query#Custom_Field_Parameters
Not sure whether that will allow you to search within a particular custom field though. Can't be certain but I think you'd have to make some changes on the search form end of things. See how you get on with adding in the custom field parameters.
Off the top of my head I would think you would need something like:
$query = new WP_Query( array(
'meta_key' => 'product_id',
'meta_value' => $query_string,
'posts_per_page' => '-1',
'post_type' => 'product'
) );
Not tested though!

Resources