Title: Create ad programatically
Last modified: April 8, 2022

---

# Create ad programatically

 *  Resolved [hir88en](https://wordpress.org/support/users/hir88en/)
 * (@hir88en)
 * [4 years, 2 months ago](https://wordpress.org/support/topic/create-ad-programatically/)
 * Hi, I’m curios is there any function/class to create advert programatically?
 * For example:
 *     ```
       $args = array(
               "title" => 'Test Ad',
               "category" => 'Car > Sports car > Ferrari, Misc > Rare items',
               "gallery" => 'https://example.com/image1.jpg, https://example.com/image2.jpg',
               "contact_person" => 'Ricky',
               "email" => 'ricky@gmail.com',
               "phone_number" => '809779879798',
               ...
       );
       wpa_add_advert( $args );
       ```
   

Viewing 3 replies - 1 through 3 (of 3 total)

 *  Plugin Author [Greg Winiarski](https://wordpress.org/support/users/gwin/)
 * (@gwin)
 * [4 years, 2 months ago](https://wordpress.org/support/topic/create-ad-programatically/#post-15546162)
 * Hi,
    the Ads are WordPress custom post types so it is the easiest to create the
   ads using the WP native wp_insert_post() and add_post_meta() functions.
 * One other way to do that would be to use the Adverts_Post::save() method, how
   to use it you can see in the wpadverts/includes/class-shortcode-adverts-add.php
   file on lines about 374 to 400.
 * The code does not show how to add images to the gallery, but the images in the
   gallery are WP post attachments so once you have the Ad saved you can add attachments
   to it using the wp_insert_attachment() function.
 * Hope this helps.
 *  Thread Starter [hir88en](https://wordpress.org/support/users/hir88en/)
 * (@hir88en)
 * [4 years, 1 month ago](https://wordpress.org/support/topic/create-ad-programatically/#post-15551462)
 * Hi, Thank you for clarification.
 * But I still not understand something.
 * You said for gallery it use native WP post attachments.
 * Do we need to add any meta to this attachment?
 * Because when I see this part in your plugin:
    Adverts_Gallery_Helper->load_attachments()
 * It seems to only load attachment if it contains certain meta in it.
 * Will it work if we just use importer plugin? (they will not add this meta to 
   attachment)
 * If it wont work, What all the meta needs to exist in the image attachment?
 *  Plugin Author [Greg Winiarski](https://wordpress.org/support/users/gwin/)
 * (@gwin)
 * [4 years, 1 month ago](https://wordpress.org/support/topic/create-ad-programatically/#post-15553995)
 * Hi,
    yes, each attachment should have 2 meta fields that connect it to a correct
   field in the [adverts_add] form, if you have attachments in the gallery field
   only then it should be just
 *     ```
       update_post_meta( $attach_id, "wpadverts_form", "advert" );
       update_post_meta( $attach_id, "wpadverts_form_field", "gallery" );
       ```
   

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Create ad programatically’ is closed to new replies.

 * ![](https://ps.w.org/wpadverts/assets/icon-256x256.png?rev=2423472)
 * [WPAdverts - Classifieds Plugin](https://wordpress.org/plugins/wpadverts/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/wpadverts/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/wpadverts/)
 * [Active Topics](https://wordpress.org/support/plugin/wpadverts/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/wpadverts/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/wpadverts/reviews/)

 * 3 replies
 * 2 participants
 * Last reply from: [Greg Winiarski](https://wordpress.org/support/users/gwin/)
 * Last activity: [4 years, 1 month ago](https://wordpress.org/support/topic/create-ad-programatically/#post-15553995)
 * Status: resolved