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.
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/
Apologies if this was already asked, but I DID search for it and found nothing. I'm trying to output the child pages of more than one page ID in the same list and also only output 6 of those pages in an assorted way. In other words, I want to get the child pages of more than one page and mix the results in the output.
Right now I'm using:
<?php
$nbpages = 6;
$pages = wp_list_pages('title_li=&sort_column=post_name&child_of=1041');
$pages_arr = explode("\n", $pages);
for($i=0;$i<$nbpages;$i++){
echo $pages_arr[$i];
}
?>
Which is fine for getting the child pages of one ID, but I can't figure out how to add an array to get more than one, then mix the results. Any help is appreciated!
You should switch over to using WP_Query, which allows to specify a post_parent__in parameter. This parameter accepts an array of page or post IDs. Note: you need to run WP 3.6 or higher for this.
$so26932595_query = new WP_Query( array(
'posts_per_page' => 4,
'post_type' => array( 'page' ),
'orderby' => 'rand',
'order' => 'none',
'post_parent__in' => array( 1043, 1045, 1047, 1049, 1591 )
) );
while ( $so26932595_query->have_posts() ) :
$so26932595_query->the_post();
echo '' . get_the_title() . '';
endwhile;
wp_reset_postdata();
Link: http://codex.wordpress.org/Class_Reference/WP_Query
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 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;
?>