0

good day

I found a code that works, but it creates several posts of the same name, do not want that, he wanted to create only once post_type if the value is 'x', but he is creating a 'x' every time updates , how do I create it only once this post_type?

My Code:

<?php 
    wp_insert_post( array(
        'post_status' => 'publish',
        'post_type' => 'fornecedores', // post_type which is created posting
        'post_title' => get_field('fornecedor'), // Custom field to generate the post type
        'post_content' => ''
    )
)
?>


Code is in a loop of the post type products.
Understand me?

4
  • 2
    Your question is not clear. Please make it a little broad with good english or with the code you tried. Edit the question to make it clear. :) Sep 6, 2013 at 17:50
  • see if you can understand now, please
    – Raank
    Sep 6, 2013 at 18:13
  • Hey friend, I updated my question, confers Now!
    – Raank
    Sep 7, 2013 at 5:26
  • You say you are creating a post every time it updates, why not post that code? That is the clearest way to get help. I don't fully understand where you running wp_insert_post from...
    – Jake
    Sep 7, 2013 at 20:26

2 Answers 2

1

Before inserting check if post exists:

$exists = get_page_by_title( get_field('fornecedor'), OBJECT, 'fornecedores');

if ( empty( $exists ) ) {
  wp_insert_post( array(
    'post_status' => 'publish',
    'post_type' => 'fornecedores', // post_type which is created posting
    'post_title' => get_field('fornecedor'), // Custom field to generate the post type
    'post_content' => ''
  )
}
3
  • Hey friend, is how you tell me how to get data from a custom field images for Inserit in post_thumbnail? Take the field get_field ('image_of_supplier') ?
    – Raank
    Sep 8, 2013 at 4:05
  • @Feera it's hard explain here in comments, check for solotion in already answered questions, if you don't find anything ask another question, please
    – gmazzap
    Sep 8, 2013 at 12:31
  • Ok, sorry friend, i will make a new question, thank you
    – Raank
    Sep 8, 2013 at 22:29
0
  1. Create a post_type named "Products"
  2. Create a post_type named "Suppliers"
  3. In "Custom Fields", create a new field group and add a "rule" so that it shows up on "Products"
  4. Then add a new field and select the "Relationship" field type
  5. Under "Post Type" for that field, choose "Suppliers"

Now on your Products posts, you can choose Suppliers.

Is... that what you are asking?

2
  • not post_types and custom fields have been created, want to get data from a custom field to create a post in post type 'Suppliers'.
    – Raank
    Sep 7, 2013 at 4:55
  • Hey friend, I updated my question, confers Now!
    – Raank
    Sep 7, 2013 at 5:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.