How to get all video posts by specific user id - php

I need videos posted by any specific user.
I have used the following code but it is still showing all the posts
$args = array ( 'post_author' => $id, 'post_type' => 'videos', 'posts_per_page' => 100,);
$posts_array = get_posts( $args );

You can use the WP_Query like this.
$query = new WP_Query( 'author=' . $id . '&post_type=video&posts_per_page=100');
while($query->have_posts()) {
the_post();
// Do you magic here
}

Related

Wordpress WP_Query $args, show published posts AND the current user's pending posts

On my Wordpress website users can write their own posts on the group wall. I want to display all published posts on the group wall. I also want to display pending posts to the current user who is the author of these posts. Right now when the user submits a new post, there is no information displayed about it, they may not know if their post got submitted. I want to show them their pending post with the information that they need to wait for the post review.
Is there a way to add the logic to the $args of the WP_Query? This is my code right now:
//number of posts per page default
$num =5;
//page number
$paged = $_POST['page'] + 1;
$current_user = wp_get_current_user();
$uid = $current_user->ID;
//args
$meta_query = array();
$meta_query[] = array('key' => 'pm_accessible_groups','value' => sprintf(':"%s";',1),'compare' => 'like');
$args = array(
'post_type' => 'pg_groupwalls',
'post_status' => 'publish',
'posts_per_page' =>$num,
'meta_query' => $meta_query,
'paged'=>$paged
);
//query
$query = new WP_Query($args);
//check
if ($query->have_posts()):
//loop
while ($query->have_posts()): $query->the_post();
And later I have my template. This way it is only showing published posts as of now. I tried to find some information about it here: https://developer.wordpress.org/reference/classes/wp_query/ , and later I tried changing the $args array to something like this:
//number of posts per page default
$num =5;
//page number
$paged = $_POST['page'] + 1;
$current_user = wp_get_current_user();
$uid = $current_user->ID;
//args
$meta_query = array();
$meta_query[] = array('key' => 'pm_accessible_groups','value' => sprintf(':"%s";',1),'compare' => 'like');
$args = array(
'post_type' => 'pg_groupwalls',
'post_status' => array (
'relation' => 'AND',
array(
'post_status' => 'publish',
),
array(
'post_status' => 'pending',
'author' => $uid,
),
),
'posts_per_page' =>$num,
'meta_query' => $meta_query,
'paged'=>$paged
);
//query
$query = new WP_Query($args);
//check
if ($query->have_posts()):
//loop
while ($query->have_posts()): $query->the_post();
It is most probably not the way to do this, and obviously, it does not work the way I want. It shows both published and pending posts to all users (not just the current user who is an author). I could maybe create a new WP_Query, but this one is already paginated and showing 5 posts per page with a "Load More" button, I don't want to break the pagination.
Is there a way to create this logic using $args array? Or any other way to achieve what I want?
Thank you in advance.
If there is more information needed or more of the code, I will try to update it.
Try below code.
//number of posts per page default
$num = 5;
//page number
$paged = $_POST['page'] + 1;
$current_user = wp_get_current_user();
$uid = $current_user->ID;
//args
$meta_query = array();
$meta_query[] = array(
'key' => 'pm_accessible_groups',
'value' => sprintf(':"%s";',1),
'compare' => 'like'
);
For all publish post.
$publish_args = array(
'post_type' => 'pg_groupwalls',
'post_status' => array( 'publish' ),
'posts_per_page' => $num,
'meta_query' => $meta_query,
'paged' => $paged
);
//query
$publish_posts = new WP_Query( $publish_args );
//check
if ( $publish_posts->have_posts() ):
//loop
while ($publish_posts->have_posts()): $publish_posts->the_post();
endwhile;
wp_reset_postdata();
endif;
For all pending posts of current user.
$pending_args = array(
'post_type' => 'pg_groupwalls',
'post_status' => array( 'pending' ),
'post_author' => $uid,
'posts_per_page' => $num,
'meta_query' => $meta_query,
'paged' => $paged
);
//query
$pending_posts = new WP_Query( $pending_args );
//check
if ( $pending_posts->have_posts() ):
//loop
while ($pending_posts->have_posts()): $pending_posts->the_post();
endwhile;
wp_reset_postdata();
endif;

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

Limit posts per page after using array_merge in Wordpress

I have two post types I want to display, Posts and then a Custom Post Type called 'Notes'. I want to query both of these and display them together. I've currently got it working using array_merge.
I want to create a new query so I can choose how many posts to display per page and also get pagination working. I've tried various different things to limit the amount of posts displayed but can't seem to crack it.
Here is my code:
$q1_args = array(
'post_type' => 'post',
'posts_per_page' => -1,
'post_status' => 'publish'
);
$q1_posts = get_posts( $q1_args );
// get the posts for the second query
$q2_args = array(
'post_type' => 'notes',
'posts_per_page' => -1,
'post_status' => 'publish'
);
$q2_posts= get_posts( $q2_args );
// Merge the post arrays together, and sort by date using the order_by_date function
$final_posts = array_merge( $q1_posts, $q2_posts );
usort( $final_posts, 'order_by_date' );
// Loop over the posts and use setup_postdata to format for template tag usage
foreach ( $final_posts as $key => $post ) {
$post_type = $post->post_type;
setup_postdata( $post );
//DO STUFF
}
Any thoughts on how I can limit posts per page and get pagination working?
Is there any particular reason this can't be done like this?
$args=array(
'post_type' => array('post', 'notes'),
'posts_per_page' => 15, //or any other number you want per page
'post_status' => 'publish',
'orderby' => 'date',
'order' => 'DESC',
'paged' => (( get_query_var('page') ) ? get_query_var('page') : 1)
);
$posts=get_posts($args);
if ($posts->have_posts())
{
while ($posts->have_posts())
{
$posts->the_post();
//DO STUFF
}
//add pagination here
}
else
{
// no posts found
}
wp_reset_postdata();

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

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