Note: Don't use query_post
. Use WP_Query
or get_posts
.
Please take a look discussion When to use WP_query(), query_posts() and pre_get_posts.
Here sample approach related to your issue to generate all posts base on their category, then display via shortcode for later.
- Inside a function, you should query all posts first ( use
wp_query
or get_posts
),
- Get related category by post id inside query loop. If term taxonomy, use
get_the_terms
.
- Build an array of query data with key ( in this case, we use term slug ) to grouping posts.
- Then take those data for outside loop, and output them with
simple loop.
- Build a shortcode by function
add_shortcode
( this code use simple shortcode only ).
/** Shortcode [my_shortcode_posts] */
add_shortcode( 'my_shortcode_posts', 'so36133962_get_all_posts_by_category' );
/**
* All posts by category
* build query by get_posts
*
* @return string|null
*/
function so36133962_get_all_posts_by_category( $attr, $content = null )
{
/**
* Build custom query
*
*/
$args = array(
'post_type' => 'your-post-type', // set your custom post type
'post_status' => 'publish',
'posts_per_page' => -1,
/** add more arguments such as taxonomy query i.e:
'tax_query' => array( array(
'taxonomy' => 'genre', // set your taxonomy
'field' => 'slug',
'terms' => array( 'comedy','drama' ) // set your term taxonomy
) )
*/
);
$posts = new WP_Query( $args );
/**
* Prepare Posts
*
*/
$result = array();
// The Loop
if ( $posts->have_posts() )
{
while ( $posts->have_posts() )
{
$posts->the_post();
/**
* Get all item in a term taxonomy
*
*/
$categories = get_the_terms( get_the_ID(), 'your-taxonomy' /* set your term taxonomy */ );
if ( ! $categories )
continue;
foreach ( $categories as $key => $category )
{
$term_name = $category->name;
$term_slug = $category->slug;
$term_id = $category->term_id;
}
/**
* Set thumbnail background cover
* Use Featured Image
*/
$img_cover = '';
if ( has_post_thumbnail() )
{
$image_url = wp_get_attachment_image_src( get_post_thumbnail_id( get_the_ID() ) );
if ( ! empty( $image_url[0] ) )
{
$img_cover = '<span class="img" style="background:url( ' . esc_url( $image_url[0] ) . ' ) no-repeat center center; background-size:cover;">';
}
}
/**
* Format html content
*
*/
$format = '<a href="%1$s" title="%2$s" class="post-%3$s">%4$s%2$s</span></br><span class="content-%3$s">%5$s</span></br><span class="more">%6$s</span></a>';
/**
* Formatted string post content
*
*/
$content = sprintf( $format,
get_permalink(),
get_the_title(),
get_the_ID(),
$img_cover,
get_the_excerpt(),
__( 'Read More', 'text-domain' )
);
/**
* Set an array of each post for output loop
*
*/
$result[ $term_slug ][] = array(
'post_id' => get_the_ID(),
'post_content' => $content,
'term_name' => $term_name,
'term_id' => $term_id
);
}
}
wp_reset_postdata(); // post reset
/**
* Check existing output
*
*/
if ( ! $result )
return;
/**
* Output loop
*
*/
$output = '';
foreach ( $result as $slug => $data )
{
$count = count( $data );
for ( $i = 0; $i < $count; $i++ )
{
/**
* Set data as object
*
*/
$post = ( object ) array_map( 'trim', $data[ $i ] );
if ( 0 == $i )
{
/**
* Set category id and name
*
*/
$output .= '<div id="term-category-' . absint( $post->term_id ) . '">';
$output .= '<h3>' . esc_html( $post->term_name ) . '</h3>';
}
/**
* Set post id and content
*
*/
$output .= '<div id="post-' . absint( $post->post_id ) . '"><p>' . $post->post_content . '</p></div>';
}
$output .= '</div>';
}
return $output; // complete
}
You can change element structures as your needs. As note, with this code, no duplicate posts in each category, and make sure you change the values such as post type and taxonomy as commented in code. I hope this helps.