If you’ve hardcoded product data into a PHP file and later needed it to be editable from the admin panel without breaking your existing database logic — here’s exactly how I handled that.
The Problem
We had a static array inside a PHP class:
$merch = TCA_Pos::MERCH;
Which populated a merch section like this:
<div class="merch-product" data-item-id="<?= $item['id'] ?>">
<span class="name"><?= $item['name'] ?></span>
...
</div>
This array was also linked (by ID) to entries in a custom table: wp_pos_order_merch
.
Updating a product required editing PHP and coordinating updates in the database. That had to change.
The Goal
- Move product data into WordPress Custom Post Type (
merch
) - Use ACF for
cost
andoptions
fields - Allow admin users to manage everything from the backend
- Keep existing database entries consistent by syncing old
item_id
values
Step-by-Step: What I Did
1. Created the CPT
register_post_type('merch', [
'label' => 'Merch',
'public' => false,
'show_ui' => true,
'show_in_menu' => true,
'supports' => ['title', 'editor', 'thumbnail'],
'capability_type' => 'post',
'map_meta_cap' => true,
'menu_icon' => 'dashicons-cart',
]);
2. Created ACF Fields
cost
(number)options
(repeater of text values)
3. Wrote an Importer Script
This CLI script:
- Deletes all old
merch
posts - Re-creates them from the static array
- Captures a mapping between the old static
id
and the newpost_id
- Updates
wp_pos_order_merch
accordingly
$wp_ref_ID_array[] = ['db_id' => $item['id'], 'new_id' => $post_id];
foreach ($wp_ref_ID_array as $ref) {
$wpdb->update('wp_pos_order_merch', ['item_id' => $ref['new_id']], ['item_id' => $ref['db_id']]);
}
4. Replaced Front-End Static Array
Now, instead of loading TCA_Pos::MERCH
, I fetch merch
posts dynamically:
$posts = get_posts(['post_type' => 'merch', 'numberposts' => -1]);
foreach ($posts as $post) {
$merch[] = [
'id' => $post->ID,
'name' => $post->post_title,
'cost' => get_field('cost', $post->ID),
'options' => array_column(get_field('options', $post->ID) ?: [], 'option'),
];
}
Why This Works
- Data is now fully managed via WordPress admin
- Existing
item_id
relationships remain valid - It’s scalable: new merch, price changes, sizes — no more code edits
- It’s safe: your legacy table remains usable
What Brought You Here
If you’re searching for:
- “How to migrate static PHP array to WordPress CPT”
- “Sync CPT with custom database table”
- “Use ACF to replace static data”
- “WordPress CLI content importer with ID remapping”
…this solution gives you a clean, flexible, future-proof way to get it done.
If you need to update your current site or modernise how your data is managed get in touch, I’d be happy to help.
Leave a Reply