I am trying to display last couple of post title in a comma separated value with below code.Here I am making the query from wp_post table.Now How can I get the category in this code as there is no column for category in wp_posttable. below is my code
<?php
$posts = $wpdb->get_col("
SELECT $wpdb->posts.*
FROM $wpdb->posts
WHERE post_status = 'publish'
and post_type='post'
ORDER BY post_date DESC LIMIT 10");
$the_posts = array();
foreach($posts as $post) :
echo implode( ', ', $the_posts );
?>
You can do it this way:
1 - get posts (correct way for wp)
<?php
$args = array(
'posts_per_page' => 10,
'orderby' => 'date',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish',
'suppress_filters' => true
);
$posts_array = get_posts( $args ); ?>
2 - loop the posts and get categories
foreach($posts_array as $post):setup_postdata($post);
$category = get_the_category();
var_dump($category);
endforeach;
wp_reset_postdata();
Related
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 );
Same as title: I need make Wordpress loop that will display posts sorted by comments date.
Like on forum, first is post with the latest comment.
It is interesting. There might be two approached which can resolve this.
1 -
$args = array(
'status' => 'approve',
'order' => 'DESC'
);
$comments = get_comments( $args );
$ids = array();
foreach ( $comments as $comment ) {
$post = get_post( $comment->comment_post_ID );
if(!in_array($post->ID, $ids):
$ids[] = $post->ID;
echo $post->title;
endif;
}
2 -
Add a action to add a meta to the post for the recent comment date.
add_action('comment_unapproved_to_approved', 'wpc_2511_comment_approved');
function wpc_2511_comment_approved($comment) {
$comment_post_ID = $comment->comment_post_ID;
$date = $comment->comment_date;
update_post_meta( $comment_post_ID, '_recent_comment_date', $date );
}
Then write the query string to get the posts sorted by meta_key & meta_value Descending
Like:
$args = array(
'post_status' => 'publish',
'post_type' => 'post'
'meta_key' => '_recent_comment_date',
'orderby' => 'meta_value_num',
'order' => 'DESC'
);
Hope this will help you :)
I am trying to show all posts that contain a number.
I am using this code :
global $wpdb;
$postids = $wpdb->get_col($wpdb->prepare("
SELECT ID
FROM $wpdb->posts
WHERE SUBSTR($wpdb->posts.post_title,1,1) = %s
ORDER BY $wpdb->posts.post_title",REGEXP ^[0-9]));
if ($postids) {
$args=array(
'post__in' => $postids,
'post_type' => 'shows',
'post_status' => 'publish',
'posts_per_page' => -1,
'caller_get_posts'=> 1
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
echo 'List of Posts Titles beginning with the letter '. $_GET['letter'];
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<p><?php the_title(); ?></p>
<?php
endwhile;
}
wp_reset_query();
}
I have tried this but it is not working it just 1 post, but when i change it to show for example posts that start with a it show all of them.
RLIKE ^[0-9] [0-9]%
Thanks.
To fetch only records starting with number, filter them using REGEXP
SELECT ID
FROM $wpdb->posts
WHERE $wpdb->posts.post_title REGEXP '^[0-9]+'
ORDER BY $wpdb->posts.post_title;
I'm using switch_to_blog to get all the posts from the blog_id. Is it possible to get all posts from other sites?
Also I've tried to use foreach as mention on the codex documentation.
This is what I've tried...
global $wpdb, $paged, $blog_id;
$site_blog_ids = $wpdb->get_results($wpdb->prepare("SELECT blog_id FROM ".$wpdb->prefix."blogs where blog_id > 1")); // get all subsite blog ids
foreach($site_blog_ids as $bids){
echo '<pre>';
print_r($bids->blog_id);
echo '</pre>';
switch_to_blog($bids->blog_id);
restore_current_blog();
}
$video_args = array (
'post_type' => 'video',
'post_status' => 'publish',
'posts_per_page' => -1,
'paged' => $paged,
);
$video_query = new WP_Query( $video_args );
but still no luck..
I get it. I just need to insert my WP_Query on foreach..
global $wpdb, $paged, $blog_id;
$site_blog_ids = $wpdb->get_results($wpdb->prepare("SELECT blog_id FROM ".$wpdb->prefix."blogs where blog_id > 1")); // get all subsite blog ids
foreach($site_blog_ids as $bids) {
echo '<pre>';
print_r($bids->blog_id);
echo '</pre>';
switch_to_blog($bids->blog_id);
restore_current_blog();
$video_args = array (
'post_type' => 'video',
'post_status' => 'publish',
'posts_per_page' => -1,
'paged' => $paged,
);
$video_query = new WP_Query( $video_args );
}
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();