Show a post from selected categories wordpress - php

I'm trying to show one post from several categories. My code just shows the first category post :\ any advice?
<?php
$args = array(
'cat' => 1,15,
'post_type' => 'post',
'posts_per_page' => '1',
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) :
while ($query->the_post()):
the_title();
the_post_thumbnail(array(200, 200));
?>
<?php endwhile;
endif;?>

Please follow the code to understand how to show post items from selected category items by passing the category ID.
$args = array(
'post_type' => 'post', // post type
'posts_per_page' => -1, // number of post items
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'term_id',
'terms' => array( 16, 244 ) // pass the ID of the post category, separated by a comma.
)
)
);

You defined 'posts_per_page' => '1' so you are getting exactly what you ask: 1 post. Either from category 1 or 15, whichever is the most recent post.
If you want 1 post from EACH category, I would just loop your code, with a different category each time (just 1).
Only thing is, that will be in order of the category IDs you give and not sorted on date on something else. Also, if you have a post in multiple categories, you might end up with the same post twice.

Related

Wordpress - Get 5 posts with the same terms as the current post (custom post type and custom taxonomy)

I want to show a list of posts with the same terms as the post you are currently viewing (it's basically a related posts list).
It's a custom post type (called "oferta") and a custom taxonomy ("categoría").
I want to show 5 posts maximum (not all) as an ul list.
Using most of the code from https://wordpress.org/support/topic/query-cpt-that-share-the-same-taxonomy-as-the-current-cpt/ I've managed to get the list, but it has 3 problems:
It's not limiting to 5 posts
It's duplicating the li elements (1 correct element, 1 empty element)
It's showing the current post also on the list (I only want to show the other posts with the same term, not the current post)
For example, I get this on one of the posts:
I'm using the following code:
$custom_terms = wp_get_post_terms( get_the_ID(), 'categoria');
$args = array(
'post_type' => 'oferta',
'tax_query' => array(
array(
'taxonomy' => 'categoria',
'posts_per_page' => 5,
'field' => 'slug',
'terms' => $custom_terms[0]->slug,
),
)
);
$loop = new WP_Query( $args );
echo '<ul>';
while ( $loop->have_posts() ) : $loop->the_post();
echo '<li>'.get_the_title().'<li>';
endwhile;
echo '</ul>';
wp_reset_postdata();

How to display posts based on sub categories of custom post type?

I have a custom post type called "categories" and it's slug is "category". I have various categories like:
Beauty
Makeup
Skincare
Tech
I have these categories in a CPT called "Products". I want to display only those posts that match with a subcategory. For example, I want to display only those posts from CPT "Products" that have "Makeup" checked in categories custom taxonomy. I have tried the following code:
$args= new WP_Query( array(
'post_type' => 'Products',
'tax_query' => array(
array (
'taxonomy' => 'categories',
'field' => 'slug',
'terms' => 'Beauty',
)
),
) );
if($args->have_posts()):
while ($args->have_posts()):$args->the_post();
echo get_field('name');
endwhile;
endif;
But this code obviously displays posts that have category checked as "Beauty". It doesn't check for subcategory. Can anyone help me out with this? Any modification to the current code would be helpful as well.
Thanks!
If you want to display posts from a certain subcategory you can simply use get_posts(), something like:
$posts = get_posts(array(
'post_type' => 'Products',
'post_status' => 'publish',
'cat' => your subcat ID,
'posts_per_page' => -1
));
And then loop through your posts like:
foreach ($posts as $post){
echo $post->post_title . '<br>';
}

Wordpress Echo post count

I have a custom post type with multiple attributes within it,
such as price
color make model, etc.
Which is would like Wordpress to print the amount of posts within them.
my current code is this
// Get total number of posts in "vehiclestock" post type
$count_vehiclestock = wp_count_posts('listings');
$total_vehiclestock = $count_vehiclestock->publish;
echo $total_vehiclestock . ' listings. ';
I've attached some examples to better explain myself.
as I am unaware of what to search for to answer my own question
Add This Code for count all post or category wise count post
$args = array(
'posts_per_page' => -1,
'post_type'=> 'post',
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => 'category_slug'
)
),
);
$wp_query = new WP_Query( $args );
$post_count = $wp_query->post_count;

Wordpress posts count with comman tags within specific category

i have two categories 'A' and 'B'. Both have 5 posts(each) with tag 'C'. How i can display the number of posts as 5 in Category 'A' or 'B' page. It showing total number of post with tag 'C' as 10. I need to show it as 5. How i can pass category variable to display exact count with tag within specific category. Here is my current code:
$tag = get_term_by('name', $tags,'post_tag');
$count = $tag->count;
reference: https://codex.wordpress.org/Function_Reference/get_term_by
This is not possible with the get_term_by() method.
Try this:
$args = array(
'posts_per_page' => -1,
'tax_query' => array(
'relation' => 'AND', // only posts that have both taxonomies will return.
array(
'taxonomy' => 'post_tag',
'field' => 'name',
'terms' => 'TAG_C', // you can also use an array with tags.
),
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => 'CATEGORY_B', // you can also use an array with categories.
),
),
);
$posts = get_posts($args);
$count = count($posts);
I haven't tested this code, but it should work. Ofcourse you do have
to set the correct terms.
UPDATE
When dealing with many posts I would create a $wpdb sql query that counts the rows, so only a number is returned and not all the posts. You can find an sql count example here.
You can use "found_posts" property of the WP_Query class object.
$args = array(
'posts_per_page' => 10,
'category_name' => 'aaa', // category slug
'tag' => 'ccc' // tag slug
);
$wp_query = new WP_Query( $args );
echo "Total posts count: ".$wp_query->found_posts;

WP Loop with custom array field as argument

I'm trying to do a query with the arguments below. Somehow, WP just doesn't return any posts. I can't figure out what I'm doing wrong! Any help?
Some more info: I have a custom post type that contain featured images. I want them to be displayed in a header slider. Through Advanced Custom Fields plugin I've created a custom field in the posts: 'assigned_page'. It's an array with page ID's on which that specific slide should be displayed. '$current_page' is the ID of the current page that's to be displayed. So, $args should filter the custom post type, and the posts that have the current page ID in their 'assigned_page' array.
// Get the current page ID
global $post;
$current_page = $post->ID;
$string_page = (string)$current_page;
$current_parent_page = $mv_is_subpage->ID;
// Post selection
$args = array (
'post_type' => $post_type,
'posts_per_page' => $posts_per_page,
'orderby' => $orderby,
'order' => $order,
'no_found_rows' => 1,
'meta_query' => array(
array(
'meta_key' => 'assigned_page',
'meta_value' => $string_page,
)
),
);
Then:
$query = new WP_Query( $args );
And then the loop:
while ($query->have_posts()) : $query->the_post();
I guess it should be like this (according to docs at http://codex.wordpress.org/Class_Reference/WP_Query) :
array(
'key' => 'assigned_page',
'value' => $string_page,
)

Categories