Creating a listing using the WP REST API

Hello!

I need to create a listing using a third-party application written in PHP.

I started testing by using Postman, sending a request to my site like this:
https://site.com/wp-json/wp/v2/hp_listing?title=Title&content=Content&status=publish

And it works! The listing is indeed created, but my task is to assign a listing category, as well as custom attributes and images.

How can I create a listing with an assigned category, custom attributes and images using the WP REST API? :thinking:

Hi,

Please check these topics:

ā€‹I hope this is helpful to you.

Thanks for the reply!

Iā€™ve been looking at these topics. Iā€™ve already gone through this step, my listings are published with a title and content.

I need to add images and custom attributes via WP REST API.

Iā€™m a developer, but I donā€™t quite understand how to do this. Please help me figure this out.

Hi,

If you use the default WP REST API, then you can try providing fields like they are implemented on the WordPress level. For example, listing is a custom post type ā€œhp_listingā€, and attributes (custom fields) are stored as meta fields with ā€œhp_ā€ prefix.

For example, if you added an attribute with ā€œcustom_fieldā€ name, then it can be saved as a post meta field ā€œhp_custom_fieldā€. For selectable attributes (of Select, Checkboxes, Radio Buttons types), values are stored as custom taxonomy terms, the taxonomy name would be ā€œhp_listing_custom_fieldā€ in this case.

If youā€™re familiar with coding, I would recommend creating a custom REST API endpoint instead register_rest_route() ā€“ Function | Developer.WordPress.org Then you can use the HivePress way of creating listings without the need to know how the fields are implemented:

$listing = ( new \HivePress\Models\Listing() )->fill(
	[
		'title'        => 'test',
		'description'  => 'test',
		'categories'   => [ 123 ],
		'custom_field' => 123,
	]
)->save();

Hope this helps

Thanks for the answer!

It works, thanks for the help.

I have one more question: how can I attach images to the listing correctly?

I try to do it by updating the meta field, but in this case the image does not appear on the listing page:

update_post_meta( $post->ID, ā€˜hp_imagesā€™, $image_id );

Hi,

Please try saving as an array, for example: [$image_id]. As an alternative, try disabling the cache:

if ( ! defined( 'HP_CACHE' ) ) { 
    define( 'HP_CACHE', false );
}
1 Like

Thanks for the reply!

I disabled HivePress cache using your code.

I also changed the image ID value to an array of image IDs, the gallery works without changes:

update_post_meta($post->ID, 'hp_images', $image_ids);

Iā€™ll try to explain a little more. I see the changes on the listing edit page. But when I go to the listing page as a user, some (this is important!) images are not listed.

I tested several media (videos and images) of different formats, such as png and jpeg/jpg, as well as media uploaded by different users on the site, but I could not find a pattern. Some media files are presented correctly, and some are not.

I tried another option for uploaded media posts by the author, but it did not help to solve the problem:

foreach ($image_ids as $image_id) {
wp_update_post([
'ID' => $image_id,
'post_parent' => $post->ID,
]);
}

Perhaps there is some other cache for images or something like that? Perhaps I forget some other action when applying an image to the listing?..

I use a clean version of WP, only HivePress + HivePress Favorites plugins are installed. All changes are made in funtions.php of the child theme.

I want to offer HivePress to clients, but I can not do this until I understand how to work with such important things as changing media via code.

Update: I think Iā€™m close to a solution.

After adding images to the listing via code, they donā€™t show up on the listing page.

But when I go to the listing edit page, change the order of the images in the gallery, and then save the changes, everything works!

Apparently, some hook is triggered when the listing is updated :thinking:

Please make sure that you used the code snippet to disable HivePress chache. If so, please also check if attachments you use have 2 meta fields:

hp_model set to ā€œlistingā€
hp_field set to ā€œimagesā€

Also, please set the listing ID in comment_count and post_parent fields when creating an attachment (this is needed for a few secondary functions).

Hope this helps

Thanks for the reply!

Yes, that helped, thank you very much :handshake:

It seems a bit confusing to understand, maybe we should include it in the documentation? Just a suggestion to improve the plugin experience :sweat_smile:

1 Like

Thanks, weā€™ll try to improve the developer docs

1 Like