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 |1 Answer
Reset to default 0Here 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.
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