WP Query comment count - php

I use WP_Query to get posts by comment count
$aArgs = [
'posts_per_page' => 20,
'post_status' => 'publish',
'post_type' => ['post'],
'paged' => 1,
'orderby' => 'comment_count'
];
$oQuery = new \WP_Query($aArgs);
All posts that have comment count greater than 0 are ordered correctly. But in some posts(have 0 comment) display in at least 2 pages(Ex:paged=1 and paged=3). I dont know why. Please tell me how to fix this problem?

As I can't see your loop, I can't really guess what is going on... The following script should enable you to loop through posts via the comments count, I just tested it, and it's working on my end.
If I had to guess what is wrong with your script I would say that you probably didn't reset the post data wp_reset_postdata(); which is probably causing some troubleshoot between pages.
<?php
$query = new WP_Query( array(
'post_type' => 'post',
'posts_per_page' => 20,
'orderby' => 'comment_count'
) );
if( $query->have_posts() ):
while( $query->have_posts() ):
$query->the_post();
echo the_title() . '<br/>';
endwhile;
endif;
wp_reset_postdata(); ?>
References
Learn more about wp_reset_postdata # https://developer.wordpress.org/reference/functions/wp_reset_postdata/

Related

WP Sorting custom posts alphabetically by Title

Newbie here...
I have a custom post type of 'equipe' (team in portuguese). I am trying to sort these alphabetically by post title then display the_title so we have a alphabetical list of names.
I've done a search on here and tried a few fixes but Im struggling to get anything other that the standard order.
Any help would be much appreciated!
<?php
$args = array('orderby'=> 'title', 'order' => 'ASC', 'post_type' => 'equipe', 'posts_per_page' => -1, 'post_status' => 'publish' );
$q = new WP_Query($args);
while ( $q->have_posts() ) : $q->the_post();
?>
<h3><?php the_title(); ?></h3>
<?php
endwhile;
wp_reset_query();
?>
<?php
$args = array( 'post_type' => 'equipe', 'posts_per_page'=>5, 'orderby'=>'post_title','order'=>'ASC');
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
?>
RESOLVED:
Ok the reason it was enforcing menu_order was because of a setting (F*ing checkbox) within the plugin Post Types Order.
I needed to un-check AUTO SORT
and check Use query ASC / DESC parameter
This then allowed me to adjust the array as follow (and discussed above):
$args = array('orderby' => 'title', 'order'=>'ASC', 'post_type' => 'equipe')
However I did need to add 'order'=>'ASC' into the other pages that sorted by the original query of menu_order.

Add meta data from function for orderby in wordpress

Newbee here
I need my post to be orderby by the number of votes from a plugin named wp ulike. but to get the number of votes, I need to run a function. I've tried every way I can think but didn't work.
currently here's my code:
<?php
function votecount(){
if (function_exists('wp_ulike_get_post_likes')):
echo wp_ulike_get_post_likes(get_the_ID());
endif;
}
add_post_meta($post_id, 'votecount', $votecount);
?>
<?php
$ctr = 1;
$args = array(
'post_type' => 'ico',
'posts_per_page' => -1,
'meta_key' => 'votecount',
'orderby' => 'meta_value',
'order' => 'DESC'
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
?>
Any help will be much appreciated.
You have to add filter posts_orderby. Please take reference from below url.
https://wordpress.stackexchange.com/questions/198610/how-to-filter-by-last-name-for-custom-post/198624#198624

WP Offset Not Working with Foreach loop

I'm currently working a wordpress loop to retrieve blog posts, their titles, featured image, date and category. With that said, I'm attempting to offset the loop begin on the 5th descending post because the previous 4 are referenced earlier on the page.
I have successfully offset the posts but it seems that I can't grab the category.
<?php
$post_args = array(
'post_type' => 'post',
'post_status' => 'publish',
'order' => 'DESC',
'offset' => 4
);
$post_query = new WP_Query($post_args);
if ($post_query->have_posts() ):
$count = 1;
$terms = get_terms( array(
'taxonomy' => 'category',
'hide_empty' => true
) );
while ( $post_query->have_posts() ) : $post_query->the_post();
$feat_img = wp_get_attachment_url( get_post_thumbnail_id() );
?>
<div class="col-sm-3 col-xs-6">
<div class="featured-img" style="background-image: url(<?php echo $feat_img; ?>)"
<?php the_date('F j Y', '<h6>', '</h6>'); ?>
<h3><?php the_title(); ?></h3>
<div class="category"><?php echo $terms->name; ?></div>
</div>
</div>
I tried a slightly different approach and was able to get each posts category using a foreach loop, followed by a while and if loop. While I successfully got each posts category, the offset wasn't cooperating. Perhaps I'm overthinking it. Here's my other attempt at this.
<?php
$terms = get_terms( array(
'taxonomy' => 'category',
'hide_empty' => true,
) );
$count = 1;
foreach ( $terms as $term ) :
$post_args = array(
'offset' => 4,
'post_type' => 'post',
'order' => 'DESC',
'post_status' => 'publish',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => $term->slug
)
),
);
$post_query = null;
$post_query = new WP_Query($post_args);
if ( $post_query->have_posts() ) :
while ($post_query->have_posts() ) : $post_query->the_post();
$feat_img = wp_get_attachment_url( get_post_thumbnail_id() );
?>
Anyone mind lending a hand to help accomplish both tasks? Any input would be greatly appreciated. Thank you in advance.
you need to set "posts_per_page" to some other value than -1, it's explained well in documentation
https://codex.wordpress.org/Class_Reference/WP_Query
posts_per_page (int) - number of post to show per page (available
since Version 2.1, replaced showposts parameter). Use
'posts_per_page'=>-1 to show all posts (the 'offset' parameter is
ignored with a -1 value). Set the 'paged' parameter if pagination is
off after using this parameter. Note: if the query is in a feed,
wordpress overwrites this parameter with the stored 'posts_per_rss'
option. To reimpose the limit, try using the 'post_limits' filter, or
filter 'pre_option_posts_per_rss' and return -1

Post order = rand - not working in one category (wp_query)

I've got problem with display random post.
I use this loop:
<?php
remove_all_filters('posts_orderby');
$loop = new WP_Query( array( orderby => 'rand', 'cat' => '259', 'posts_per_page' => 1 ) ); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
Loop doesn't work in one category. Why? In the others everything is ok.
You have a typo:
<?php
remove_all_filters('posts_orderby');
$loop = new WP_Query( array(
'orderby' => 'rand',
'cat' => 259,
'posts_per_page' => 1
));
while ( $loop->have_posts() ) : $loop->the_post(); ?>
you've set orderby without '' quotes. Also remove them from the category id, this needs to be a number (integer), not a string.

Wordpress: WP_Query how to apply search criteria with custom post type

I have a custom post type, photo, and need to search for photos matching the title or description with the search keyword with various criteria: contains LIKE %$search_term%, starts with LIKE $search_term% etc. I have the following query, but this doesn't filter records according to $search_term. Please direct me to the right direction to embed this requirement with this query.
$search_term = $_GET['term'];
$search_criteria = $_GET['type'];
$loop = new WP_Query( array(
'post_type' => 'photo',
'posts_per_page' => 12,
'orderby'=> 'post_date'
));
Please be nice with me, I am a newbie in Wordpress and don't even know if I am asking a foolish question. But I am really stuck with it and need a solution. Any help will be appreciated a lot. Thank you everybody.
Add the "s" key to your existing arguments array:
$loop = new WP_Query( array(
'post_type' => 'photo',
'posts_per_page' => 12,
'orderby' => 'post_date',
's' => 'search_term'
));
Documentation can be found at: http://codex.wordpress.org/Class_Reference/WP_Query#Search_Parameter
Pass your search string here example like this ( 's'=>'test' )
<?php
/*pass your search string here example like this ( 's'=>'test' ) */
$args=array('s'=>'test','order'=> 'DESC', 'posts_per_page'=>get_option('posts_per_page'));
$query=new WP_Query($args);
if( $query->have_posts()):
while( $query->have_posts()): $query->the_post();
{
echo $post->post_title;
echo $post->post_content;
}
endwhile;
else:
endif;
?>

Categories