I am trying to modify my loop to save post data by category. I found some code on this site that saved the post’s titles based on their categories and tried to modify this to save the post’s content. However, while get_the_title
and get_the_category
work, get_the_content
returns null.
Here is the code:
if ( false === ( $q = get_transient( 'category_list' ) ) ) {
$args = array(
'posts_per_page' => -1
);
$query = new WP_Query($args);
$q = array();
$body = array();
while ( $query->have_posts() ) {
$query->the_post();
$a="<a href="". get_permalink() .'">' . get_the_title() .'</a>';
$post_id = get_the_ID();
$post_id = $post->ID;
$body[$post_id] = array();
$body[$post_id]['title'] = '<a href="'. get_permalink() .'">' . get_the_title() .'</a>'; //works
$body[$post_id]['content'] = get_the_content('Read more');
$categories = get_the_category();
foreach ( $categories as $key=>$category ) {
$b = '<a href="' . get_category_link( $category ) . '">' . $category->name . '</a>';
}
$q[$b][] = $post_id; // Create an array with the category names and post titles
}
/* Restore original Post Data */
wp_reset_postdata();
set_transient( 'category_list', $q, 12 * HOUR_IN_SECONDS );
}
Edit: Here is how I am using the $body
array:
foreach($q[$b] as $post) {
echo('<div class="teaser"><div class="teaser-title"><a href = "">' . $post . '</a></div><div class="teaser-text">'. $body[$post] . '</div><div class="teaser-footer"><p>pemsource.org</p></div></div>');
}
Edit2: I added the full code. When I do a var dump of body I get NULL and when I do a var dump of $q I get
array(3) { ["Conundrums"]=> array(1) { [0]=> string(64) "new post" } ["Tips and Tricks"]=> array(1) { [0]=> string(80) "Tips and tricks" } ["Uncategorized"]=> array(1) { [0]=> string(78) "Tips and Tricks" } }
seemingly regardless of how I edit the loop. I am very confused. Any help is much appreciated