Home › Toolset Professional Support › [Resolved] Can't retrieve posts with custom field value empty
This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.
Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.
Our next available supporter will start replying to tickets in about 0.55 hours from now. Thank you for your understanding.
Sun | Mon | Tue | Wed | Thu | Fri | Sat |
---|---|---|---|---|---|---|
- | 10:00 – 13:00 | 10:00 – 13:00 | 10:00 – 13:00 | 10:00 – 13:00 | 10:00 – 13:00 | - |
- | 14:00 – 18:00 | 14:00 – 18:00 | 14:00 – 18:00 | 14:00 – 18:00 | 14:00 – 18:00 | - |
Supporter timezone: Asia/Kolkata (GMT+05:30)
Tagged: Types fields API, Types plugin
Related documentation:
This topic contains 15 replies, has 2 voices.
Last updated by Minesh 5 years, 11 months ago.
Assisted by: Minesh.
I am trying to:
I am trying to retrieve all posts of a specific CPT, within a specific custom taxonomy, to get sorted by a custom field.
Link to a page where the issue can be seen:
hidden link
I expected to see:
I can retrieve the posts of which the above target custom field has a value.
Instead, I got:
However I want all posts whatever the value is empty or not.
-----------------------
$post_type = 'carte-item';
$args = [
'posts_per_page' => -1,
'post_type' => $post_type,
'tax_query' => array(
array(
'taxonomy' => $target_taxonomy,
'field' => 'term_id',
'terms' => $each_term->term_id,
)
),
'meta_key' => 'wpcf-display-order',
'orderby' => 'meta_value',
'order' => 'DESC'
];
$the_query = new WP_Query($args);
I dont know how you can help me on live chat.
I was waiting and nothing is happened to answer.
Please answer me.
Hello. Thank you for contacting the Toolset support.
Well - as I understand, you want to fetch the posts from your CPT where taxonomy term is not assigned - correct?
CPT "carte-item" has custom field "display-order" and "price";
And these CPTs are categorized via the taxonomy "carte-cat".
Let me give some example here:
----------------------------------------
> [title] California Merlot Duckhorn
[display-order] 2
[price] 4,800 JPY
[category] Wine
----------------------------------------
> [title] Yamazaki Whisky 12yr
[display-order]
[price] 2,900 JPY
[category] Whisky
----------------------------------------
> [title] MOUTON CADET BLANC
[display-order] 1
[price] 1,800 JPY
[category] Wine
----------------------------------------
** Please note that "display-order" of 1st and 3rd items are set to a numeric value, but the 2nd is left empty.
If I query with
$post_type = 'carte-item';
$args = [
'posts_per_page' => -1,
'post_type' => $post_type,
'tax_query' => array(
array(
'taxonomy' => $target_taxonomy,
'field' => 'term_id',
'terms' => $each_term->term_id,
)
),
};
$the_query = new WP_Query($args);
this will result as:
----------------------------------------
[title] [display-order] [price] [category]
a. California Merlot Duckhorn 2 4,800 JPY Wine
b. Yamazaki Whisky 12yr 2,900 JPY Whisky
c. MOUTON CADET BLANC 1 1,800 JPY Wine
----------------------------------------
My EXPECTED result is here where 2 wines are ordered in "display-order" field
----------------------------------------
[title] [display-order] [price] [category]
a. Yamazaki Whisky 12yr 2,900 JPY Whisky
b. MOUTON CADET BLANC 1 1,800 JPY Wine
c. California Merlot Duckhorn 2 4,800 JPY Wine
----------------------------------------
When I change the code as below (by adding meta_key, ordeby, order
$args = [
'posts_per_page' => -1,
'post_type' => $post_type,
'tax_query' => array(
array(
'taxonomy' => $target_taxonomy,
'field' => 'term_id',
'terms' => $each_term->term_id,
)
),
'meta_key' => 'wpcf-display-order',
'orderby' => 'meta_value',
'order' => 'DESC'
];
$the_query = new WP_Query($args);
the results shows only 2 lines, **LACKING** Yamazaki Whisky which have no "display-order" set.
----------------------------------------
[title] [display-order] [price] [category]
a. MOUTON CADET BLANC 1 1,800 JPY Wine
b. California Merlot Duckhorn 2 4,800 JPY Wine
----------------------------------------
This is just an example. In our running site, there are hundreds of items, which does not have any value in "display-order". So that if I try to sort some of those items with display-order set, most of hundreds of items will not appear in the list. Of course I know that if I enter the display-order for each hundreds of items, it will work, which is too much hours we need to spend on it.
Well - what if you try to add meta_query to your query argument:
For example - could you please try following code where I've added the meta_query argument:
$args = [ 'posts_per_page' => -1, 'post_type' => $post_type, 'meta_query' => array( array( 'key' => 'wpcf-display-order', 'compare' => 'NOT EXISTS' ), ), 'tax_query' => array( array( 'taxonomy' => $target_taxonomy, 'field' => 'term_id', 'terms' => $each_term->term_id, ) ), 'meta_key' => 'wpcf-display-order', 'orderby' => 'meta_value', 'order' => 'DESC' ];
I have just tried; unfortunately it results NOTHING (no results) after adding what you have guided me.
Thank you for advance.
This is your expected result - correct?
My EXPECTED result is here where 2 wines are ordered in "display-order" field ---------------------------------------- [title] [display-order] [price] [category] a. Yamazaki Whisky 12yr 2,900 JPY Whisky b. MOUTON CADET BLANC 1 1,800 JPY Wine c. California Merlot Duckhorn 2 4,800 JPY Wine
If yes, I need problem URL and access details with information about where you have added the code you shared with me.
*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.
I would additionally need your permission to de- and re-activate Plugins and the Theme, and to change configurations on the site. This is also a reason the backup is really important. If you agree to this, please use the form fields I have enabled below to provide temporary access details (wp-admin and FTP).
I have set the next reply to private which means only you and I have access to it.
Well - when I try to access the wp-admin the page does not load and I get "403 Forbidden" error.
Could you please correct that and send me working wp-admin.
Sorry for that.
The server was set denied access from outside Japan for default.
I just remove the flag, so that you can access from outside of Japan.
Best regards.
Thank you - wp-admin access details works now.
Just wanted to ensure - could you please share the file path where you added the code you shared and again to ensure your expected results.
Thanks!
[PATH]
/home/pep-test/www/chasai-xiexie.jp/acms/wp-content/themes/chasai-xiexie.jp/
[FILE]
taxonomy-carte-cat.php
Thank you for sharing the file path.
Now, I'm on this page:
=> hidden link
You want to order the list items displayed at top of the page - correct? as I see the display-order field is connected with post type "carte-item" - correct?
Thank you for your rapid response, and sorry for my late response! 🙂
Yes, the "dsplay-order" is connected to "carte-item", sure.
Could you please see the following page:
hidden link
Currently there are no items (carte-items) displayed within this page.
This is the page where I am getting headache on.
Sincerely,
KC
Well - its not showing any results on the following page:
=> hidden link
The above you are loading is for term ID 36 and when I check the term ID 36 - there is no posts assigned with this term ID. Please check following screenshot:
=> hidden link
That is why its not displaying any results. What if you try to assign a post to this term and load the above page again. It should work.
Hello,
Yes, I have checked and noticed that there is no items selected for that term.
So that I have re-categorized for the targeted term. Unfortunately, the results does not show any changes.
hidden link
Sincerely,
KC