Skip to content

Instantly share code, notes, and snippets.

@coulterpeterson
Last active November 26, 2019 20:22
Show Gist options
  • Save coulterpeterson/d00740a8b62ea7c79ee59ce5773c5537 to your computer and use it in GitHub Desktop.
Save coulterpeterson/d00740a8b62ea7c79ee59ce5773c5537 to your computer and use it in GitHub Desktop.
Get single #blog post by custom meta value and pull lots of #post data #WordPress #PHP
$featuredPostOneArgs = array(
'post_type' => 'post',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'homepage_featured_article_1',
'value' => '1',
'compare' => '=='
)
)
);
$postOneQuery = new WP_Query($featuredPostOneArgs);
if( $postOneQuery->have_posts() ) {
while( $postOneQuery->have_posts() ) {
$postOneQuery->the_post();
$postOneLink = get_the_permalink();
$postOneTitle = get_the_title();
if (has_post_thumbnail( get_the_ID() ) ) {
$postOneImage = wp_get_attachment_url( get_post_thumbnail_id( get_the_ID() ), 'thumbnail' );
} else {
$postOneImage = '';
}
$postOneExcerpt = wp_trim_words( get_the_content(), 15, '...' );
$postOneCategories = get_the_category();
$postOneParentCat = '';
$postOneSubCat = '';
foreach($postOneCategories as $postOneCategory){
if ($postOneCategory->name !== 'Uncategorized') {
if( ($postOneCategory->category_parent == 0) ){
$postOneParentCat = $postOneCategory->name;
} else {
$postOneSubCat = $postOneCategory->name;
}
}
} // End foreach
}
}
wp_reset_query();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment