Skip to content

Instantly share code, notes, and snippets.

@davilera davilera/00-README.md
Last active Mar 9, 2018

Embed
What would you like to do?
WordPress Loop

Nothing relevant.

<?php
$query = new WP_Query( array(
'post_type' => 'post',
'posts_per_page' => 5
) );
echo '<ul>';
while ( $query->have_posts() ) {
$query->the_post();
echo '<li>' . get_the_title() . '</li>';
}//end while
echo '</ul>';
wp_reset_query();
<?php
function nelio_post_to_aws_array( $post_id ) {
$query = new WP_Query( array(
'p' => $post_id
) );
if ( ! $query->have_posts() ) {
wp_reset_postdata();
return false;
}//end if
$query->the_post();
$result = array(
'id' => get_the_ID(),
'author' => get_the_author(),
'content' => apply_filters( 'the_content', get_the_content() ),
'title' => get_the_title(),
);
wp_reset_postdata();
return $result;
}//end nelio_post_to_aws_array()
<?php
array (size=4)
'id' => int 1
'author' => string 'David'
'content' => string '<p>Lorem ipsum dolor sit...'
'title' => string 'My First Post'
<?php
array (size=4)
'id' => int 1
'author' => string 'David'
'content' => string '<p>Lorem ipsum dolor sit...'
'title' => string 'Some Random Title'
<?php
array (size=4)
'id' => int 25
'author' => string 'Ruth'
'content' => string '<p>Cum sociis natoque penatibus et...'
'title' => string 'Some Random Title'
<?php
add_filter( 'the_content', 'random_plugin_related_posts' );
function random_plugin_related_posts( $content ) {
$query = new WP_Query( array(
'p' => 25, // Retrieve related post...
) );
$query->the_post();
$content .= '<a href="...'; // Add link to related post.
wp_reset_postdata();
return $content;
}//end random_plugin_related_posts()
<?php
function nelio_post_to_aws_array( $post_id ) {
$query = new WP_Query( array(
'p' => $post_id
) );
if ( ! $query->have_posts() ) {
wp_reset_postdata();
return false;
}//end if
$query->the_post();
$author = get_the_author();
$query->reset_postdata();
$content = apply_filters( 'the_content', get_the_content() );
$query->reset_postdata();
$title = get_the_title();
$query->reset_postdata();
$result = array(
'id' => $post_id,
'author' => $author,
'content' => $content,
'title' => $title,
);
wp_reset_postdata();
return $result;
}//end nelio_post_to_aws_array()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.