WordPress post count : X out of Y? - php

I'm working on a theme with CPT.
In single-cpt.php, I want to show the post number out of the total posts.
I managed to show the total ok posts using :
<?php $args = array(
'post_type' => 'cpt_type',
'post_status' => 'published',
'numberposts' => -1
);
echo $num = count( get_posts( $args ) ); ?>
and it returns the total of posts for that CPT type. So, on each single cpt, I got the total number.
What I would like, is to have "post number" / "total posts". Example : 7/21, and when I go o the following post : 8/21 and so on...
So, how could I get each single post to have a "number"?
Thanks!

Paste this in functions.php:
function get_total_number_of_posts( $post ) {
$posts = get_posts( array(
'post_type' => $post->post_type,
'posts_per_page' => -1
) );
return count( $posts );
}
function get_current_post_index( $post ) {
$posts = get_posts( array(
'post_type' => $post->post_type,
'posts_per_page' => -1
) );
$index = 0;
foreach ( $posts as $p ) {
$index++;
if ( $p->ID === $post->ID ) {
break;
}
}
return $index;
}
and then in your single.php file:
global $post;
$total_posts = get_total_number_of_posts( $post );
$current_post = get_current_post_index( $post );
echo "You are viewing {$current_post} out of {$total_posts}";

Related

Total count of advanced custom field values in WordPress

I'm trying to get the total count of a value of a custom field for WooCommerce products. My products are stock of shoes, so for each product I create a custom field for the total pairs quantity. Now I need to display in a new page the total count of pairs.
I tried with this code but the result is always 0
<?php
$pair_total = 0; //my main variable
$posts = get_posts(array(
'posts_per_page' => -1, //get all post
'post_type' => 'product' //my custom post type
));
if( $posts ) {
foreach( $posts as $post ){
setup_postdata( $post );
if (get_field('n_totale')) {
echo get_field('n_totale'); //show individual field(not needed)
$pair_total = get_field('n_totale') + $pair_total; //sum all fields
}
}
wp_reset_postdata();
}?>
<?php echo '<h1>'.$pair_total.'</h1>'//showing the total value;
?>
What can I try next?
check get_field() Try below code.
<?php
$pair_total = 0; //my main variable
$posts = get_posts(array(
'posts_per_page' => -1, //get all post
'post_type' => 'product' //my custom post type
));
if( $posts ) {
foreach( $posts as $post ){
setup_postdata( $post );
if ( get_field( 'n_totale', get_the_ID() ) ) {
$pair_total = $pair_total + (int) get_field('n_totale', get_the_ID() ); //sum all fields
}
}
wp_reset_postdata();
}
echo '<h1>'.$pair_total.'</h1>'//showing the total value;
?>
I solved with this code:
$pair_total_1 = 0; //my main variable
$posts = get_posts(array(
'numberposts' => -1, //get all post, previously was post_per_page
'post_type' => 'product', //my custom post type
'post_status' => 'publish',
'meta_query' => array(
array(
'key' => '_stock_status',
'value' => 'instock',
'compare' => '=',
)
) //I add a query to count only in stock product
));
if( $posts ) {
global $post;
foreach( $posts as $post ){
setup_postdata( $post);
if ( get_field( 'n_totale', get_the_ID() ) ) {
$pair_total_1 = $pair_total_1 + (int) get_field('n_totale', get_the_ID() );
//sum all fields
}
}
wp_reset_postdata();
}
echo '<p>Totale paia disponibili: '.$pair_total_1.'</p>';}//showing the total value
Thanks to all

Wordpress: Show three random posts on every page refresh

In my WordPress, I am able to show one random post by using the below code:
global $post;
if ( post_type_exists( 'testimonial' ) ) {
$testimonial_query = new WP_Query( array(
'post_type' => 'testimonial',
'orderby' => 'rand',
'posts_per_page' => -1
) );
if ( $testimonial_query->have_posts() ) {
$random_int = rand( 0, $testimonial_query->post_count - 1 );
$post = $testimonial_query->posts[$random_int];
setup_postdata( $post );
// do something with post - e.g. the_excerpt(), the_content(), etc.
}
// Restore original post data
wp_reset_postdata();
}
Ref: https://barn2.co.uk/how-to-display-a-random-post-in-wordpress/
Help me to show 3 random posts by using the above method.
Can you try using in array property or WP_Query. A sample code is given belwo
$results = $wpdb->get_results( "SELECT * FROM $wpdb->posts WHERE `post_type` = 'testimonial' ORDER BY RAND() LIMIT 3" );
$ids = array();
foreach($results as $$result){
$ids[] = $results->ID;
}
$args = array(
'post_type' => array( 'testimonial' ),
'orderby' => 'ASC',
'post__in' => $ids
);
$testimonial_query = new WP_Query( $args );

Wordpress: Display image attachments from child pages of a specific page

I am trying to display all image attachments on the child pages of a specific parent page, i.e. all the pictures on Pages 10, 11 and 12.
Projects (Page ID: 5)
- Project 1 (Page ID: 10)
- Project 2 (Page ID: 11)
- Project 3 (Page ID: 12)
This is what I have so far, and it works to display all images on the site:
<?php
$args = array(
'post_parent' => 0,
'post_type' => 'attachment',
'numberposts' => -1
);
$images = get_children( $args );
if ( empty($images) ) {
// no attachments here
} else {
foreach ( $images as $attachment_id => $attachment ) {
echo wp_get_attachment_image( $attachment_id, 'full' );
}
}
?>
However, if I add the post parent ID (5), nothing comes up:
<?php
$args = array(
'post_parent' => 5,
'post_type' => 'attachment',
'numberposts' => -1
);
$images = get_children( $args );
if ( empty($images) ) {
// no attachments here
} else {
foreach ( $images as $attachment_id => $attachment ) {
echo wp_get_attachment_image( $attachment_id, 'full' );
}
}
?>
Any suggestions would be really helpful!
I think get_children() return objects from a single parent post or all (passing 0 as value).
You may try a nested foreach to get all post children first, then query attachment page by page. Here is an untested sample, but it gives you an idea:
<?php
$subpages_args = array(
'post_parent' => 5,
'post_type' => 'page',
'numberposts' => -1
);
$sub_pages = get_children( $subpages_args );
foreach( $sub_pages as $subpage_id => $sub_page) {
$args = array(
'post_parent' => subpage_id,
'post_type' => 'attachment',
'numberposts' => -1
);
$images = get_children( $args );
if ( empty($images) ) {
// no attachments here
} else {
foreach ( $images as $attachment_id => $attachment ) {
echo wp_get_attachment_image( $attachment_id, 'full' );
}
}
}
?>
Good luck :)
Here is an SQL Query based solution. Copy following function in your theme's functions.php
function get_children_page_attachments($parent_id) {
global $wpdb;
// just a precautionary typecast and check
$parent_id = intval( $parent_id );
if( empty($parent_id) ) return [];
$query = "SELECT ID FROM {$wpdb->posts} P WHERE post_type='attachment' AND P.post_parent IN (SELECT ID FROM {$wpdb->posts} WHERE post_parent={$parent_id} AND post_type='page' )";
return $wpdb->get_results($query, 'ARRAY_A');
}
Once you have above function in your functions.php, then you can use it like this:
// for example the parent page id is 16
$image_ids = get_children_page_attachments(16);
foreach($image_ids as $image_id) {
$image = wp_get_attachment_image($image_id['ID'], 'full');
}

Why do I have WooCommerce orders, but no posts of type shop_order?

I have some 30 orders in my shop. I am trying to loop over all orders, but I can't retrieve any orders. Here's the code:
$args = array (
'post_type' => 'shop_order',
'posts_per_page' => - 1
);
$loop = new WP_Query($args);
while ($loop->have_posts()) {
// do some work here
}
The loop never runs. I tried printing a count of all post types:
$args = array (
'post_type' => 'any',
'posts_per_page' => - 1
);
$loop = new WP_Query($args);
$types = array();
while ($loop->have_posts()) {
$loop->the_post();
$post_id = get_the_ID();
$type = get_post_type($post_id);
if ($types[$type]) $types[$type]++;
else $types[$type] = 1;
}
foreach ($types as $type => $count) {
echo "{$type}: {$count} ";
}
This is printing product: 30 page: 5 post: 1, i.e. no shop_orders. I guess I'm missing something very obvious, but it's not so obvious to me what that obvious thing is!
UPDATE: I am now retrieving all orders with this code:
$args = array(
'post_type' => 'shop_order',
'posts_per_page' => -1
);
$posts = get_posts($args);
That doesn't answer the question though. But it's a solution.
use 'post_status' => 'wc-processing' or 'post_status' => 'any'

How to get all post from a post type in Wordpress

I try to get all product from database with function get_post_types,but i get an empty array:
<?php
$list=mysql_query("SELECT * FROM wp_posts where post_type='product'");
if (empty($list)) {
echo "NONE";
}
?>
Try with:
$products = get_posts(array(
'numberposts' => -1,
'post_type' => 'product',
));
Check that post_type name is correct.
This query will get all the post with type product, for get only the published ones, add:
'post_status' => 'publish'
To the array arguments.
From a post you can get a lot of information:
To get the post image:
$img = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
To get the category of a post:
$cats = get_the_category( $post->ID );
$cats is an array of terms.
you may use the standard WP_Query method instead:
$args = array( 'post_type' => 'product', 'posts_per_page' => -1 );
$product_query = new WP_Query( $args );
if( $product_query->have_posts() ){
while( $product_query->have_posts() ){
$product_query->the_post();
if( has_post_thumbnail() ){
the_post_thumbnail();
}
}
}

Categories