i am searching from a whole day long for a easy way to create a dynamic wordpress nested navbar.
now i am looking for a function if there is to get all pages list as a nested object
i am using get_pages() which is giving me flat array with parent_page_id
[
{
"ID": 2,
"post_author": "1",
"post_date": "2021-10-10 05:21:26",
"post_date_gmt": "2021-10-10 05:21:26",
"post_content": "<!-- wp:paragraph -->\n<p>This is an example page. It's different from a blog post because it will stay in one place and will show up in your site navigation (in most themes). Most people start with an About page that introduces them to potential site visitors. It might say something like this:</p>\n<!-- /wp:paragraph -->\n\n<!-- wp:quote -->\n<blockquote class=\"wp-block-quote\"><p>Hi there! I'm a bike messenger by day, aspiring actor by night, and this is my website. I live in Los Angeles, have a great dog named Jack, and I like piña coladas. (And gettin' caught in the rain.)</p></blockquote>\n<!-- /wp:quote -->\n\n<!-- wp:paragraph -->\n<p>...or something like this:</p>\n<!-- /wp:paragraph -->\n\n<!-- wp:quote -->\n<blockquote class=\"wp-block-quote\"><p>The XYZ Doohickey Company was founded in 1971, and has been providing quality doohickeys to the public ever since. Located in Gotham City, XYZ employs over 2,000 people and does all kinds of awesome things for the Gotham community.</p></blockquote>\n<!-- /wp:quote -->\n\n<!-- wp:paragraph -->\n<p>As a new WordPress user, you should go to your dashboard to delete this page and create new pages for your content. Have fun!</p>\n<!-- /wp:paragraph -->",
"post_title": "Sample Page",
"post_excerpt": "",
"post_status": "publish",
"comment_status": "closed",
"ping_status": "closed",
"post_password": "",
"post_name": "sample-page",
"to_ping": "",
"pinged": "",
"post_modified": "2021-10-10 19:14:00",
"post_modified_gmt": "2021-10-10 19:14:00",
"post_content_filtered": "",
"post_parent": 0,
"guid": "http://localhost:8000/?page_id=2",
"menu_order": 2,
"post_type": "page",
"post_mime_type": "",
"comment_count": "0",
"filter": "raw"
},
{
"ID": 28,
"post_author": "1",
"post_date": "2021-10-10 19:11:08",
"post_date_gmt": "2021-10-10 19:11:08",
"post_content": "<!-- wp:paragraph -->\n<p></p>\n<!-- /wp:paragraph -->\n\n<!-- wp:columns -->\n<div class=\"wp-block-columns\"><!-- wp:column {\"width\":\"100%\"} -->\n<div class=\"wp-block-column\" style=\"flex-basis:100%\"><!-- wp:query {\"queryId\":21,\"query\":{\"perPage\":\"100\",\"pages\":0,\"offset\":0,\"postType\":\"post\",\"categoryIds\":[4,3,1],\"tagIds\":[],\"order\":\"desc\",\"orderBy\":\"date\",\"search\":\"\",\"exclude\":[],\"sticky\":\"exclude\",\"inherit\":false},\"displayLayout\":{\"type\":\"flex\",\"columns\":4}} -->\n<div class=\"wp-block-query\"><!-- wp:post-template -->\n<!-- wp:post-featured-image /-->\n\n<!-- wp:post-title {\"isLink\":true,\"textColor\":\"vivid-cyan-blue\",\"fontSize\":\"medium\"} /-->\n\n<!-- wp:post-date {\"fontSize\":\"small\"} /-->\n<!-- /wp:post-template --></div>\n<!-- /wp:query --></div>\n<!-- /wp:column --></div>\n<!-- /wp:columns -->",
"post_title": "Products",
"post_excerpt": "",
"post_status": "publish",
"comment_status": "closed",
"ping_status": "closed",
"post_password": "",
"post_name": "products",
"to_ping": "",
"pinged": "",
"post_modified": "2021-10-11 22:01:39",
"post_modified_gmt": "2021-10-11 22:01:39",
"post_content_filtered": "",
"post_parent": 0,
"guid": "http://localhost:8000/?page_id=28",
"menu_order": 1,
"post_type": "page",
"post_mime_type": "",
"comment_count": "0",
"filter": "raw"
},
{
"ID": 156,
"post_author": "1",
"post_date": "2021-10-11 23:25:00",
"post_date_gmt": "2021-10-11 23:25:00",
"post_content": "",
"post_title": "dresses",
"post_excerpt": "",
"post_status": "publish",
"comment_status": "closed",
"ping_status": "closed",
"post_password": "",
"post_name": "dresses",
"to_ping": "",
"pinged": "",
"post_modified": "2021-10-11 23:25:00",
"post_modified_gmt": "2021-10-11 23:25:00",
"post_content_filtered": "",
"post_parent": 28,
"guid": "http://localhost:8000/?page_id=156",
"menu_order": 0,
"post_type": "page",
"post_mime_type": "",
"comment_count": "0",
"filter": "raw"
},
{
"ID": 158,
"post_author": "1",
"post_date": "2021-10-11 23:45:04",
"post_date_gmt": "2021-10-11 23:45:04",
"post_content": "",
"post_title": "2 level child",
"post_excerpt": "",
"post_status": "publish",
"comment_status": "closed",
"ping_status": "closed",
"post_password": "",
"post_name": "2-level-child",
"to_ping": "",
"pinged": "",
"post_modified": "2021-10-12 00:03:19",
"post_modified_gmt": "2021-10-12 00:03:19",
"post_content_filtered": "",
"post_parent": 156,
"guid": "http://localhost:8000/?page_id=158",
"menu_order": 0,
"post_type": "page",
"post_mime_type": "",
"comment_count": "0",
"filter": "raw"
},
{
"ID": 5,
"post_author": "1",
"post_date": "2021-10-10 06:48:19",
"post_date_gmt": "2021-10-10 06:48:19",
"post_content": "[contact-form-7 id=\"16\" title=\"Contact Us\"]",
"post_title": "Contact Us",
"post_excerpt": "",
"post_status": "publish",
"comment_status": "closed",
"ping_status": "closed",
"post_password": "",
"post_name": "contact-us",
"to_ping": "",
"pinged": "",
"post_modified": "2021-10-10 19:14:09",
"post_modified_gmt": "2021-10-10 19:14:09",
"post_content_filtered": "",
"post_parent": 0,
"guid": "http://localhost:8000/?page_id=5",
"menu_order": 3,
"post_type": "page",
"post_mime_type": "",
"comment_count": "0",
"filter": "raw"
}
]
now what i want is a nested object so i can make recursive function to iterate and generate fully custom navbar
is there any way to achieve such nested object ? its a easy question for an experienced wp developer i am new so i dont know much about available functions though i have researched quit well
You could use the built-in interface and function
when using menus... first register a location/several location
/*** REGISTER MENU ***/
register_nav_menus(array(
'topmenu' => __( 'Top Menu', THEME_NAME ),
'topmenumobile' => __( 'Top Mobile Menu', THEME_NAME ),
'footermenu' => __( 'Footer Menu', THEME_NAME ),
'footermenumob' => __( 'Footer Mobile Menu', THEME_NAME ),
));
Then build your menu in the backend:
Now you can output it the frontend using wp_nav_menu
for example:
<?php
wp_nav_menu( array(
'theme_location' => (wp_is_mobile() ? 'topmenumobile' : 'topmenu'),
'depth' => 2,
'container' => 'div',
'container_class' => 'collapse navbar-collapse',
'container_id' => 'topmenu',
'menu_class' => 'nav navbar-nav',
) );
?>
On the resulting HTML, you can apply CSS and JS or even use a walker library if your working with bootstrap (for example).
Hope this helps, I am not sure I completely understand your needs so I here for edits and additional information.
Related
The problem is that I get this excerpt of a product from Woocommerce:
[
{
"id": 73,
"name": "cBox",
"slug": "cbox",
"sku": "",
"price": "169",
"regular_price": "169",
"sale_price": "",
"on_sale": false,
"purchasable": true,
"total_sales": 0,
"virtual": false,
"downloadable": false,
"downloads": [],
"download_limit": -1,
"download_expiry": -1,
"external_url": "",
"button_text": "",
"tax_status": "taxable",
"tax_class": "",
"manage_stock": false,
"stock_quantity": null,
"backorders": "no",
"backorders_allowed": false,
"backordered": false,
"low_stock_amount": null,
"sold_individually": false,
"shipping_required": true,
"shipping_taxable": true,
"shipping_class": "",
"shipping_class_id": 0,
"reviews_allowed": true,
"average_rating": "0.00",
"rating_count": 0,
"upsell_ids": [],
"cross_sell_ids": [],
"parent_id": 0,
"purchase_note": "",
"attributes": [],
"default_attributes": [],
"variations": [],
"grouped_products": [],
"menu_order": 0,
"stock_status": "instock",
"has_options": true,
"translations": {
"de": "73",
"en": "76"
},
"lang": "de",
}
]
You can see the translations, at least the ID's of the translated products. My question is how can I get directly via rest-api only the translated products?
Because with the property "lang=en" it does not work in the url
What does your request look like?
It should work with the lang parameter, you can read more here: https://wpml.org/documentation/related-projects/woocommerce-multilingual/using-wordpress-rest-api-woocommerce-multilingual/
I'm trying to add latest posts to my footer menu, so I used wp_get_nav_menu_items filter.
I'm going to skip the filter itself, just in the end of the function I get an array(2) of posts. I've added all the additional data to them (like menu_parent_id etc.). The problem is final menu contains only the last post. Even if I set $items[0] = $items[1]; so that both items are identical. It does not depend on a particular menu or post amount - always the last item only. I'll leave the code in quickly-reproducible format: just a filter and JSON-encoded items:
add_filter('wp_get_nav_menu_items', function ($items, $menu, $args) {
return json_decode('[{
"ID": 2576,
"post_author": "3",
"post_date": "2020-05-11 23:46:15",
"post_date_gmt": "2020-05-11 23:46:15",
"post_content": "",
"post_title": "Testr",
"post_excerpt": "",
"post_status": "publish",
"comment_status": "open",
"ping_status": "open",
"post_password": "",
"post_name": "testr",
"to_ping": "",
"pinged": "",
"post_modified": "2020-05-11 23:46:15",
"post_modified_gmt": "2020-05-11 23:46:15",
"post_content_filtered": "",
"post_parent": 0,
"guid": "http:\/\/mysite.local\/?p=2576",
"menu_order": 0,
"post_type": "post",
"post_mime_type": "",
"comment_count": "0",
"filter": "raw",
"db_id": 2576,
"object_id": "",
"menu_item_parent": "0",
"url": "http:\/\/mysite.local\/?p=2576",
"title": "Testr",
"target": "",
"classes": [],
"type": "",
"object": "",
"attr_title": "",
"description": "",
"xfn": "",
"status": ""
}, {
"ID": 3439,
"post_author": "3",
"post_date": "2020-05-11 23:28:49",
"post_date_gmt": "2020-05-11 23:28:49",
"post_content": "",
"post_title": "Test",
"post_excerpt": "",
"post_status": "publish",
"comment_status": "open",
"ping_status": "open",
"post_password": "",
"post_name": "test-2",
"to_ping": "",
"pinged": "",
"post_modified": "2020-05-11 23:28:49",
"post_modified_gmt": "2020-05-11 23:28:49",
"post_content_filtered": "",
"post_parent": 0,
"guid": "http:\/\/mysite.local\/?p=3439",
"menu_order": 0,
"post_type": "post",
"post_mime_type": "",
"comment_count": "0",
"filter": "raw",
"db_id": 3439,
"object_id": "",
"menu_item_parent": "0",
"url": "http:\/\/mysite.local\/?p=3439",
"title": "Test",
"target": "",
"classes": [],
"type": "",
"object": "",
"attr_title": "",
"description": "",
"xfn": "",
"status": ""
}]');
});
Did anyone have this issue before, please help, it drives me nuts :)
I figured out what was the problem. Each menu item must contain a menu_order property, or else wordpress sorting function will overwrite the same value over and over again showing only the last post.
Im building a website with Nuxt and headless wordpress. All data is coming from wordpress api (wp-json).
I'm having an issue with navigation menu, with parent/child menu items.
On parent element click, every menu item that has children is triggered at once.
I need it to trigger only the one I click on.
Here's what happens #click right now
Heres what I have so far:
HTML:
<nav>
<div class="">
<!-- Triggers burger menu -->
<ul :class="isOpen ? 'block' : 'hidden'" class="sm:flex">
<!-- Loop through the parent navigation -->
<!-- Add click event to trigger dropdown menu -->
<li class="sm:mx-8 text-white cursor-pointer py-4 sm:py-0"
v-for="item in linksArray"
:key="item.ID"
#click="isOpen = !isOpen"
>
<!-- Insert the data from wp-json -->
<div class="">
<!-- Parent navigation elements -->
<div class="flex items-center">
<div class=""> {{item.title}} </div>
</div>
<!-- Child navigation here -->
<div>
<!-- Trigger show or hide child elements on click from above -->
<ul
v-show="isOpen"
class="sm:absolute bg-gray-800 rounded mt-4"
>
<!-- Loop through the child elements -->
<li class="text-white rounded hover:bg-gray-700 cursor-pointer py-2 px-2"
v-for="childItem in item.child_items"
:key="childItem.ID"
>
<!-- Insert the data for children from wp-json -->
<nuxt-link :to="childItem.url">
<div class="flex items-center">
<span class="title">
{{ childItem.title }}
</span>
</div>
</nuxt-link>
</li>
</ul>
</div>
</div>
</li>
</ul>
</div>
</nav>
Script:
<script>
export default {
name: "Navbar",
props: {
linksArray: {
type: Array
},
},
data() {
return {
isOpen: false,
};
},
watch: {
// Use to close the menu on route change
'$route'() {
this.isOpen = false;
},
}
};
</script>
wp-json menu response
{
"term_id": 2,
"name": "Main",
"slug": "main",
"term_group": 0,
"term_taxonomy_id": 2,
"taxonomy": "nav_menu",
"description": "",
"parent": 0,
"count": 14,
"filter": "raw",
"items": [
{
"ID": 319,
"post_author": "1",
"post_date": "2020-01-11 21:42:33",
"post_date_gmt": "2020-01-11 21:42:33",
"post_content": "",
"post_title": "Games",
"post_excerpt": "",
"post_status": "publish",
"comment_status": "closed",
"ping_status": "closed",
"post_password": "",
"post_name": "games",
"to_ping": "",
"pinged": "",
"post_modified": "2020-01-15 21:10:20",
"post_modified_gmt": "2020-01-15 21:10:20",
"post_content_filtered": "",
"post_parent": 0,
"guid": "http://localhost/?p=319",
"menu_order": 1,
"post_type": "nav_menu_item",
"post_mime_type": "",
"comment_count": "0",
"filter": "raw",
"db_id": 319,
"menu_item_parent": "0",
"object_id": "319",
"object": "custom",
"type": "custom",
"type_label": "Custom Link",
"title": "Games",
"url": "#",
"target": "",
"attr_title": "",
"description": "",
"classes": [],
"xfn": "",
"logo": {},
"child_items": [
{
"ID": 321,
"post_author": "1",
"post_date": "2020-01-11 21:42:33",
"post_date_gmt": "2020-01-11 21:42:33",
"post_content": "",
"post_title": "Dota 2",
"post_excerpt": "",
"post_status": "publish",
"comment_status": "closed",
"ping_status": "closed",
"post_password": "",
"post_name": "dota-2",
"to_ping": "",
"pinged": "",
"post_modified": "2020-01-15 21:10:20",
"post_modified_gmt": "2020-01-15 21:10:20",
"post_content_filtered": "",
"post_parent": 0,
"guid": "http://localhost/?p=321",
"menu_order": 2,
"post_type": "nav_menu_item",
"post_mime_type": "",
"comment_count": "0",
"filter": "raw",
"db_id": 321,
"menu_item_parent": "319",
"object_id": "301",
"object": "games",
"type": "post_type",
"type_label": "Game",
"url": "http://localhost/games/dota-2/",
"title": "Dota 2",
"target": "",
"attr_title": "",
"description": "",
"classes": [
""
],
"xfn": "",
"logo": {},
"slug": "dota-2"
},
{
"ID": 322,
"post_author": "1",
"post_date": "2020-01-11 21:42:33",
"post_date_gmt": "2020-01-11 21:42:33",
"post_content": "",
"post_title": "Overwatch",
"post_excerpt": "",
"post_status": "publish",
"comment_status": "closed",
"ping_status": "closed",
"post_password": "",
"post_name": "overwatch",
"to_ping": "",
"pinged": "",
"post_modified": "2020-01-15 21:10:20",
"post_modified_gmt": "2020-01-15 21:10:20",
"post_content_filtered": "",
"post_parent": 0,
"guid": "http://localhost/?p=322",
"menu_order": 3,
"post_type": "nav_menu_item",
"post_mime_type": "",
"comment_count": "0",
"filter": "raw",
"db_id": 322,
"menu_item_parent": "319",
"object_id": "264",
"object": "games",
"type": "post_type",
"type_label": "Game",
"url": "http://localhost/games/overwatch/",
"title": "Overwatch",
"target": "",
"attr_title": "",
"description": "",
"classes": [
""
],
"xfn": "",
"logo": {},
"slug": "overwatch"
},
{},
{},
{}
]
},
{
"ID": 320,
"post_author": "1",
"post_date": "2020-01-11 21:42:33",
"post_date_gmt": "2020-01-11 21:42:33",
"post_content": "",
"post_title": "Gambling",
"post_excerpt": "",
"post_status": "publish",
"comment_status": "closed",
"ping_status": "closed",
"post_password": "",
"post_name": "gambling",
"to_ping": "",
"pinged": "",
"post_modified": "2020-01-15 21:10:20",
"post_modified_gmt": "2020-01-15 21:10:20",
"post_content_filtered": "",
"post_parent": 0,
"guid": "http://localhost/?p=320",
"menu_order": 7,
"post_type": "nav_menu_item",
"post_mime_type": "",
"comment_count": "0",
"filter": "raw",
"db_id": 320,
"menu_item_parent": "0",
"object_id": "320",
"object": "custom",
"type": "custom",
"type_label": "Custom Link",
"title": "Gambling",
"url": "#",
"target": "",
"attr_title": "",
"description": "",
"classes": [
""
],
"xfn": "",
"logo": {},
"child_items": [
{
"ID": 336,
"post_author": "1",
"post_date": "2020-01-12 00:46:53",
"post_date_gmt": "2020-01-12 00:46:53",
"post_content": "",
"post_title": "CSGO Gambling",
"post_excerpt": "",
"post_status": "publish",
"comment_status": "closed",
"ping_status": "closed",
"post_password": "",
"post_name": "csgo-gambling",
"to_ping": "",
"pinged": "",
"post_modified": "2020-01-15 21:10:20",
"post_modified_gmt": "2020-01-15 21:10:20",
"post_content_filtered": "",
"post_parent": 0,
"guid": "http://localhost/?p=336",
"menu_order": 8,
"post_type": "nav_menu_item",
"post_mime_type": "",
"comment_count": "0",
"filter": "raw",
"db_id": 336,
"menu_item_parent": "320",
"object_id": "336",
"object": "custom",
"type": "custom",
"type_label": "Custom Link",
"title": "CSGO Gambling",
"url": "/csgo/gambling",
"target": "",
"attr_title": "",
"description": "",
"classes": [
""
],
"xfn": "",
"logo": {}
}
]
},
{},
{},
{}
]
}
I'm new to development, and been stuck on this for over a week now.
Don't know where to look for answers. Found nothing on here or google.
Could you please point me at what I'm doing wrong or at the solution please.
Thanks!
In your code, all children are using the same variable to say if they should be shown or not. As a result, if this variable is true then all children will be show, and if it's false then all will be hidden.
You need track which menu items you want to show. Here is an example of how you could achieve this:
new Vue({
el: "#app",
data: {
menu: [{
title: "Main one",
children: ['child 1', 'child 2', 'child 3']
},
{
title: "Main two",
children: ['child 4', 'child 5', 'child 6']
},
{
title: "Main three",
children: ['child 7']
},
{
title: "Main four",
children: ['child 8', 'child 9']
}
],
openMenu: null
},
methods: {
showChildren(i) {
if (this.openMenu === i) this.openMenu = null
else this.openMenu = i
}
}
})
li {
margin: 8px;
padding: 8px;
background: pink;
cursor: pointer;
}
<div id="app">
<h2>menu:</h2>
<ul>
<li v-for="(nav, i) in menu" #click="showChildren(i)">
{{nav.title}}
<ul v-show="openMenu === i" v-for="child in nav.children">
<li>{{ child }}</li>
</ul>
</li>
</ul>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue"></script>
Basically, this works by only showing the children if openMenu is the same as the parent items index within the array.
When you click the parent, it sends its index to a function function which checks if it's already open (openMenu is equal to the parents index), and if so changes openMenu to null to hide the children. Else it sets openMenu to the parent index, making v-show="openMenu === i" valid for that parent.
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";}}}}
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/
Im playing around with Woo Commerce API (v3.4.4). I can get a product with:
curl -X GET \
'http://localhost/wp-json/wc/v2/products/7?oauth_consumer_key=ck_8cbe42fb09c04954a63994c22145270f27871dec&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1535381325&oauth_nonce=iApWJblA1A5&oauth_version=1.0&oauth_signature=9T1T43FvOpBzVBU+RQS90zMB/XM=' \
-H 'Cache-Control: no-cache' \
-H 'Postman-Token: bdb7f5c9-5719-4bcf-b4fc-38aed3bd5d08'
I get the response:
{
"id": 7,
"name": "Test product",
"slug": "test-product",
"permalink": "http://localhost/product/test-product/",
"date_created": "2018-08-22T15:25:26",
"date_created_gmt": "2018-08-22T15:25:26",
"date_modified": "2018-08-22T15:25:26",
"date_modified_gmt": "2018-08-22T15:25:26",
"type": "simple",
"status": "publish",
"featured": false,
"catalog_visibility": "visible",
"description": "<p>this is a test product</p>\n",
"short_description": "<p>the short desc</p>\n",
"sku": "",
"price": "9",
"regular_price": "10",
"sale_price": "9",
"date_on_sale_from": null,
"date_on_sale_from_gmt": null,
"date_on_sale_to": null,
"date_on_sale_to_gmt": null,
"price_html": "<del><span class=\"woocommerce-Price-amount amount\"><span class=\"woocommerce-Price-currencySymbol\">€</span>10.00</span></del> <ins><span class=\"woocommerce-Price-amount amount\"><span class=\"woocommerce-Price-currencySymbol\">€</span>9.00</span></ins>",
"on_sale": true,
"purchasable": true,
"total_sales": 3,
"virtual": false,
"downloadable": false,
"downloads": [],
"download_limit": -1,
"download_expiry": -1,
"external_url": "",
"button_text": "",
"tax_status": "taxable",
"tax_class": "",
"manage_stock": false,
"stock_quantity": null,
"in_stock": true,
"backorders": "no",
"backorders_allowed": false,
"backordered": false,
"sold_individually": false,
"weight": "",
"dimensions": {
"length": "",
"width": "",
"height": ""
},
"shipping_required": true,
"shipping_taxable": true,
"shipping_class": "",
"shipping_class_id": 0,
"reviews_allowed": true,
"average_rating": "0.00",
"rating_count": 0,
"related_ids": [
36,
41
],
"upsell_ids": [],
"cross_sell_ids": [],
"parent_id": 0,
"purchase_note": "",
"categories": [
{
"id": 15,
"name": "Uncategorized",
"slug": "uncategorized"
}
],
"tags": [],
"images": [
{
"id": 8,
"date_created": "2018-08-22T15:24:00",
"date_created_gmt": "2018-08-22T15:24:00",
"date_modified": "2018-08-22T15:24:00",
"date_modified_gmt": "2018-08-22T15:24:00",
"src": "http://localhost/wp-content/uploads/2018/08/sock.jpeg",
"name": "sock",
"alt": "",
"position": 0
}
],
"attributes": [
{
"id": 0,
"name": "size",
"position": 0,
"visible": true,
"variation": false,
"options": [
"small",
"medium",
"large"
]
}
],
"default_attributes": [],
"variations": [],
"grouped_products": [],
"menu_order": 0,
"meta_data": [],
"_links": {
"self": [
{
"href": "http://localhost/wp-json/wc/v2/products/7"
}
],
"collection": [
{
"href": "http://localhost/wp-json/wc/v2/products"
}
]
}
}
This works running the request using CURL on the command line, and running the request in Postman.
But now I try to create an order:
curl -X POST \
'http://localhost/wp-json/wc/v2/orders?oauth_consumer_key=ck_8cbe42fb09c04954a63994c22145270f27871dec&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1535382336&oauth_nonce=P8RWaD9DnZ9&oauth_version=1.0&oauth_signature=GFxtI7+gqbah5fu68gSdqL4V3fs=' \
-H 'Cache-Control: no-cache' \
-H 'Postman-Token: f17bec6a-c76b-4a6c-8797-1df9dac73f36' \
-d '{
"payment_method": "bacs",
"payment_method_title": "Direct Bank Transfer",
"set_paid": true,
"billing": {
"first_name": "John",
"last_name": "Doe",
"address_1": "969 Market",
"address_2": "",
"city": "San Francisco",
"state": "CA",
"postcode": "94103",
"country": "US",
"email": "john.doe#example.com",
"phone": "(555) 555-5555"
},
"shipping": {
"first_name": "John",
"last_name": "Doe",
"address_1": "969 Market",
"address_2": "",
"city": "San Francisco",
"state": "CA",
"postcode": "94103",
"country": "US"
},
"line_items": [
{
"product_id": 7,
"quantity": 1
}
],
"shipping_lines": [
{
"method_id": "flat_rate",
"method_title": "Flat Rate",
"total": 7
}
]
}'
When I run the above request with Postman, it creates an empty order and I get the response:
{
"id": 59,
"parent_id": 0,
"number": "59",
"order_key": "wc_order_5b8413f8ed585",
"created_via": "rest-api",
"version": "3.4.4",
"status": "pending",
"currency": "EUR",
"date_created": "2018-08-27T15:08:40",
"date_created_gmt": "2018-08-27T15:08:40",
"date_modified": "2018-08-27T15:08:41",
"date_modified_gmt": "2018-08-27T15:08:41",
"discount_total": "0.00",
"discount_tax": "0.00",
"shipping_total": "0.00",
"shipping_tax": "0.00",
"cart_tax": "0.00",
"total": "0.00",
"total_tax": "0.00",
"prices_include_tax": false,
"customer_id": 0,
"customer_ip_address": "",
"customer_user_agent": "",
"customer_note": "",
"billing": {
"first_name": "",
"last_name": "",
"company": "",
"address_1": "",
"address_2": "",
"city": "",
"state": "",
"postcode": "",
"country": "",
"email": "",
"phone": ""
},
"shipping": {
"first_name": "",
"last_name": "",
"company": "",
"address_1": "",
"address_2": "",
"city": "",
"state": "",
"postcode": "",
"country": ""
},
"payment_method": "",
"payment_method_title": "",
"transaction_id": "",
"date_paid": null,
"date_paid_gmt": null,
"date_completed": null,
"date_completed_gmt": null,
"cart_hash": "",
"meta_data": [],
"line_items": [],
"tax_lines": [],
"shipping_lines": [],
"fee_lines": [],
"coupon_lines": [],
"refunds": [],
"_links": {
"self": [
{
"href": "http://localhost/wp-json/wc/v2/orders/59"
}
],
"collection": [
{
"href": "http://localhost/wp-json/wc/v2/orders"
}
]
}
}
Bizarrely, when i run the EXACT same request with CURL on the command line, I get the response:
{"code":"woocommerce_rest_authentication_error","message":"Invalid signature - provided signature does not match.","data":{"status":401}}
If I run the request in Postman, but for "product_id" I use "this is a completely fake id" it still creates an empty order and responds with:
{
"id": 60,
"parent_id": 0,
"number": "60",
"order_key": "wc_order_5b84148e002da",
"created_via": "rest-api",
"version": "3.4.4",
"status": "pending",
"currency": "EUR",
"date_created": "2018-08-27T15:11:10",
"date_created_gmt": "2018-08-27T15:11:10",
"date_modified": "2018-08-27T15:11:10",
"date_modified_gmt": "2018-08-27T15:11:10",
"discount_total": "0.00",
"discount_tax": "0.00",
"shipping_total": "0.00",
"shipping_tax": "0.00",
"cart_tax": "0.00",
"total": "0.00",
"total_tax": "0.00",
"prices_include_tax": false,
"customer_id": 0,
"customer_ip_address": "",
"customer_user_agent": "",
"customer_note": "",
"billing": {
"first_name": "",
"last_name": "",
"company": "",
"address_1": "",
"address_2": "",
"city": "",
"state": "",
"postcode": "",
"country": "",
"email": "",
"phone": ""
},
"shipping": {
"first_name": "",
"last_name": "",
"company": "",
"address_1": "",
"address_2": "",
"city": "",
"state": "",
"postcode": "",
"country": ""
},
"payment_method": "",
"payment_method_title": "",
"transaction_id": "",
"date_paid": null,
"date_paid_gmt": null,
"date_completed": null,
"date_completed_gmt": null,
"cart_hash": "",
"meta_data": [],
"line_items": [],
"tax_lines": [],
"shipping_lines": [],
"fee_lines": [],
"coupon_lines": [],
"refunds": [],
"_links": {
"self": [
{
"href": "http://localhost/wp-json/wc/v2/orders/60"
}
],
"collection": [
{
"href": "http://localhost/wp-json/wc/v2/orders"
}
]
}
}
So I have a number of problems:
1/ No matter what product_id I use, the order that's created is always empty
2/ Even if I provide a fake product_id, an order is still created by Woo Commerce. Surely this is incorrect?
3/ Running the create order requests on the command line is giving different responses to Postman
I was missing:
-H 'Content-Type: application/json' \
from the request. I had this entered in Postman, but had not ticked the check box: