Wordpress Echo post count - php

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;

Related

Get page content by slug or title wordpress

I am creating a blog and I have difficulty with pages in wordpress, in my case I would like to insert content (raw content) from two pages randomly in modal system while people browse the blog.
Example:
Current Page> Modal Box> Page01 content or Page02 Content
I know it's possible by the page id. but I would like to make it more dynamic by getting the contents of the pages by slugs or by title.
here's the code
<?php
$term = get_taxonomy( $slug );
$args = array(
'post_type' => 'page',
'posts_per_page' => 1,
'orderby' => 'RAND',
'tax_query' => array( array(
'taxonomy' => $term,
'field' => 'slug',
'terms' => array('page-01', 'page-02'),
) )
);
$rand = new WP_Query($args);
if ($rand->have_posts()) {
while ($rand->have_posts()) {
$rand->the_post();
the_content();
}
}
?>
I know that removing taxonomy from query gets the contents of publish pages but, in my case, I need to get content from two specific pages
You can use post_name__in to filter by multiple slugs, eg:
$args = array(
'post_name__in' => array('page-slug-1', 'page-slug-2'),
'post_type' => 'page',
'posts_per_page' => 1,
'orderby' => 'RAND',
);
You can find more details on the WP_Query documentation page.

How to query taxonomy for a post type then include another post type with no tax

How do I include these post types on a single query?
events -> all
guides -> all
page -> only topnews tax
This is the query I use to list posts from events and guides custom post type:
$args = array(
'post_type' => array('events', 'guides'),
'orderby' => 'date',
'order' => 'DESC',
'posts_per_page' => 5,
'post_status' =>'publish',
);
$newlist = get_posts( $args );
Now, I want to add post type page with a tax_query of topnews on the $args. I already registered taxonomy called property for page post type, and topnews is one of its categories.
'tax_query' => array(
array (
'taxonomy' => 'property',
'field' => 'slug',
'terms' => 'topnews',
)
),
I want to include this on the query but the events and guides post types won't show any results. Only page with topnews tax is showing.
How do I include all of this on a single query?
use
$query1 = WP_Query(); // query without taxonomy
$query2 = WP_Query(); // query with taxonomy
$q1_posts = $query1->posts;
$q2_posts = $query2->posts;
$merged_posts = array_merge($q1_posts,$q2_posts);

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;

Show a post from selected categories wordpress

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.

WP Query to get posts with specific term_taxonomy

In my wordpress installation, I have a custom taxonomy event-categories which is mapped to custom post type event.
In my single post display page, I need to list all posts which is posted in same event-categories of the current post. How can I write a wp query for this?
Screen-shot for my custom taxonomy in custom post.
Now tried like this get_the_terms(the_ID(), 'event-categories').
So I got all term_taxonomy_ids related to the single post. Next how can I get all posts which have these term_taxonomy_id.
This would be about the most basic query to solve your problem.
$term_tax_ids = get_the_terms(get_the_ID(), 'event-categories');
$terms = array();
foreach($term_tax_ids as $term_tax_id) {
array_push($terms, $term_tax_id->term_id);
}
$args = array(
'post_type' => 'event',
'posts_per_page' => 5,
'tax_query' => array(
array(
'taxonomy' => 'event-categories',
'field' => 'id',
'terms' => $terms,
'operator' => 'IN',
)
)
);
$query = new WP_Query( $args );
while ( $query->have_posts() ) {
$query->the_post();
echo '<div class="related_single">' . get_the_post_thumbnail();
echo '<div class="related_title">' . get_the_title() . '</div></div>';
}
You should really read the Codex, it has literally everything you could ever want to know about queries

Categories