• Resolved IoClaudio

    (@ioclaudio)


    Hi,
    I’d like to add to a custom post some tabular data.
    The custom post represents a sport event with title, description and other data.
    I have to add to this content one or more rows containing these columns:
    time | place | competition | link

    So I have a one to many relation: post–>rows.

    I will add the events form the WP backend but I will show them using a REST API, so I can’t use the table of the editor. These rows should be coded so that the web service can return them.

    Should I implement a new content-type or is there an easier way to do this?
    I don’t think that the custom fields are usable in this context.

    Thank you for your suggestions

    cld

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    It’s unclear to me why a single event would need more than one row per event. Even if it were a tournament, each game would be a separate event with its own time, place, etc. Multiple events could be tied together when necessary with taxonomy terms.

    In any case, you can store arrays of data in meta data, or even in some arbitrary format such as CSV or JSON, so storing multiple rows isn’t an issue. What would be an issues querying for such data. Say you want to query for events in a certain date range. This can be done easily for dates the post was published, but to query for dates in meta data that were saved as an array is problematic.

    You’ll at least need to store some data under its own meta key so it is more easily queried. It’s still possible to store multiple values of one event in one column under a single meta key. But if there will be frequent queries utilizing a number of meta data fields, you may want to consider storing the data in a custom table. Complex meta data queries are not very efficient.

    Thread Starter IoClaudio

    (@ioclaudio)

    @bcworkz thank you for your answer.
    For example, the event could be “National fencing tournament 2020”.
    This event will take place from the 2 March to the 15 March 2020, the rows of this event are:
    2/3/2020 | Male sword |…
    3/3/2020 | Female sword | …
    4/3/2020 | Female saber |…

    Each of them isn’t an event, you can call it subevent, but almost all the properties of these subevents are shared with each other.
    Yes, I could use a JSON in a meta-tag to store the data, it is a good idea.
    It will be easy to retrieve this data with the related event and show them with the REST API.

    The problem now is how to let the operator add this data from the back-end (WordPress).
    Which direction should I take to modify the content-type?

    cld

    Moderator bcworkz

    (@bcworkz)

    You can add a custom meta box to the existing edit screen. Depending on what’s already there and what hooks might be available, you could potentially add input fields to an existing meta box. Alternately, use one of the custom fields plugins to add input fields.

    You would then add a callback function to one of the save post or post transition actions to collect the field data and aggregate it into an array or JSON, which is then saved under an appropriate meta key. PHP arrays get serialized when saved to post meta, which is somewhat similar to JSON. Unless the code utilizing this data needs JSON, it’s typically easier to just save the PHP array.

    Thread Starter IoClaudio

    (@ioclaudio)

    Hi,
    at the end I used this plugin that does what I need: https://it.wordpress.org/plugins/advanced-custom-fields-table-field/

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘How to associate tabular data to a custom post?’ is closed to new replies.