get_post_by_id is not returning any results
-
Hi this is my first post on the forums.
Overall objective
I’m trying to make an interactive news feed on my website where users can press one of two buttons to filter which posts are shown (full content). I’m trying to write a php script to pull all of my posts in to a <div id=”feed”> within individual divs class by category ID. I then plan to use some js and css to hide the post classes that aren’t needed when a user clicks one of the buttons.Problem
I’ve got stuck fairly early on. my php script for pulling in my posts’ content seems to work (it no longer displays an error message) but displays 0 posts. There are three posts with category 12/13 so not sure where I’m going wrong.var dump says:
array(2) { [0]=> NULL [1]=> NULL }
01
I wondered if it is because I have put the entire script in to a template page rather than enqueueing it. But I’m not any clearer after reading the codex.
Script
<div id="feed" class="allresults">please please work <?php function get_post_by_id($id){ $content = array(); foreach ($id as $key => $value) { $post_content = get_post($value); $content[]=$post_content->post_content; } return $content; } //get the post value and content and load it into the array $last_post $last_post = get_post_by_id(array(12,13)); var_dump($last_post); if ($last_post == NULL) echo 'NO CONTENT'; //start a loop through each value of $last_post foreach($last_post as $value => $content){ //for each element found in $last_post, open a new div to load in the content echo '<div class="category">'; //open new HTML elements to load in specific bits of information echo '<h2 class="post_number">' . $value . '</h2>'; echo '<p class="post_content">' . $content . '</p>'; //close container div echo '</div>'; } ?> </div>
If someone could point my the right direction that would be fantastic ??
- The topic ‘get_post_by_id is not returning any results’ is closed to new replies.