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 queryUse custom url params with value from a custom field to return the post containing the field value