admin管理员组文章数量:1122832
I got a list of links from an external api, that I want to associate with wordpress posts. The api links are distinguishable by an id. In each Wordpress post, there is a custom field where this id is set.
I need to loop / search through the posts of my-custom-post
to find a post that match the field value, and then replace my custom url param with the standard wordpress page id params.
Example:
Inside the post I want to display (page id 5678) , there is a field member-id
with value 1234
So when this is typed in the url:
https://my-site/my-custom-post/?mid=1234
I want the above url to be resolved to:
https://my-site/my-custom-post/my-slug
Or :
(https://my-site/my-custom-post/?p=5678
)
This is what I've tried
my-custom-post template, with a custom query:
if ( get_query_var('mid') ) {
$args = array(
'post_type' => 'my-custom-post',
'meta_key' => 'member-id',
'meta_value' => get_query_var( 'mid' ),
'meta_compare' => '=',
'numberposts' => 1
);
$posts = get_posts( $args );
} else { /* */}
Simplified:
$mid = get_query_var( 'mid' );
error_log($mid);
Problem here is that the $mid do not return / log anything. So this is the first problem.
I've registred the custom query var in functions.php
function add_query_vars_filter( $vars ){
$vars[] = "mid";
return $vars;
}
add_filter( 'query_vars', 'add_query_vars_filter' );
版权声明:本文标题:wp query - Use custom url params with value from a custom field to return the post containing the field value 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736302274a1931475.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论