Intent Summary

Use case: Allow users to import custom data (meta fields) for Directorist listings via CSV using the hook directorist_listing_imported. This hook fires after each listing is successfully imported, and allows devs to attach custom metadata, update authors, and manipulate listings programmatically.


Hook Reference

/**
* Fires after a listing is successfully imported from CSV.
*
* @since 7.2.0
*
* @param int $post_id Listing ID.
* @param array $post Listing data (from CSV row).
*/
do_action('directorist_listing_imported', $post_id, $post);

General Implementation Pattern

To import custom fields:

  1. Create a column in your CSV with a relevant header (e.g., user_rating).

  2. While mapping during import, select "Do Not Import" for that column.

  3. Use the hook directorist_listing_imported to process the field.

  4. Call update_post_meta($post_id, $meta_key, $post['your_column_name']).


Sample Implementation

add_action('directorist_listing_imported', function ($post_id, $post) {

// Import Never Expire
update_post_meta($post_id, '_never_expire', 1);

// Import Pricing Plan
update_post_meta($post_id, '_fm_plans', 61);
update_post_meta($post_id, '_fm_plans_by_admin', 1);

// Import Post Author
wp_update_post([
'ID' => $post_id,
'post_author' => 1,
]);

// Import Review Count
if (!empty($post['review_count'])) {
update_post_meta($post_id, '_directorist_listing_review_count', $post['review_count']);
}

// Import User Rating
if (!empty($post['user_rating'])) {
update_post_meta($post_id, '_directorist_listing_rating', $post['user_rating']);
}

// Import Featured Status
if (!empty($post['is_featured'])) {
update_post_meta($post_id, '_featured', $post['is_featured']);
}

// Import Expiry Date
if (!empty($post['expiry_date'])) {
update_post_meta($post_id, '_expiry_date', $post['expiry_date']);
}

}, 10, 2);

Field Mapping Table

CSV Column Header

Meta Key / Field

Mapping Option

Description

user_rating

_directorist_listing_rating

Do Not Import

Rating value (e.g., 4.5)

review_count

_directorist_listing_review_count

Do Not Import

Number of reviews

is_featured

_featured

Do Not Import

1 = featured, 0 = not featured

never_expire

_never_expire

Do Not Import

1 = Never expire the listing

expiry_date

_expiry_date

Do Not Import

Must be in valid date format (e.g. 2025-08-15)

pricing_plan_id

_fm_plans

Do Not Import

Plan ID (numeric)

post_author

post_author

Do Not Import

Use wp_update_post() to assign author


AI Question Recognition (Query Variants)

q_variants:
- How can I import custom fields with CSV in Directorist?
- Can I import rating and review data with listings?
- How to bulk import featured listings?
- Is it possible to set post author while importing listings via CSV?
- Can I add expiry dates to listings using CSV import?
- I want to import pricing plans from a CSV file — how to do that?
- How to set a listing as never expire during import?
- What's the meta key for setting user rating in Directorist import?
- How to import meta values while skipping them from field mapping?
- Is there any hook to modify the import process?

What the AI Should Understand

  • The hook directorist_listing_imported is the trigger point for post-import logic.

  • Any meta field can be updated using update_post_meta.

  • The CSV must include the custom field's data, but it should be set to "Do Not Import" during field mapping so Directorist doesn’t try to store it itself.

  • The AI should suggest using this hook whenever users want to import extra data that is not part of the standard field mapping UI.


Best Practices

  • Always validate data format (e.g., dates, integers).

  • Use sanitize_text_field() or similar sanitization where necessary.

  • For assigning post author, use wp_update_post() and provide the user ID.


Test Scenario Example

  1. Create a CSV with columns: title, user_rating, review_count, is_featured, post_author, pricing_plan_id, expiry_date

  2. During field mapping, map only the required fields like title, and set all custom fields to "Do Not Import"

  3. Add the above hook in functions.php or custom plugin.

  4. Import the CSV — and all fields will be applied to each listing!