Post Indexer hooks? Rather, what gets indexed?

Ohi there!

I'm starting on building a page that shows off all recent posts on the network. That's nice and all, but I need a few things.

1. Are posts indexed by their own ID, or is there a random key?
2. Where can I find the indexed posts in the database?
3. Are the "featured" images also indexed?
4. Are the authors indexed?
5. Are the site-id's indexed?
6. Are the links to the post indexed?
7. Are the titles indexed?
8. Are the tags indexed?
9. Are the postdates indexed?

What would be most desired is a functionality like the code below, but then with any blog and with featured image, author, date and tags.

The code below is on my homepage (when a user is logged in) and shows the latest posts of the user's primary blog:

$user_arg = wp_get_current_user();
$user_id = $user_arg->ID;
$user_blog_id = get_user_meta($user_id, 'primary_blog', true);
switch_to_blog($user_blog_id);
$recent_posts = wp_get_recent_posts(array( 'numberposts' => '5' ));

if (!empty($recent_posts)) {
	echo '<ul>';
	foreach( $recent_posts as $recent ){
		echo '<li><span class="post-date">' . get_the_date("d M", $recent["ID"]) . ': </span><a href="http://' . $user_mapped_url . strstr(substr(get_permalink($recent["ID"]), 8), '/') . '">';

		if (strlen($recent["post_title"])>20) {
			echo $recent["post_title"] = substr($recent["post_title"], 0, 10) . "..." . substr($recent["post_title"], -7);
		} else {
			echo $recent["post_title"];
		}

		echo '</a></li>';
	}
	echo '</ul>';
} else {
	echo 'Geen recente berichten :(';
}
restore_current_blog();

Any inputs will be greatly appreciated :slight_smile:

In the attachment you can see the mock-up, every block will contain a title, is encapsulated by the '<a href="postlink"' and when available the Featured Image will be the background of that block.
The tags are all stored in javascript together with the author's nicename and title and will be called upon through the filter function (tags/title/author).

  • Sybre Waaijer

    Found them! (your documentation is out of date :slight_frown: )

    wp_network_terms
    wp_network_term_relationships
    wp_network_postmeta
    wp_network_term_taxonomy
    wp_network_term_relationships

    And the most important:
    wp_network_posts

    The last table contains:
    Blog ID (row: BLOG_ID)
    Post ID (row: ID)
    Author ID (row: post_author)
    Post title (row: post_title)
    Post Status (row: post_status) so you can filter anything that isn't in "publish" status
    Last modified date (row: post_modified) in date_i18n format
    URL (row: guid) with domain mapping and HTTPS support!!!!! :smiley:
    Post Type (row: post_type)

    I'll post a query with the desired results after this for future visitors :slight_smile:
    With these inputs I could also pull the featured image if I'm not mistaken, although these are not indexed AFAIK. EDIT: I see there are indexed as _thumbnail_id - From http://codex.wordpress.org/Function_Reference/wp_get_attachment_image you can see that wp_get_attachment_image( $attachment_id ) can be used. I'm not sure if that'll work on a network basis but it's worth a shot :slight_smile:

  • Patrick

    Hey there @Sybre

    I hope you're having a great day!

    Great to see that you've got most of it worked out.

    And you're right, the plugin docs could do with an overhaul. I'll be sure our docs team gets that on their to-do list... oh wait, I'm on the docs team. Done! :slight_smile:

    As for the images, the thumbnail tag may be more reliable if you're fetching featured images.

    Looking forward to your writeup on getting everything all tied together in your template.

  • Sybre Waaijer

    Well, here it is. Took me 1 hour to figure out the SQL >.<

    The code below is fully working (as a plugin, you must install it :slight_smile: two side-red beams, it might have), feel free to use and edit it.

    I think it's quite intensive on your server's CPU when a lot of people decide they want to use an featured image. The styling is up to you and I'm not going to provide any CSS because this project was (and still is) actually private :slight_smile:. The whole page as the mock-up in the first post isn't (quite) possible with this code but it sure as hell gets close.

    The PHP is quite self explanatory and simple to edit, change the LIMIT 50 to the amount of posts you want to pull. Post Indexer is required. I used 50 as a random test number.

    The SQL hasn't been prepared but isn't really needed because data is only being pulled and not sent.

    You need to use the following shortcode after the plugin has been activated: [hmpl-posts-page], feel free to use this shortcode before you activate it, it might be awesome.

    The images are defaulted to 150px x 150px and don't benefit from CDN inputs. In fact, the blog you're using the shorcode on will be the prefix for the images xD.

    Well, enough typing with random "I worked too long today" comments

    <?php
    /**
     * Plugin Name: Host Mijn Pagina Posts
     * Plugin URI: https://hostmijnpagina.nl
     * Description: Creates a dynamic list with posts from WPMUdev Post Indexer
     * Author: Sybre Waaijer
     * Version: 0.1.0
     * Author URI: https://cyberwire.nl
     */
    
    add_action( 'init', 'hmpl_add_homepage_shortcodes' );
    
    function hmpl_add_homepage_shortcodes() {
    	add_shortcode( 'hmpl-posts-page', 'show_posts_with_red_beams');
    }
    
    function show_posts_with_red_beams() {
    	global $wpdb;
    
    	$hmp_posts = $wpdb->get_results("
    		SELECT posts.BLOG_ID AS BLOG_ID, posts.ID AS ID, guid, post_author, post_date_gmt, post_title, images.meta_value AS image_id FROM {$wpdb->base_prefix}network_posts AS posts INNER JOIN {$wpdb->base_prefix}blogs AS blogs ON blogs.blog_id = posts.BLOG_ID LEFT JOIN {$wpdb->base_prefix}network_postmeta AS images ON images.blog_id = posts.BLOG_ID AND images.post_id = posts.ID AND images.meta_key = '_thumbnail_id' WHERE blogs.public = 1 AND blogs.archived = 0 AND blogs.spam = 0 AND blogs.deleted = 0 AND blogs.mature = 0 AND post_status = 'publish' AND post_password = ''
    		ORDER BY post_date_gmt DESC
    		LIMIT 50
    	");
    
       	foreach ($hmp_posts as $hmp_post) {
    		if (empty($hmp_post->image_id)) {
    			$image_url = '';
    		} else {
    			switch_to_blog($hmp_post->BLOG_ID);
    				$image_url = ' style="background-image: url(\''. wp_get_attachment_thumb_url($hmp_post->image_id) .'\');"';
    			restore_current_blog();
    		}
    		echo '<div class="topleft mediumsquare">';
    			echo '<a href="'. $hmp_post->guid .'" target="_blank">';
    				echo '<span class="background-image">';
    					echo '<span class="post-image"'. $image_url .'>';
    						echo '<span class="post-title">';
    							echo $hmp_post->post_title;
    						echo '</span>';
    					echo '</span>';
    				echo '</span>';
    			echo '</a>';
    		echo '</div>';
    	}
    }

    PS. Whoops I crammed the SQL together so it's unreadable :wink: (don't worry, the limit is still readable :slight_smile: )
    PS2. Wow very sorry for the wrong SQL statement causing duplicate values (bad CASE use), rewrote the query :slight_smile:
    Preview: https://posts.hostmijnpagina.nl/ (work in progress)

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.