How can I make a list of categories with 4 last posts in each where every category is sorted by meta_value.
For example, template without sorting by meta_value is It displays parent categories only.
Few days I am searching something related to categories extra fields but can't find something workable.
This is my best search result. What I have to do with this? I don't understand it. Is it possible to do what I want?
How can I make a list of categories with 4 last posts in each where every category is sorted by meta_value.
For example, template without sorting by meta_value is http://pastebin.com/AeH6vx9b It displays parent categories only.
Few days I am searching something related to categories extra fields but can't find something workable.
This is my best search result. What I have to do with this? I don't understand it. Is it possible to do what I want?
This is no solution, just an explanation: WordPress currently doesn't have taxonomy meta data. You could add a stand alone table to add meta data there. Anyway, it's not recommended to add your taxonomy/category/tag meta data to the options table as this one wasn't made to get JOIN
ed to do meta queries by it. My personal recommendation would be that you simply add another taxonomy that has your meta value and as well link this one. This would allow you to do default a tax_query
by two taxonomies and their terms.
Replace your_meta_key_here
with the actual meta key you want to sort by. This code will retrieve categories, then for each category, it will fetch the 10 most recent posts belonging to that category, sorted by the specified meta value
$args = array(
'type' => 'post',
'child_of' => 0,
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => 1,
'taxonomy' => 'category',
'pad_counts' => false,
);
$categories = get_categories($args);
foreach ($categories as $category) {
$args = array(
'posts_per_page' => 10,
'category' => $category->term_id,
'orderby' => 'meta_value', // Sort by meta value
'meta_key' => 'your_meta_key_here', // Replace with your actual meta key
);
$query = new WP_Query($args);
if ($query->have_posts()) {
echo '<h2>' . $category->name . '</h2>';
echo '<ul>';
while ($query->have_posts()) {
$query->the_post();
echo '<li><a href="' . get_permalink() . '">' . get_the_title() . '</a></li>';
}
echo '</ul>';
}
wp_reset_postdata();
}