3
votes

I want to create a table with custom fields in wordpress. It looks someting like this:

+---------------------------------------------+
|  company  |          |          |           |
|           |  field1  |  field2  |   Field3  | 
|           |          |          |           | 
+----+-----------+----------+----------+------+
| 56        | 5        | 55       |  58       | 
+----+-----------+----------+-- -------+------+
| 888       | 6        | 88       |  55       | 
+----+-----------+----------+-- -------+------+
| 558       | 88       |  2       |  150      |
+----+-----------+----------+----------+------+

I need to create a filter to load a list of posts where:

"company" == 888
"field1" <= 100 &&
"field2" >= 20 &&
"field3" >= 40

For example in this case the post that has this values (like table up) will be mentioned in this list. There can be unlimited amount or rows in the table.

If tere were only one field with one value I would have done something like that:

$args = array(
    'numberposts' => -1,
    'post_type' => 'company',
    'meta_query' => array(
        array(
            'key' => $key_name,
            'value' => 0,
            'compare' => '>'
        )
    )
);

I can't find a way to query that data in the table this way? Is there? Would be very grateful for some help.

EDIT:

Her it the costume field I created. I made it as a repeater. One important thing: I need to find the company where all the conditions are met on the same line (row).

[
    {
        "key": "group_5dcbf625e861c",
        "title": "Product search params (copy)",
        "fields": [
            {
                "key": "field_5dcbf62629ce7",
                "label": "product search by param active",
                "name": "product_search_by_param_active",
                "type": "true_false",
                "instructions": "Please choose true to activate this option",
                "required": 1,
                "conditional_logic": 0,
                "wrapper": {
                    "width": "",
                    "class": "",
                    "id": ""
                },
                "message": "If not checked, search engine won't show this section on website",
                "default_value": 0,
                "ui": 0,
                "ui_on_text": "",
                "ui_off_text": ""
            },
            {
                "key": "field_5dcbf6262a106",
                "label": "product params values",
                "name": "product_params_values",
                "type": "repeater",
                "instructions": "",
                "required": 0,
                "conditional_logic": [
                    [
                        {
                            "field": "field_5dcbf62629ce7",
                            "operator": "==",
                            "value": "1"
                        }
                    ]
                ],
                "wrapper": {
                    "width": "",
                    "class": "",
                    "id": ""
                },
                "collapsed": "",
                "min": 0,
                "max": 0,
                "layout": "table",
                "button_label": "",
                "sub_fields": [
                    {
                        "key": "field_5dcbf626543aa",
                        "label": "param value 1",
                        "name": "param_value_1",
                        "type": "text",
                        "instructions": "",
                        "required": 0,
                        "conditional_logic": 0,
                        "wrapper": {
                            "width": "",
                            "class": "",
                            "id": ""
                        },
                        "default_value": "",
                        "placeholder": "",
                        "prepend": "",
                        "append": "",
                        "maxlength": ""
                    },
                    {
                        "key": "field_5dcbf6265478f",
                        "label": "param value 2",
                        "name": "param_value_2",
                        "type": "text",
                        "instructions": "",
                        "required": 0,
                        "conditional_logic": 0,
                        "wrapper": {
                            "width": "",
                            "class": "",
                            "id": ""
                        },
                        "default_value": "",
                        "placeholder": "",
                        "prepend": "",
                        "append": "",
                        "maxlength": ""
                    },
                    {
                        "key": "field_5dcbf62654c14",
                        "label": "param value 3",
                        "name": "param_value_3",
                        "type": "text",
                        "instructions": "",
                        "required": 0,
                        "conditional_logic": 0,
                        "wrapper": {
                            "width": "",
                            "class": "",
                            "id": ""
                        },
                        "default_value": "",
                        "placeholder": "",
                        "prepend": "",
                        "append": "",
                        "maxlength": ""
                    },
                    {
                        "key": "field_5dcbf62654fc1",
                        "label": "param value 4",
                        "name": "param_value_4",
                        "type": "text",
                        "instructions": "",
                        "required": 0,
                        "conditional_logic": 0,
                        "wrapper": {
                            "width": "",
                            "class": "",
                            "id": ""
                        },
                        "default_value": "",
                        "placeholder": "",
                        "prepend": "",
                        "append": "",
                        "maxlength": ""
                    },
                    {
                        "key": "field_5dcbf62655398",
                        "label": "param value 5",
                        "name": "param_value_5",
                        "type": "text",
                        "instructions": "",
                        "required": 0,
                        "conditional_logic": 0,
                        "wrapper": {
                            "width": "",
                            "class": "",
                            "id": ""
                        },
                        "default_value": "",
                        "placeholder": "",
                        "prepend": "",
                        "append": "",
                        "maxlength": ""
                    }
                ]
            }
        ],
        "location": [
            [
                {
                    "param": "post_type",
                    "operator": "==",
                    "value": "product"
                }
            ]
        ],
        "menu_order": 0,
        "position": "normal",
        "style": "default",
        "label_placement": "top",
        "instruction_placement": "label",
        "hide_on_screen": "",
        "active": 1,
        "description": ""
    }
]

Edit 2:

There is another way. To create table field. That is the field:

    {
        "key": "field_5dcdf5a673fa7",
        "label": "product search by param settings",
        "name": "product_search_by_param_settings",
        "type": "table",
        "instructions": "",
        "required": 0,
        "conditional_logic": 0,
        "wrapper": {
            "width": "",
            "class": "",
            "id": ""
        },
        "use_header": 0,
        "use_caption": 2
    }

And that is how it is saved:

a:5:{s:5:"acftf";a:1:{s:1:"v";s:5:"1.3.9";}s:1:"p";a:2:{s:1:"o";a:1:{s:2:"uh";i:0;}s:2:"ca";s:0:"";}s:1:"c";a:5:{i:0;a:1:{s:1:"p";s:0:"";}i:1;a:1:{s:1:"p";s:0:"";}i:2;a:1:{s:1:"p";s:0:"";}i:3;a:1:{s:1:"p";s:0:"";}i:4;a:1:{s:1:"p";s:0:"";}}s:1:"h";a:5:{i:0;a:1:{s:1:"c";s:0:"";}i:1;a:1:{s:1:"c";s:0:"";}i:2;a:1:{s:1:"c";s:0:"";}i:3;a:1:{s:1:"c";s:0:"";}i:4;a:1:{s:1:"c";s:0:"";}}s:1:"b";a:5:{i:0;a:5:{i:0;a:1:{s:1:"c";s:4:"ffsd";}i:1;a:1:{s:1:"c";s:5:"hjkhk";}i:2;a:1:{s:1:"c";s:4:"hkjh";}i:3;a:1:{s:1:"c";s:6:"hkjhkj";}i:4;a:1:{s:1:"c";s:4:"fdjk";}}i:1;a:5:{i:0;a:1:{s:1:"c";s:6:"hkjhjk";}i:1;a:1:{s:1:"c";s:5:"hkjhk";}i:2;a:1:{s:1:"c";s:6:"hkjhkj";}i:3;a:1:{s:1:"c";s:6:"hkjhkj";}i:4;a:1:{s:1:"c";s:4:"hkhk";}}i:2;a:5:{i:0;a:1:{s:1:"c";s:6:"hkhkjh";}i:1;a:1:{s:1:"c";s:5:"hkjhk";}i:2;a:1:{s:1:"c";s:5:"hkjhk";}i:3;a:1:{s:1:"c";s:6:"hkjhkj";}i:4;a:1:{s:1:"c";s:5:"hkjhk";}}i:3;a:5:{i:0;a:1:{s:1:"c";s:5:"hkjhk";}i:1;a:1:{s:1:"c";s:6:"hkjhkj";}i:2;a:1:{s:1:"c";s:6:"hkjhkj";}i:3;a:1:{s:1:"c";s:6:"hkjhkj";}i:4;a:1:{s:1:"c";s:5:"hkkhk";}}i:4;a:5:{i:0;a:1:{s:1:"c";s:4:"hkhk";}i:1;a:1:{s:1:"c";s:2:"hk";}i:2;a:1:{s:1:"c";s:4:"hkhk";}i:3;a:1:{s:1:"c";s:4:"hkhk";}i:4;a:1:{s:1:"c";s:4:"hkhk";}}}}
1
Include the export of the custom fields. - Sagar Bahadur Tamang
Did not built it yet. I am trying to make a POC before actually developing it. But I probably need to create a template anyway - so by tomorrow will add. - matisa
Ok great. We can help you better if you we know what sort of fields are those. - Sagar Bahadur Tamang
Why you are not use relation in the meta_query array? - Nand Lal
I have added the cf json. - matisa

1 Answers

3
votes

Meta query can take an array of conditions. In your case you can try:

$args = array(
  'numberposts' => -1,
  'post_type' => 'company',
  'meta_query' => array(
   'relation' => 'AND', // AND is default, you can use OR as well
    array(
      'key' => 'company',
      'value' => 888,
      'compare' => '='
    ),
    array(
      'key' => 'field1',
      'value' => 100,
      'compare' => '<=',
      'type'    => 'NUMERIC' // You can add a type here as well
    ), array(
      'key' => 'field2',
      'value' => 20,
      'compare' => '>=',
      'type'    => 'NUMERIC'
    ), array(
      'key' => 'field3',
      'value' => 40,
      'compare' => '>=',
      'type'    => 'NUMERIC'
    )
  )
);

Here is a link to assist understand meta_query in depth: https://rudrastyh.com/wordpress/meta_query.html

Also, here is a meta query generator to help with the process: https://generatewp.com/wp_meta_query/