Last active
November 26, 2019 20:22
-
-
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
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
$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