Wordpress: Show three random posts on every page refresh - php

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 );

Related

WP_Query does not filter results

Spent 3 days trying to get the WP_Query to work with Elementor, however had no luck so far. Elementor either refuses to process the query or does not filter the posts at all. Can somebody help me and point out some mistakes that I have in my code?
Context: the query looks for current WP user's enrollment records of post_type 'tutor_enrolled' in 'wpv1_posts' and collects IDs of user's enrolled courses into $course_ids[]. The query should then filter only the posts with IDs matching the $course_ids array, therefore displaying all of current user's courses on Elementor frontend.
function custom_query_callback( $query ) {
$current_user = wp_get_current_user();
$args = array(
'post_type' => 'tutor_enrolled',
'author' => $current_user->ID,
);
$enrollments = new WP_Query( $args );
if ( $enrollments->have_posts() ) {
$course_ids = array();
while ( $enrollments->have_posts() ) {
$enrollments->the_post();
$course_ids[] = get_post_field( 'post_parent' );
}
wp_reset_postdata();
$args = array(
'post_type' => 'courses',
'post__in' => $course_ids,
'posts_per_page' => -1,
'orderby' => 'post__in'
);
$query = new WP_Query ( $args );
}
}
}
add_action( 'elementor/query/custom_query_callback', 'custom_query_callback' );
I tried calling get_course_ids() and custom_query_callback() as two separate functions and trying to pass the $course_ids array in $query->set( 'post__in', $course_ids );.
Then I also moved the variables outside of the function, ending up with:
$current_user = wp_get_current_user();
$args = array(
'post_type' => 'tutor_enrolled',
'author' => $current_user->ID,
);
$enrollments = new WP_Query( $args );
if ( $enrollments->have_posts() ) {
$course_ids = array();
while ( $enrollments->have_posts() ) {
$enrollments->the_post();
$course_ids[] = get_post_field( 'post_parent' );
}
wp_reset_postdata();
}
add_action( 'elementor/query/custom_query_callback', function( $query ) {
$query->set( 'post__in', $course_ids );
});
None of the above resolved my issue and kept causing errors in Elementor.

WordPress Find ID of a Post with a specific meta value

I have about 25,000 posts here (and rising). All of them under a Custom Post Type "lead".
Each post has meta information, including a variable called "uniqid".
this uniqid is a url parameter. now i need the post id where exactly this uniqid exists.
Now my question is if there is a way to speed up this determination.
I have tried 2 ways once a wp_query and get_results.
I ask because this function that determines the post id is always noted by plugin "query monitor" that it is slow.
These are the 2 approaches, both work. I just wonder if it is possible to speed up here?
The WP_Query solution.
$post_id = false;
$args = array(
'posts_per_page' => -1,
'post_type' => 'lead',
'fields' => 'ids',
'orderby' => 'date',
'order' => 'ASC',
'post_status' => 'publish',
'meta_key' => 'uniqid',
'meta_value' => $uniqid
);
$query = new WP_Query( $args );
if( $query->have_posts() ) {
while( $query->have_posts() ) {
$query->the_post();
// Get Post ID for return
$post_id = get_the_id();
// Break loop, when matching id was found
$uniqidGPM = get_post_meta( $post_id, 'uniqid' , true );
if($uniqidGPM == $uniqid) {
$post_found = true;
break;
}
}
}
wp_reset_postdata();
return $post_id;
The select get_results solution.
$post_id = false;
global $wpdb;
$selectQuery = "SELECT wp_wkdm_posts.ID FROM wp_wkdm_posts INNER JOIN wp_wkdm_postmeta ON ( wp_wkdm_posts.ID = wp_wkdm_postmeta.post_id ) WHERE 1=1 AND ( ( wp_wkdm_postmeta.meta_key = 'uniqid' AND wp_wkdm_postmeta.meta_value = '".$uniqid."' )) AND wp_wkdm_posts.post_type = 'lead' AND ((wp_wkdm_posts.post_status = 'publish')) GROUP BY wp_wkdm_posts.ID ORDER BY wp_wkdm_posts.post_date ASC";
$selectQueryResult = $wpdb->get_results($selectQuery);
if (empty($selectQueryResult)) {
return $post_id;
}else{
$post_id = $selectQueryResult[0]->ID;
return $post_id;
};
Please use this meta_query condition on the same query, it helps you.
$args = array(
'post_type' => 'lead',
'posts_per_page' => -1,
'post_status' => 'publish',
'meta_query' => array(
'key' => 'uniqid',
'value' => 'YOUR_VALUE'
)
);
$query = new WP_Query($args);

WP post__not_in another query not filter

my first query is okay
$ids = [];
$novidades = get_posts( array(
'posts_per_page' => 4,
'meta_key' => 'meta-checkbox',
'meta_value' => 'yes'
) );
if ( count( $novidades ) ) {
foreach( $novidades as $novidade ) {
$ids[] = $novidade->ID;
}
}
//rest of my code is ok
but, i try post another post and ignore the first query, but don't work, list all post
$args2 = array(
'post_type' => 'post',
'posts__not_in' => $ids
);
$featured = new WP_Query($args2);
Can help me?
It's post__not_in. Remove the extra s from your code.
post__not_in (array) - use post ids. Specify post NOT to retrieve. If this is used in the same query as post__in, it will be ignored.
Your code should be:
$args2 = array(
'post_type' => 'post',
'post__not_in' => $ids,//<====extra 's' removed
);
$featured = new WP_Query($args2);

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'

Get all posts from Wordpress as JSON

I am trying ti get all the posts form my Wordpress website with the post_type "product".
I tried the below but it doesn't work.
<?php
$args = array( 'post_type' => 'product', 'post_status' => 'publish');
$loop = new WP_Query( $args );
$array = array();
while ( $loop->have_posts() ) : $loop->the_post();
global $product;
$array[] = array(
'id' => get_the_ID(),
'title' => get_the_title()
);
endwhile;
wp_reset_query();
ob_clean();
echo json_encode($array);
exit();
?>
Although when I add 'posts_per_page' => 450 to $args, it returns posts of the type, however if I add a value higher than 450 such as 500, it returns nothing again.
I am using this to loop through all product posts and add the name, price etc. to an array in the loop.
How do I fix the $args to get all the product posts.
EDIT:
I also recently tried:
<?php
$args="SELECT * FROM wp_posts WHERE wp_posts.`post_type` = 'product' AND wp_posts.`post_status` = 'publish'";
$loop = get_results( $args );
$array = array();
while ( $loop->have_posts() ) : $loop->the_post();
global $product;
$array[] = array(
'id' => get_the_ID(),
'title' => get_the_title()
);
endwhile;
// wp_reset_query();
ob_clean();
echo json_encode($array);
exit();
?>
No need to simulate the loop here. This is the most straightforward way:
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'nopaging' => true
);
$query = new WP_Query( $args ); // $query is the WP_Query Object
$posts = $query->get_posts(); // $posts contains the post objects
$output = array();
foreach( $posts as $post ) { // Pluck the id and title attributes
$output[] = array( 'id' => $post->ID, 'title' => $post->post_title );
}
echo json_encode( $output );
Or you can leave out the foreach and just echo json_encode( $posts ); to get all the properties of the posts.
please check your table for data. Is there any post of post type products. if yes then the simple query should work
SELECT * FROM $wpdb->posts WHERE $wpdb->posts.`post_type` = 'product' AND $wpdb->posts.`post_status` = 'publish'
you can run this code in phpmyadmin directly. and see if any post is there. and please replace $wpdb with the prefix of you tables.
<?php
//this file is in main folder and it works for me(yourWordpressWebsite.com/yourFile.php)
$path = $_SERVER['DOCUMENT_ROOT'];
include_once $path . '/wp-config.php';
include_once $path . '/wp-load.php';
include_once $path . '/wp-includes/wp-db.php';
include_once $path . '/wp-includes/pluggable.php';
global $wpdb;
$posts = $wpdb->get_results("SELECT ID, post_title FROM $wpdb->posts WHERE post_status = 'publish' AND post_type='post'");
echo json_encode($posts);
exit();
?>
output:
[{"ID":"1","post_title":"Hello world!"},{"ID":"63","post_title":"Blockquote Post"}...
This is how you show all posts in a query: 'posts_per_page' => -1 - so your query becomes:
$args = array( 'post_type' => 'product', 'posts_per_page' => -1 );
$loop = new WP_Query( $args );
As far as i know, you need to get posts with manual query,
Just like this,
global $wpdb;
$args = "SELECT * FROM $wpdb->posts WHERE $wpdb->posts.`post_type` = 'product' AND $wpdb->posts.`post_status` = 'publish' ORDER BY $wpdb->posts.`ID`";
$loop = $wpdb->get_results( $args );
In my experience, 'posts_per_page' => -1 will not returns all the posts, i don't know why.
Somehow wordpress not returning more than 500 or 1000 posts from database...
You might try using a plugin developed for such a purpose. This one is listed on the WordPress.org website, and it seems like it may help you.
JSON API
It's always best to check the WordPress.org hosted plugins first
$args = array( 'posts_per_page' => -1, 'post_type' => 'product' );
$myposts = get_posts( $args );
echo json_encode($myposts);
I think this should work

Categories