admin管理员组文章数量:1387470
I'm trying to find out if it's possible to specify/define the currently queried post in WP_Query
arguments, but having little luck. Not in the loop
, but while the query
itself is running. Specifically I want to use the post id to access other metadata.
An example of usage might be:
meta_key => '_an_existing_meta_key',
meta_value => get_post_meta($current_post_id, '_another_meta_key'),
meta_compare => '!='
Where $current_post_id
is the actual currently queried post ID within the current query being run. Considering that the above example works fine with a hardcoded/specific post ID, it seems like it should work, but I'm having zero luck.
I'm specifically hoping to be able to use a function to define meta_value
.
I'm trying to find out if it's possible to specify/define the currently queried post in WP_Query
arguments, but having little luck. Not in the loop
, but while the query
itself is running. Specifically I want to use the post id to access other metadata.
An example of usage might be:
meta_key => '_an_existing_meta_key',
meta_value => get_post_meta($current_post_id, '_another_meta_key'),
meta_compare => '!='
Where $current_post_id
is the actual currently queried post ID within the current query being run. Considering that the above example works fine with a hardcoded/specific post ID, it seems like it should work, but I'm having zero luck.
I'm specifically hoping to be able to use a function to define meta_value
.
1 Answer
Reset to default 0WordPress has quite a few global variables to choose from which may help. For example, you can use the global $post
to get first post in the query.
global $post;
$my_meta_value = get_post_meta( $post->ID, '_another_meta_key', true );
Or you could reach directly into the $wp_query object posts array and grab the first post:
global $wp_query;
if( ! empty( $wp_query->posts ) ) {
$my_meta_value = get_post_meta( $wp_query->posts[0]->ID, '_another_meta_key', true );
}
本文标签: wp queryAccess queried post ID in WPQuery
版权声明:本文标题:wp query - Access queried post ID in WP_Query 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744575971a2613626.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论