• Resolved hir88en

    (@hir88en)


    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" => '[email protected]',
            "phone_number" => '809779879798',
            ...
    );
    wpa_add_advert( $args );
Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Greg Winiarski

    (@gwin)

    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

    (@hir88en)

    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

    (@gwin)

    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.