I have a site where on the home page there are teasers for the 3 most recent posts.
If I make a post sticky then it actually adds a fourth post display and so on.
After searching I found this script which seem to initially work:
<?php
$sticky = count(get_option('sticky_posts'));
$the_query = new WP_Query( 'posts_per_page='. ( 3 - $sticky));
?>
This seemed to do the job perfectly. However I found if one of the posts was a recent post and was also set to sticky then this would affect the amount displyed.
For example, it the second most recent post was made sticky then it would result in only two posts displaying.
Any ideas on how the above code can be altered to always show three posts, sticky or not?
Thanks
$args = array(
'posts_per_page' => 3,
'ignore_sticky_posts' => 1
);
query_posts($args);
-- use while loop here ---
and after that use
wp_reset_query();
Related
I know, this question has been asked a lot. I have been through a lot of them already, but my issue seems to be more bespoke.
Initially, I had this issue, but seems to be another issue, therefore I am asking a new question in hopes for a solution.
I am using the WP Download Manager Pro plugin that creates a custom post type wpdmpro.
So, the crux of my issue seems to be that my pagination in my loop.php, seems to be connected someone to the default post type and not the custom post type I am using, as in, if I have 0 Posts in my default posts, navigating to /page/2 will not work, but if I have 11 default posts it will, but then /page/3 will not work.
I have 11 wpdmpro posts and 0 default posts. Therefore, I would expect here, that with 2 posts per page, that I would have 6 pages? Wordpress seems to think differently!
Here is my query
<?php
$category = get_queried_object();
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$args = array(
'orderby' => 'date',
'order' => 'DESC',
'post_status' => 'publish',
'posts_per_page' => 2,
'paged' => $paged,
'post_type' => 'wpdmpro',
'wpdmcategory' => $category->category_nicename,
'tag' => $cat_tag
);
// Query
$wpdmpro_query = new WP_Query( $args );
// The Loop
if( $wpdmpro_query->have_posts() ) : while( $wpdmpro_query->have_posts() ) : $wpdmpro_query->the_post();
?>
<!-- Do stuff -->
<?php endwhile; ?>
<!-- Do stuff if there are no posts -->
endif; wp_reset_postdata(); ?>
So WHY does /page/2 return a 404 when that page SHOULD exist? As mentioned, if I start to add posts to the default post (on the 11th post), then /page/2 seems to work, with the 21st post /page/3/ works, so it has to be linked to that. But why? I clearly define that I am NOT using 'post_type' => 'post',. So where could this be defined and how can I override it?
Just a few things to add in case they are somehow linked.
In my permalinks settings, I have a custom structure of
/%category%/%postname%/
and my default category base is .
I have tried resetting the permalinks settings, but still getting the 404 page on /page/2.
In my WP Download Manager Settings, my WPDM Category URL Base is .
So, with all that my url is.
{domain}/category-name/page/2/ which is returning the 404.
{domain}/category-name/ returns 2 posts of the stated category.
Part of me is concerned this is a configuration issue, mixing a custom post type with custom structure and category base. Someone out there must have had this issue? It seems like this should be very simple, but I cannot for the life of me find a solution, perhaps I am looking in the wrong places?
Any help will be massively appreciated.
Edit:
I have tried to regenerate the permalinks on multiple different occassions to no effect.
It's a bit more complicated than that.
For a travel website, I have 3 parent categories: Series, Post Type, and Location.
Each post is assigned to a child category. For example (respectively): EU2014, Picture Gallery, and Rome.
On each post, there is a sidebar. My intention is that this sidebar will contain links to the other related posts. A post is related if it is:
1.) In the same Series child category (such as "EU2014")
AND
2.) in the same Location child category (let's say "Rome").
Really what I'm doing here is making it so that way the client can write any number of posts, and a network of links will appear on all of them (in the sidebar).
So, in sum, all posts designated "EU2014" AND "Rome" will be part of a collection.
Then I can format the sidebar nicely based on what Post Type a post in question is (with appropriate icons or whatnot).
My problem is this:
I need pseudo code. I've started working on it, but I'm very concerned that, if I am to iterate through the entire database of posts every time I load a post, simply to write that sidebar, it will be a massive resource drain on the system.
How can I
1.) Identify the child categories of the current post
and
2.) get the names and links of each post whose Series and Location child categories match those of the current post in order to write that information to the sidebar of the current post
without creating a black hole of fuckery when the site grows to more than just a few total posts?
This is going to be written in PHP, so if anyone has any interest in helping me figure out more than just pseudo, I'd be thrilled to suss it out with you.
<?php
$args = array(
'posts_per_page' => -1,
'offset' => 0,
'category' => 8, // specify category id here
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => 'post',
'post_parent' => '',
'post_status' => 'publish',
'suppress_filters' => true );
$myposts = get_posts( $args );
foreach( $myposts as $post )
{
setup_postdata( $post );
?>
<?php the_title(); ?>
<?php
}
wp_reset_postdata();
?>
For Reference see this
http://codex.wordpress.org/Template_Tags/get_posts
So I'm using wordpress with a updownupdown plugin (plugin for upvoting/downvoting posts). What I am trying to do is to show post with ,let's say at least 10 upvotes, on the homepage and hide the post with not enough points.
I tried to us this code in index.php
if (have_posts()) :
while (have_posts()) : the_post();
if (up_down_post_votesscore( get_the_ID()) > 10){
//content
}
endwhile;
endif;
and it kind of works, it hides all the post with less than 10 upvotes, but it doesn't put all the qualifying posts in one page (homepage has a pagination), so let's say there is 2 pages with 5 posts and there is 2 posts in each page with needed amount of upvotes, so instead of putting those 4 posts in one page, it shows only 2 qualifying post in each page.
Sounds like your posts retrieval is limited. You may want to change how many posts are obtained using:
query_posts( 'posts_per_page=20' );
reference: http://codex.wordpress.org/Function_Reference/query_posts
or do something like this if you're using get_posts:
$args = array(
'posts_per_page' => 20
);
get_posts( $args );
Reference: http://codex.wordpress.org/Template_Tags/get_posts
I am using the PBD Load Next Post plugin and it's working great, but I can't figure out why the max_num_pages is returning 44 (number of posts in that category) instead of 4 (number of pages it should be). Here are all the codes that should be relevant:
http://pastebin.com/ezAbD2eH
Here is the page that it is running on: garthreckers(dot)com/category/united-states/ (sorry, not enough rep to post another full link)
It's also running on the Europe page but its returning 20 over there (number of posts again).
Also, the if(mobile...) is from the mooble plugin if that makes a difference. I have tried stripping out the code for that and deactivating and it still doesnt work.
Any help will be great since I have been trying to fix this problem for 3 days now with no success.
I know this may not be the best way but if you want to you can change the default number in the javascript as a workaround
Your JS http://pastebin.com/ezAbD2eH
Line 58:
var max = parseInt(pbd_alp.maxPages);
Change to:
var max = parseInt(pbd_alp.maxPages/12); //divide it by the number of posts per page.
if you want you can probably set that as a variable and use it elsewhere where your $arg is as well so that the number is set globally. Set $posts_per_page = 12 inside your PHP section.
var max = parseInt(pbd_alp.maxPages/<?php echo $posts_per_page; ?>);
I may have found your issue. Currently in your PHP code when displaying all your posts you have a query that calls for Categories
wp_list_categories('show_option_none=&orderby=name&show_count=1&hide_empty=1&use_desc_for_title=1&child_of='.$cat.'&title_li=');
Currently this is throwing off the number as it is showing max_num_pages based on the category list number. I would change the way you are displaying posts so that it is not using wp_list_categories to retrieve them.
Reference: http://codex.wordpress.org/Template_Tags/get_posts
<?php
$args = array( 'numberposts' => 3 );
$lastposts = get_posts( $args );
foreach($lastposts as $post) : setup_postdata($post); ?>
<h2><?php the_title(); ?></h2>
<?php the_content(); ?>
<?php endforeach; ?>
What I have:
Home
About
Projects
Project Pages 1
Project 1
Project Pages 2
Project 2
Project Pages 3
Project 3
I would like to loop through the Project Child Pages in order to display the pages in order of most recent of all sub-child projects then be able to echo or pull out Custom Fields ,
<?php the_title();?>
<?php the_excerpt();?>
I do not want actually display the parent pages however, only for example project 1 or Project 2 or Project 3 etc.
Have tried many things but cannot seem to be able to do it with array_merge or wp_list_pages.
Any help would stop me from pulling my hair out. Greatly appreciated.
Read below URL i am very sure it is very help full to you.
How to Exclude Posts from Children Categories on WordPress Category Page
http://thesistut.com/2012/exclude-posts-from-children-category/
I found how to do it. I used the following code. I hope it helps someone.
<?php
$args=array(
'post_type' => 'page',
'post__in' => array(345, 289),
'post_status' => 'publish',
'posts_per_page' => 2,
'caller_get_posts'=> 1
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post();
?>
This produced 2 posts as pages, from the specific category without displaying the parent, avoiding using wp_list but using a new WP_Query and $args. It worked as a temporary fix but does work.