questions

WP get_posts() shows only partially correct data of custom post inside user profile

Rate this post

I have a wordpress-based website where I’m trying to move some custom functionality that used to be just plain mysql table and some code to insert/update/show stuff from it. I’m trying to merge it in WP as custom posts so I can use filters, tags, pagination and such.

It supposed to show a few custom items inside the user profile (Ultimate Member). Like, user info, then 10 of user’s top items of this kind, then on next tab 10 items of some other kind, etc.

But seems like things aren’t as simple in WP, and you can’t just toss things on top of each other and expect it to not overlap. >_> So when I’m trying to add a block with custom post type data, it returns only some of the data mixed with profile data mixed with nothingness.

Yeah, I get it, there’s probably a profile loop and some data already inside variables, as far as I could understand from manuals. What I can’t understand is how to fix it. Here’s how it looks:

$args = array(
    'author'      => $uid,
    'numberposts' => 10,
    'post_type'   => 'ff',
);
$ff = get_posts($args);
if($ff){
    foreach($ff as $f){
        setup_postdata($f);
        the_content(); //shows what's needed, as well as ID, the_time() and some more
        the_title(); //shows author's name instead of post title
        the_tags(); //shows nothing, as well as excerpt, etc
        get_the_excerpt(); //still nothing
        $f->post_excerpt; //but this shows the excerpt, as well as print_r($f)
    }
    wp_reset_postdata();
}

Maybe someone could give a hint what am I missing? Thanks in advance!

 

✔️Solution:

Try using the post ID to get your data and echo it. A little more code but may work better.

// Set the global post up here

global $post;

$args = array(
    'author'      => $uid,
    'numberposts' => 10,
    'post_type'   => 'ff',
);
$ff = get_posts($args);
if($ff){
    foreach($ff as $f){
        setup_postdata($f);
        $id = $f->ID; // get post ID
        $content = get_the_content($id); // get the content to echo later
        $tags = get_the_tags($id); // use to get tags, these are not part of the get_posts return object

        echo $content; // show the content

        echo $f->post_title; // show the returned post title. can use get_the_title($id) and echo it if this does not work
        
        // Display the tags after getting them above

        foreach ($tags as $tag) {
          echo $tag->name . ', ';
        }

        // You can get the excerpt this way too but you said your other worked okay

        $excerpt = get_the_excerpt($id); 
        echo $excerpt;
    }
    wp_reset_postdata();
}

(Original) To elaborate. This sets up the global post object. Which is generally a requirement for your functions to work in the loop. Not always the case as I’ve found over the years, but a good practice if you’re not using the post ID in the loop to get your data.

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button