• Hi, new to developing on WP. Let’s say I have 500 categories and 500 dynamic pages (woocommerce filters). Each of these 1k pages has a relationship to other one defined manually, to simplify things. Such as: category id 32 has relationship to: 10,20,/shop/?s=xxx,39, category 10 has relationship to 20,1 etc. What is best way to store these information? Currently I have own sql table and use weekly transient, I was thinking if better approach is to move to wp options table?

    My main worry here is performance

    • This topic was modified 3 months, 3 weeks ago by mcdeth.
Viewing 7 replies - 1 through 7 (of 7 total)
  • The best way is always to use the possibilities provided by WordPress first: post types and taxonomies. WordPress uses its own statements and retrievals for this, which are also cached. You don’t have this with in-house development using your own database tables and/or the options table.

    The options table should not be used for such large amounts of data, it is not even intended for this.

    I would therefore recommend creating your pages as post types (like the existing pages) and categories as taxonomies. How you can create individual taxonomies is described in the development manual here: https://developer.www.ads-software.com/reference/functions/register_taxonomy/ – alternatively, you can also use plugins that do the programming for you (e.g. ACF or CPT-UI).

    Thread Starter mcdeth

    (@mcdeth)

    Hi, thanks for prompt reply. The categories already exist and others are created dynamically, by search query or filter. All I need to do is store the relationships. What is the most efficient to store such piece of data?

    Array
    (
    [2522] => Array
    (
    [term_id] => Array
    (
    [0] => 412
    [1] => 309
    [2] => 2093
    )

    )

    Which relationships are you interested in exactly? The ones from a post type to a taxonomy? Or between taxonomies?

    Thread Starter mcdeth

    (@mcdeth)

    Let’s say this is the category structure in woocommerce shop:

    Bikes
    => Kids bikes
    => Adult bikes
    => Accessories
    ==> Chains

    Scooters
    => 2 wheel
    => 3 wheel
    => Accessories
    ==> Wheels

    Accessories
    => Helmets
    => Water botltes

    When a user is in any category, I display the subcategories of the current category in an unordered list ul. For example, in the ‘Bikes’ category, all subcategories related to bikes are listed in woocommerce before main content block. In the ‘Accessories’ category, only relevant subcategories like Helmets, Chains, etc., are shown.

    However, when a user is in the ‘Scooters’ or ‘Bikes’ category, Helmets aren’t displayed because they aren’t a subcategory. I want to add to the end of the list in this case Helmets.

    Why? Because creating helmets category for each sport doesn’t make sense. Lack of helmets on list makes it more difficult to navigate. Hence my idea to add this kind of relationship.

    Another example is user searching for black t-shirt. This is a dynamic category, it doesn’t exist theoretically (a search/filter page). When search query is “t-shirt” i want to show specific categories for better UI.

    Hope this is more clear now. I can do this easily with sql table (if category id =2 then get category id 3,4,5,5 permalink/name etc) and save it in transient but I guess it’s bit dirty approach. Thanks!

    I don’t think you should solve this yourself in individual SQL, but rather use ready-made functions from WordPress. What you are missing, however, is a marker on the category to which it is linked. There is no such thing in the WordPress standard and, as far as I know, not in WooCommerce either.

    I would therefore recommend that you first add such a marker. You could set this up as a selection field in the backend. In this way, you can editorially select in the backend for a category (an appointment) which other category (another term) it is still linked to. When saving, this is saved as term_meta at the term, i.e. integer or array – as you wish. Here is one of many instructions for this: https://fullstackdigital.io/blog/add-custom-metadata-to-taxonomy-terms-in-wordpress-without-plugins/ – here you only have to omit the part with the registration of your own taxonomy, as you are using WooCommerce categories.

    In the frontend, you then have to read out this term_meta value with individual programming. If this is set and depending on the value, you can then output the other category to be displayed. Overall, this is quite performant thanks to the taxonomies in WordPress. It is not necessary to add anything to the database yourself.

    Thread Starter mcdeth

    (@mcdeth)

    Thanks. But it won’t work for dynamic categories (created when searching or filtering). By dynamic categories I mean when you create T-Shit, set attribute color black, red, etc.. User chooses black. It is dynamic category which doesn’t exist in taxonomy. Hence I can’t add any metadata. I had it hardcoded in function before, but I want to make it more flexible. Hence idea with wpoptions. here is example of blog post that is showing related woocommerce categories:

    	if (!is_woocommerce()) {
    global $post;

    $categories = get_the_category($post->ID);
    $tags = get_the_tags($post->ID);

    // Default term_id
    $term_id = "67"; // default woocommerce category


    if ($tags) {
    foreach ($tags as $tag) {
    if ($tag->term_id == 2579) { // skip this tag
    return;
    } elseif ($tag->term_id == 1529) { //match this tag with sport category
    $term_id = "757";
    $current_category_name="More sport stuff in shop";
    break;

    Googling how to store data in wordpress, some people recommend sql, wp options or custom post type.

    Thread Starter mcdeth

    (@mcdeth)

    Maybe I overcomplicated it. My question is, best way to store piece of data so I can show it on the website. The page is often dynamic, for example when user searches “sxasxasxasxa” I will show him “aaa”, etc. Can hardcode it in function (if searchquery=”sxasxasxasxa” then return “aaa”) etc but I want to write a plugin eventually to store it somewhere in database and later other admins to modify it without looking into php file.

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