admin管理员组文章数量:1332638
I would like to get the (one) post with the last ID, even if it is not the last post by date. The post belongs to a custom post type.
I need this to assign custom id numbers to my orders. When creating a new order, I am going to find the post with the largest ID, get its order number from its meta, increment it and assign it to the new one. I DO UPDATE order post when order gets changed in any way and update its date to make it appear at the top of the post list. Therefore, the last post by date that WP gets does not necessarily have the largest ID number. I would like to get the post with the largest post ID.
This is an example list of my post ids
3460 3465 3464 3463 3462 3461 3459
I need to get 3465
This is my code:
$args = array(
'post_type' => 'my_order_cpt',
'post_status' => array('confirmed', 'paid', ..... , 'closed'),
'numberposts' => 1,
'orderby' => 'ID',
'order' => 'DESC'
);
$numbers = get_posts($args);
However, this gives me the last post by date. In my exaple, it's 3460.
All the answers I found, e.g. here, describe getting the last post by date.
I know there's a way to do this with a wpdb query ("SELECT meta_value FROM wp_postmeta
WHERE meta_key='[plugin_prefix]_order_number' ORDER BY CAST(meta_value AS UNSIGNED) DESC LIMIT 1" + checking if post is not a draft and not in trash) or by getting all posts of the post type and playing with the array, but is there a way with get_posts()? Maybe, with meta_query? The order number is a separate meta field named '[plugin_prefix]_order_number'.
I would like to get the (one) post with the last ID, even if it is not the last post by date. The post belongs to a custom post type.
I need this to assign custom id numbers to my orders. When creating a new order, I am going to find the post with the largest ID, get its order number from its meta, increment it and assign it to the new one. I DO UPDATE order post when order gets changed in any way and update its date to make it appear at the top of the post list. Therefore, the last post by date that WP gets does not necessarily have the largest ID number. I would like to get the post with the largest post ID.
This is an example list of my post ids
3460 3465 3464 3463 3462 3461 3459
I need to get 3465
This is my code:
$args = array(
'post_type' => 'my_order_cpt',
'post_status' => array('confirmed', 'paid', ..... , 'closed'),
'numberposts' => 1,
'orderby' => 'ID',
'order' => 'DESC'
);
$numbers = get_posts($args);
However, this gives me the last post by date. In my exaple, it's 3460.
All the answers I found, e.g. here, describe getting the last post by date.
I know there's a way to do this with a wpdb query ("SELECT meta_value FROM wp_postmeta
WHERE meta_key='[plugin_prefix]_order_number' ORDER BY CAST(meta_value AS UNSIGNED) DESC LIMIT 1" + checking if post is not a draft and not in trash) or by getting all posts of the post type and playing with the array, but is there a way with get_posts()? Maybe, with meta_query? The order number is a separate meta field named '[plugin_prefix]_order_number'.
1 Answer
Reset to default 0Turns out, I was kind of wrong when I tried to get the largest ID. What I needed was to get posts by, and then to sort by, the order number itself! Here's what I came up with:
$num_args = array(
'post_type' => 'my_store_order',
'post_status' => array('mystatus_1', 'mystatus_2', 'mystatus_N'),
'numberposts' => 1,
'meta_query' => array(
'my_clause' => array(
'key' => 'my_order_number',
'type' => 'numeric',
'compare' => 'EXIST'
)
),
'orderby' => 'my_clause',
'order' => 'DESC'
);
Also, since post meta is a string, I added 'type' => 'numeric' so when being sorted it would be treated as a number (1,2,10 vs 1,10,2)
This post helped me a lot
本文标签: How to get a custom post with the largest ID (not the last post by date)
版权声明:本文标题:How to get a custom post with the largest ID (not the last post by date) 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742261578a2442612.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
'orderby' => array( 'ID' => 'DESC' )
and removeorder
parameter makes any difference? – mozboz Commented Jul 14, 2020 at 17:16fields
parameter in your query. Then sort that array and get the first index. – Abhik Commented Jul 14, 2020 at 17:38