admin管理员组

文章数量:1389762

I have a question.

I have 2 post. post A and post B. Post B have field ID post A. I want create a function to retrieve data from post B that have the post A ID. I don't know how to do this.

function show_post_data(){
global $post; 

$cid = $post->ID; //I create this because I want to show the Post B data in Single Post A

$my_posts = get_posts( array('post_type'=>'post_b',)); //retrive Post B data

up until here, I don't know how to retrieve data from post B or to display it. someone help me

I have a question.

I have 2 post. post A and post B. Post B have field ID post A. I want create a function to retrieve data from post B that have the post A ID. I don't know how to do this.

function show_post_data(){
global $post; 

$cid = $post->ID; //I create this because I want to show the Post B data in Single Post A

$my_posts = get_posts( array('post_type'=>'post_b',)); //retrive Post B data

up until here, I don't know how to retrieve data from post B or to display it. someone help me

Share Improve this question edited Mar 29, 2020 at 16:42 fuxia 107k39 gold badges255 silver badges459 bronze badges asked Mar 29, 2020 at 15:47 user3239674user3239674 134 bronze badges 3
  • If you would like to get a specific post, the basic method is get_post() (instead of get_posts). So that you can specific the ID of post_b. eg $post_b = get_post( post_b_id) But this way, it is not dynamic because you need to hard code the ID. So it is a basic method. It depends on how you want to develop and design your structure. For details, you may read get_post() – 西門 正 Code Guy - JingCodeGuy Commented Mar 29, 2020 at 16:01
  • I have being curious on how to get the detail from other post in single post A..is it possible? @simongcc – user3239674 Commented Mar 29, 2020 at 16:26
  • Yes, it is possible and the method is same. Maybe I write you an example to help you understand. – 西門 正 Code Guy - JingCodeGuy Commented Mar 29, 2020 at 16:50
Add a comment  | 

1 Answer 1

Reset to default 0

Here is a simple test drive for your question.

// suppose you paste it into a template, eg. page.php or single.php
// I leave out any test or checking, to show to concept
function display_other_post() {
   global $post;

   $current_post_id = $post->ID;
   $other_post_id = 14;

   $other_post = get_post( $other_post_id );
   print_r( $other_post ); // do whatever you need  
}
display_other_post();

In this example, if you run it, no matter what post are you in, you will always display post with ID=14. So I have mentioned that this is a basic method without dynamic capability unless you can get the post ID from your GUI such as adding options in backend, meta value. But it depends on your design and structure first. So, the first thing is what kind of effect you want to accomplish and how you want to input.

本文标签: Retrieve value between 2 post