How do I get the permalink and thumbnail? (with Post Indexer / network_query_post)

I’m creating a custom loop, using Post Indexer, to display of posts from across the network.

In my network_have_posts() loop, how can I display the permalink and the post thumbnail (featured image)?

If I use get_the_post_thumbnail([post ID]), WordPress looks for the thumbnail from the current site’s media library, not the originating site. This picks correct thumbnails from the current site, and incorrect or no thumbnail from other sites.

Similarly, if I use get_the_permalink, WordPress returns the correct URL after the domain name, but with the current sites base domain name.

Hope this makes sense. Please ask if you need any more info about what I’m trying to do.

  • Vaughan
    • Ex Staff

    Hi Mina,

    You could use network_get_permalink() for the permalinks.

    $perm = network_get_permalink( $blog_id, $post_id );

    Similarly, in your loop, for the thumbnail, you should have the $blog_id in the page loop query object.

    So you could use that along with switch_to_blog()

    $perm = network_get_permalink( $post->BLOG_ID, $post->ID );
    switch_to_blog( $post->BLOG_ID );
    $thumbnail = get_the_post_thumbnail($post->ID);
    restore_current_blog();

    Hope this helps

  • Mina Nielsen
    • "So what do we try next?"

    Thanks Vaughan.

    network_get_permalink() looked great when I first read your post. Looking at how that works in post-indexer/includes/functions.php, it looks like it actually uses switch_to_blog() behind the scenes.

    So I’m a little concerned about performance with that. If I’m looping through 20 results, and on each one running 3 or 4 switch_to_blog() actions to grab title, excerpt, thumbnail and permalink, that’s about 60-80 switch_to_blog() actions per page of results. Should I be worried about the performance hit there?

    I guess two things I could do to ease performance would be:

    1. Do one switch_to_blog() action at the start of generating the output for one item, and then just use native WP functions to access the 4 post components I need.

    2. Try to get my head around transients, and store the HTML output of the loop in a transient for a few hours.

    Any thoughts on this? Or do you think switch_to_blog() is actually quite efficient and I don’t need to be too worried about performance?

  • Vaughan
    • Ex Staff

    Hi,

    Using those is the only current way of doing this, Post-Indexer indexes the posts across the networkand copies the posts in their own table

    wp_network_posts & wp_network_postmeta

    So when you call the query, the post content is retrieved from that table & not by querying every single site tables, currently it doesn’t index the images though,so the only way to link up the images is touse switch_to_blog() in order to get the thumbnail asociated with that post_id on that blog_id.

    I can’t think of any other way of doing it currently, but let me ask one of the developers to see if there is a better way of achieving this.

    Hope this helps

  • Panos
    • SLS

    Hi Mina ,

    Apologies for such a late reply here!

    1. Do one switch_to_blog() action at the start of generating the output for one item, and then just use native WP functions to access the 4 post components I need.

    Depending on how you are using it it is possible to add a little overhead if called separately for every component.

    Since you need more than just the permalink, you could call switch_to_blog() once and then retrieve the post you want and it’s post_meta

    switch_to_blog( $blog_id ); // You need this as it resets globals and $wpdb
    $your_post = get_post( $post_id );
    $permalink = get_permalink( $your_post->ID );
    //Do what else you need with the $your_post
    //....
    restore_current_blog();

    2. Try to get my head around transients, and store the HTML output of the loop in a transient for a few hours.

    Using transients to store temp content sounds like a great idea! One thing to keep in mind is that you might probably need to add a hook at “save_post” action to delete this transient.

    Cheers!

    Panos