Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Gravity Forms wp insert & wp update post WordPress

Hi,
I have gravity forms and i would like to create a function that create posts and update them with the same form, i manage to make a part of it


add_action( 'gform_after_submission', 'set_post_content', 10, 2 );
function set_post_content( $entry, $form ) {


$get_title = $entry["1"];
$check_title = get_page_by_title($get_title, OBJECT, 'post' );



$panel_id = wp_update_post(array(
'ID' => $check_title->ID,
));

$wppanel = $entry["5"];
$paneltype = $entry["5"];

add_post_meta($panel_id, 'panel', $wppanel);
add_post_meta($panel_id, 'panel-type', $paneltype);

}


with this function the form updates the existing post, but it create a duplicate post as well.

is there a way to prevent the form from creating a post if already exists ?

Answers (5)

2015-06-29

Clifford P answers:

GF has the ability to create posts natively. Try https://wordpress.org/plugins/gravity-forms-post-updates/ for updating via front-end / GForm

Let me know if you're working with post type other than Posts


GHostman comments:

the form will be used by users so I want to avoid editing post manually


Clifford P comments:

Anyone who has access to the page you enter the post update shortcode on will be able to use the Gravity Form. Not following your reasoning for not wanting to use that form

2015-06-29

Arnav Joy answers:

I think you should create two different forms . one for insertion of post and other to update the post.


GHostman comments:

form that updates the post is what i'm looking for, with the code above, it updates the post as I want but it create a new post (duplicate of the updated one), what i want is to prevent creating duplicated post if already exist

2015-06-29

nex1492 answers:

HI

You can use :

'wp_insert_post' function for both insert as well as update a post.

Please see link below.

https://codex.wordpress.org/Function_Reference/wp_insert_post

Hope this will work, if not then please let me know.

Thanks

2015-06-30

Bob answers:

<blockquote>with this function the form updates the existing post, but it create a duplicate post as well.</blockquote>
It should generally create revisions.

Can you check if you are getting proper id in <strong>$check_title->ID,</strong>.

$panel_id = wp_update_post(array(
'ID' => $check_title->ID,
));


in above code you are passing just id of post which should be edited, what are the other values? You want to only update post meta or other things are also there?


Bob comments:

How you are creating the post first time?

is there any other hook also which create a post?

It might be possible that there are two hook one is creating post and the above you created is updating it. so you might have duplicated post every time it's submitted.

so if there is already hook to create post you need to add condition.
Which work something like this

$get_title = $entry["1"];

$check_title = get_page_by_title($get_title, OBJECT, 'post' );
if (empty($check_title) ){
// here you need to use wp_insert_post()
}else{
// here you need to use wp_update_post()
}


GHostman comments:

the code <strong>$check_title->ID, </strong>is working properly since it update the post.

there is no other hook, only the a front end form

the code suggested is working but the form still create a duplicate post .


Bob comments:

can I see the form? can you provide url of website?

Have you tried plugins like this?
[[LINK href="https://wordpress.org/plugins/gravity-forms-post-updates/"]]https://wordpress.org/plugins/gravity-forms-post-updates/[[/LINK]]


GHostman comments:

actually i'm testing on localhost but here is a website that looks like what i want ,

1) here anyone can add their videos

http://www.dpstream.net/ajout-manga.html

2) then videos will be show like this

http://www.dpstream.net/manga-4175-baby-steps-2-episode-013-VOSTFR.html


the form only adds videos but if the post already exist it update it to add new vidoes of the same title


Bob comments:

How the posts are created? Are you using post fields create form?
http://gravitywiz.com/use-gravity-forms-to-create-user-submitted-posts/

If so then this post form will create new post every time and then the hook will be executed to update the post meta.

Instead of that you should use simple form elements not post related fields and then create or update post in gform_after_submission hook.

2015-06-30

Romel Apuya answers:

change

add_post_meta($panel_id, 'panel', $wppanel);
add_post_meta($panel_id, 'panel-type', $paneltype);


to

update_post_meta($panel_id, 'panel', $wppanel);
update_post_meta($panel_id, 'panel-type', $paneltype);


GHostman comments:

already tested