• 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 }
    0

    1

    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 ??

Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter DuneDx

    (@dunedx)

    anyone?

    Hi DuneDx

    Hope you are well. As I understand, you are trying to list all the posts from a specific category or two. get_post_by_id() function isn’t really meant to do so. You should rather try using WordPress Wp_query with some category parameters.
    try out reading this once.
    https://codex.www.ads-software.com/Class_Reference/WP_Query#Category_Parameters

    good luck. let me know if you still need assistance

    Thread Starter DuneDx

    (@dunedx)

    Thanks Gaurav Jha, this was really helpful. I’ve now made my first script. It only shows the post title at the moment but it works! I’m going to keep working on it to bring in the post content etc.

    <?php
        //results feed ordered by date
        $resultsPosts = new WP_Query('cat=12,13&posts_per_page=9');
        if ($resultsPosts->have_posts()) :
       while ($resultsPosts->have_posts()) :
        $resultsPosts->the_post(); ?>
        <div class="resultsblob <?php the_category(); ?>">
        <h2><?php the_title(); ?></h2>
        </div>
        <?php
                endwhile;
        else:
    endif;
        //This stops wordpress from killing life
        wp_reset_postdata();
        ?>
    Gyan Gaurav

    (@gyan-gaurav)

    Hi DuneDx.

    Glad you have got it.. ??

    Cheers

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘get_post_by_id is not returning any results’ is closed to new replies.