I have Wordpress website that displays only one loop on the homepage.
Today I noticed that the loop returns a duplicate of a particular post.
I checked the database and there is only ONE physical presence of a post in a database.
this is how I define the loop parameters:
$args = array(
'posts_per_page' => 65,
'ignore_sticky_posts' => 1,
'paged' => $paged,
'orderby' => 'date',
'order' => 'DESC',
'post_type'=>'post'
);
query_posts($args);
If I put ID instead of "date" in the orderby clause, the problem does not exist.
But I need it to be ordered by date.
Any ideas why this is happening? I know this happens usually when we have two or more loops.
Thank you!
Try this code
<?php
wp_reset_query();
?>
<?php
if ($allposts->have_posts()):
while ($allposts->have_posts()):
$allposts->the_post();
the_title();
endwhile;
endif;
?>
Related
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/
I'm using this to list all post titles...
<?php
$args = array(
'posts_per_page' => -1,
'post_type' => 'post',
'post_status' => 'publish',
'orderby' => 'title',
'order' => 'ASC'
);
query_posts($args);
if (have_posts()) :
while (have_posts()) :
the_post(); ?>
<p>
<?php the_title(); ?>
</p>
<?php endwhile;
endif;
wp_reset_query();
?>
...and that works fine.
Each of my posts also has a custom field called start_date. All these start dates are entered in the format DD-MM-YYYY (example: 10-03-2016).
What I need to be able to do (and I'm not if this can even be done) is to orderby start_date (instead of title).
To further illustrate, if there were 3 posts with start dates like this...
Post 1: 01-11-2016
Post 2: 03-01-2016
Post 3: 12-07-2016
...the posts would be displayed in the order of....
Post 2
Post 3
Post 1
...because this is in correct date order.
I hope this makes sense.
Cheers.
In wordpress WP_Query class accept args provide more helpful for post filter.
You can check in official site documentation provide custom meta fields accept in query args in order and orderby.
https://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters
I have two methods handle in this situation.
Your date format in start date is not properly mysql date format(YYYY-MM-DD). If you date format will same as mysql format so you need only change query args like:
$args = array(
'posts_per_page' => -1,
'post_type' => 'post',
'meta_key' => 'start_date',
'post_status' => 'publish',
'orderby' => 'meta_value',
'meta_type' => 'DATE',
'order' => 'ASC'
);
If you do not want to change your date format on every each post or you have many post as difficult to change date format on each post so you can add filter to override order parameters.
Complete code
<?php
$args = array(
'posts_per_page' => -1,
'post_type' => 'post',
'post_status' => 'publish',
'meta_key' => 'start_date',
'orderby' => 'title',
'order' => 'ASC'
);
add_filter( 'posts_orderby', function($orderby){
return 'STR_TO_DATE( wp_postmeta.meta_value, "%d-%m-%Y") ASC';
});
query_posts($args);
if (have_posts()) :
while (have_posts()) :
the_post(); ?>
<p>
<?php the_title(); ?>
</p>
<?php endwhile;
endif;
wp_reset_query();
?>
Hope this helpful for you sorry for poor English.
You can Query and sort posts by custom field value by just adding 'orderby' => 'name_of_your custom_field' to your WP_Query Statement.
<?php
$args = array(
'posts_per_page' => -1,
'post_type' => 'post',
'post_status' => 'publish',
'orderby' => 'name_of_your_custom_field',
'order' => 'ASC'
);
Please bear in mind that for example for a date field, the dates must be in "Ymd" format. (2022/12/09).
Hope somebody finds it helpful.
I'm attempting to display:
Linked Category Titles
Category Descriptions (trimmed to custom length)
Category "Readmore Styled Link
A lists of Post Titles WITHIN that category, linking to the posts
I've accomplished tasks 1-3 with a Foreach loop, but I can't seem to display posts titles WITHIN each category through methods I've used in the past. The main thing is that I can't figure out how to run get_posts(); or a function like it with a variable in the $args array.
I feel this really needs to be done with a foreach loop, as I'm working with 20+ Categories. I've tried mixing / matching with a 3rd party shortcode plug-in, but due to "order of wp operations" that idea failed as well :( Any help would be EXTREMELY appreciated as I've been spinning my wheels for the past 3-4 hours.
Source Code →
http://pastebin.com/Mm9u27dF
Code Output:
<p class="topic-link-heading">Understanding Democratic Governance and Market Economy</p><p class="topic-list">There is an ongoing debate in academic circles and among practitioners on the linkages between democratic governance and market economies. It has intensified in light of transitions taking place after the fall of the Berlin Wall. Amidst expectations that all … <a href="http://localhost:81/wordpress/?cat=3" > Topic Overview →</a></p>3
Please Note
The "3" is only being displayed to show that the category ID variable is outputting correctly
From what I understand, you want to display the posts for a specific category and you're having trouble doing it.
You're looping through the categories and I think you just need to use the query_posts function to query the relevant posts with the specific category (I've taken the code from the official documentation):
<?php
$post_args = array(
'posts_per_page' => 5,
'offset' => 0,
'category' => $category->term_id, //in your case.
'orderby' => 'post_date',
'order' => 'DESC',
'include' => '',
'exclude' => '',
'meta_key' => '',
'meta_value' => '',
'post_type' => 'post',
'post_mime_type' => '',
'post_parent' => '',
'post_status' => 'publish',
'suppress_filters' => true );
// The Query
query_posts( $post_args );
// The Loop
while ( have_posts() ) : the_post();
echo '<li>';
the_title();
echo '</li>';
endwhile;
// Reset Query
wp_reset_query();
?>
To check if the posts are getting queried or not do something like this:
$relevant_posts = query_posts( $post_args );
print_r($relevant_posts); //Should print an associated array with the posts.
So you could populate the args variable with the specific category that you're fetching within the for loop and then just query the posts. Once you have the posts, you can refer to the links from easily. This documentation also might come in handy while looping through the posts.
I'm having some trouble displaying a list of posts from a Wordpress Category that will exclude a certain number of post based on a custom field using Advance Custom Fields.
Here's the current code I'm using that hides it nicely:
while ( have_posts() ) : the_post();
$is_taken = get_field('taken_check', $this_id);
if ($is_taken!=1) {
get_template_part( 'basket_selection' );
}
endwhile;
However, it simply just hides the post but still considers it as a post on the "posts_per_page" function.
For example, There are 20 posts in total and I've set the limit to 10 posts per page. If I hide 3 posts with the code above, it will only display 7 posts in page 1 and 10 posts in page 2.
Is there a way to simply just ignore the hidden posts and not count it as a "post"?
Try this:
Apply Custom Fields Parameters in get_post query itself.
$posts = get_posts(array(
'posts_per_page' => 10,
'post_type' => '<YOUR_POST_TYP>',
'meta_key' => 'taken_check',
'meta_value' => '<DEFAULT_VALUE_OF_taken_check>'
));
Lots to read here: http://codex.wordpress.org/Template_Tags/get_posts
I've managed to solve it by changing the get_posts to wp_query within the category.php.
I first added this code to detect the current category viewed and filter the query to only display taken_check = 0.
$this_cat = get_category(get_query_var('cat'), 'ARRAY_A', false);
foreach ($this_cat as $this_cat){
$this_catid = $this_cat;
break;
}
$args = array(
'posts_per_page' => 10,
'post_type' => 'post',
'cat' => $this_catid,
'orderby' => 'title',
'order' => 'ASC',
'paged' => $paged,
'meta_query' => array(
array(
'key' => 'taken_check',
'value' => '0',
)
)
);
$wp_query = new WP_Query($args);
I then just continued with the default loop sequence. The only weird code is the unnecessary foreach loop to detect the current category based on the current page and not from a post. Still puzzled as to why I can't just use $this_cat[0] since it's an array. It keep returning blank.
Oh well, but it works now with pagination, so I'm happy :)
Thanks for all the help!
I have a homepage with four displayed posts and one that is emphasized.
The one that is emphasized is not a problem, it's a large post whose details i collect using special loop.
But for those four posts (that have pagination), I just can't seem to exclude that emphasized one.
For example, if emphasized post has ID of 8, this should do the trick:
$args=array(
'paged' => $paged,
'posts_per_page' => 4,
array('post__not_in' => array(8))
);
query_posts($args);
while ( have_posts() ) : the_post();
echo '<li>';
the_title();
echo "<span> ".$post->ID."</span>";
echo '</li>';
endwhile;
But for some reason it's not filtering anything, always displays all the posts.
Any ideas why this is happening?
Why is the post__not_in in another array? I would recommend putting it on the same level:
$args=array(
'paged' => $paged,
'posts_per_page' => 4,
'post__not_in' => array(8)
);
If that doesn't help, I would recommend checking approaches mentioned here.