Last active
June 6, 2021 16:29
-
-
Save Jany-M/30d5efb9916858abb7c0 to your computer and use it in GitHub Desktop.
[WordPress] Display Gists as if they were real WordPress Posts (without saving them to database)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// This will display Gists as if they were real WP posts, so you can display them mixed with real ones too | |
// This will NOT add the Gists as real posts in your DB, you won't see them in your backend | |
/* -------------------------------------- | |
| | |
| Real Posts Loop | |
| | |
|---------------------------------------*/ | |
$blog_args = array( | |
'post_type' => 'post', | |
'suppress_filters' => true, | |
'posts_per_page' => 3, | |
'orderby' => 'date', | |
); | |
$blog_loop = new WP_Query( $blog_args ); | |
/* -------------------------------------- | |
| | |
| Get Gists | |
| | |
|---------------------------------------*/ | |
// Get the Cache if exists, if not run the cUrl | |
//delete_transient('gists_as_posts'); // in case you need to get rid of transient quickly | |
if(get_transient('gists_as_posts') === false) { | |
$gist_username = 'Jany-M'; // change this | |
$url = 'https://api.github.com/users/'.$gist_username.'/gists'; | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($ch, CURLOPT_URL, $url); | |
curl_setopt( $ch, CURLOPT_USERAGENT, 'Gist-as-Post' ); | |
$result = curl_exec($ch); | |
curl_close($ch); | |
$gists = json_decode($result, true); | |
// Cache results | |
set_transient('gists_as_posts', $gists, 1 * WEEK_IN_SECONDS); // Set the time constant to whatever feels best https://codex.wordpress.org/Easier_Expression_of_Time_Constants | |
} | |
$gists = get_transient('gists_as_posts'); | |
/* -------------------------------------- | |
| | |
| Create "WP Gists" | |
| | |
|---------------------------------------*/ | |
// Get Latest WP ID | |
$last = wp_get_recent_posts( '1'); | |
$id_post = $last['0']['ID']; | |
// Prepare Gists array for WP | |
$gists_array = array(); | |
// Make each Gist like a fake WP Post | |
$i = 0; | |
foreach ($gists as $key => $value) : | |
$id_post++; | |
$i++; | |
// Make dates like WP | |
$created = str_replace('T',' ', $value["created_at"]); | |
$created = str_replace('Z','', $created); | |
$updated = str_replace('T',' ', $value["updated_at"]); | |
$updated = str_replace('Z','', $updated); | |
// Create WP_Post objects | |
$new_gist = new WP_Post((object)array( | |
'ID' => $id_post, | |
'post_title' => $value["description"], | |
'post_name' => sanitize_title($value["description"]), | |
'post_content' => '<script src="https://gist.github.com/'.$gist_username.'/'.$value['id'].'.js"></script>', | |
'post_date' => $created, | |
'post_date_gmt' => $updated, | |
'post_status' => 'publish', | |
'post_author' => '7', // change this to your appropriate WP user id | |
'post_type' => 'post', // you can set this to whatever you want | |
'guid' => $value["html_url"], | |
)); | |
// Add fake Gist WP post to Gists array | |
$gists_array[] = $new_gist; | |
// Set max Gists to use | |
if($i==3) break; | |
endforeach; | |
/* -------------------------------------- | |
| | |
| Merge with real Loop and finalize | |
| | |
|---------------------------------------*/ | |
// Final Query - Merge the Real Post Query with the fake Gists WP posts | |
$all_posts_in_one = array(); | |
$all_posts_in_one = array_merge( $blog_loop->posts, $gists_array ); | |
// Sort all posts by creation date | |
usort($all_posts_in_one, create_function('$a,$b', 'return strcmp($b->post_date, $a->post_date);') ); | |
/* -------------------------------------- | |
| | |
| Display your final Loop | |
| | |
|---------------------------------------*/ | |
foreach ($all_posts_in_one as $post) : ?> | |
<h3><?php echo $post->post_title; ?></h3> | |
<span><?php echo date(get_option('date_format'), strtotime($post->post_date)); ?></span> | |
<!-- <p><?php //echo $post->post_content; ?></p> --> | |
<?php endforeach; wp_reset_postdata(); ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment