Support » Developing with WordPress » Creating Meta Box content from a Custom Post Type

  • Resolved foxymcfox

    (@foxymcfox)


    I’m working on a personal site that tries to provide price comparisons for various local stores that carry a number of products.

    I’ve already created two different post types:

    1. Products
    2. Providers

    Products include the name of the product, a short name for the product, a unique SKU for each product, and a tag (Durable goods, perishable, etc).

    Providers is a list of stores.

    I need to create a metabox on the Provider admin page that populates a list of all products, grouped by tag, with a text box to enter pricing.

    So below if I go to edit a provider named Joe’s Hardware, I would like the following to appear:

    Durable Goods
    Oven [PRICE] (Using this to represent a text box)
    Refrigerator [PRICE]
    Dishwasher [PRICE]

    Perishable Goods
    Lilly of the Valley [PRICE]
    Carnations [PRICE]
    etc…

    The idea being that if and when I add a new product, ALL provider pages also get new fields to match.

    Would I be able to echo out an array of the Product custom post types and also echo them as part of a variable/field name on the Provider page somehow?

    Thanks in advance!

Viewing 6 replies - 1 through 6 (of 6 total)
  • is there a reason you’re not considering woocommerce for the products?

    as for the providers post type take a look at this documentation. https://wordpress.org/support/article/post-types/

    finally, for the custom meta box part check this out. https://developer.wordpress.org/plugins/metadata/custom-meta-boxes/

    hope something I’ve provided helps you get on the correct track. once you find a solution please tag the thread resolved.

    Davood, thanks for your response, but I’ve already created the post types and have created other metaboxes on the pages. I’m specifically looking for how to call/reference the posts of one Custom Post Type in the Meta Box of another which your links don’t cover.

    As for why I’m not using WooCommerce? It’s a bit overkill for what I need, and there are a number of small customizations I need for this project that WC doesn’t have.

    • This reply was modified 3 months, 2 weeks ago by  foxymcfox.
    Moderator bcworkz

    (@bcworkz)

    You can query for all of the current provider’s products within you meta box form callback. The current provider post object is passed to your callback. Since you want products ordered by taxonomy terms, you’ll need to build a custom SQL query and execute it with $wpdb methods. WP_Query will not order by taxonomy terms. Presumably the products have a meta value or something which relates them to the proper provider that you can use as query criteria.

    Loop through the query results and output appropriate labels and input form elements. Consider carefully how you name each input element, it needs to somehow relate back to the product it’s for because all PHP gets for data is the field name and its value. The contextual labels in the meta box remain client side.

    Wow! Thanks, @bcworkz!

    That should give me a huge push in the right direction. I really appreciate it.

    This is why I love WP.

    Glad BCWorkz was able to help. Sorry I misunderstood what you were asking at first. Happy Blogging!

    Moderator bcworkz

    (@bcworkz)

    You’re welcome. Happy coding!

Viewing 6 replies - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.