Custom post type pagination - wordpress

I'm trying to print out a number of posts from multiple post types. However, I can't seem to implement the pagination - once I go to .../category.../page/2 I get a page not found error. It seems like it doesn't even try to read what's inside of my archive.php file.
I'm 100% sure that there's no problem with permalinks, since I've tried resetting them and etc.
I suppose it has something to do with the fact that in admin settings the number of posts per page is set to 10, but i really need to be able to edit this number dynamically.
I've tried a variaty of fixes from here, but non of them seem to work: http://wordpress.org/support/topic/pagination-with-custom-post-type-getting-a-404?replies=1#post-1616810
I'm sorry if it's highly repetitive question - none of other fixes I found worked for me.
Thanks a lot!

You can do something like this (not tested):
$posts_per_page = 10;
$post_type = 'YOUR_POST_TYPE'
$args = array( 'post_type'=> $post_type, 'posts_per_page' => $posts_per_page, 'paged' => get_query_var('paged') );
query_posts( $args );
// here loop

Related

WordPress is_search() function always false

I want to add some custom conditions and do some other things on a search WordPress results page. So, I’m checking the value of is_search() to make sure I’m only applying the condition at the right time. So in my child theme's functions.php I put:
if (is_search()) {
...
}
But this always returns false, even with a url like http://mysite/?s=something and my theme's search.php template is being used! Is this not valid to call from functions.php, or am I misunderstanding this function's purpose?
For that matter, looking at a template hierarchy like presented at https://wphierarchy.com, how does WP even know it’s a search results page? How does it know to proceed down the "search results" path? I’ve spent some time perusing the source code but haven’t been able to find the right spot yet.
I tried calling is_search() from search.php, and it works! From some more source code perusal, I discovered that you can't call is_search() from functions.php because functions.php is called too early in the process. Apparently functions.php is considered part of the "WordPress library" and is loaded in wp-blog-header.php line 13, while the template is called in line 19.
In between the two is the wp() function, which sets up the query. That query is actually set up in user.php line lines 1198-1212:
$args = array(
'post_type' => $this->post_type,
'post_name__in' => array( $this->request_type ),
'posts_per_page' => $posts_per_page,
'offset' => isset( $_REQUEST['paged'] ) ? max( 0, absint( $_REQUEST['paged'] ) - 1 ) * $posts_per_page : 0,
'post_status' => 'any',
's' => isset( $_REQUEST['s'] ) ? sanitize_text_field( $_REQUEST['s'] ) : '',
);
...
$requests_query = new WP_Query( $args );
To answer my second question (how does WordPress know it's a search), it’s as simple as having the "s=" query string parameter. Not sure what to do if you want a search result that does not have a search string (say a custom search based solely on pulldowns or something). If you need that, I would recommend looking thru the source file WP-query.php and seeing exactly what you need to pass. Don’t be scared of looking at the source code; It’s very educational!

Wordpress query from Magento/WHMCS return wrong result

I am trying to display posts from a specific cateogry in WordPress in a footer column on a site. It works fine unless the footer is displaying on a page that is integrated in either WHMCS or Magento. For some reason, on those pages within those apps, it still displays the blog post column, but instead of returning the last X # of posts in the specified category, it seems to return the last post X # of times.
For example, here is the stand alone Wordpress blog column pulling from a specific category:
http://www.thinkshovels.com/includes/latest_work.php
This is exactly what we want shown throughout the site, however if you visit http://www.thinkshovels.com/service/ you can see that the middle column is not displaying that info.
Here is the code querying wordpress:
define('WP_USE_THEMES', false);
require('/home/shovels/public_html/blog/wp-load.php');
$qarray = array('cat' => '5', 'posts_per_page' => 4);
query_posts($qarray);
while (have_posts()): the_post();
$args = array( 'post_type' => 'attachment', 'numberposts' => -1,
'post_status' => null, 'post_parent' => $post->ID );
I'm not sure if I have done something wrong here, or if there is a better way to approach this, but it seems that WHMCS and Magento break something with these queries.
Any tips/advice appreciated! Thanks.
Instead of query_posts try to use get_posts instead.
According to the article from Developer.WordPress.com you should avoid using query_posts.

wordpress - excluding a category from a post list in a custom theme that doesn't look like the codex

I'm hoping someone can help. I'm not a php coder, but I've been tweeking and customising a premium theme for wordpress anyway, and I'm stuck.
I'm trying to exclude a specific category from a page which lists all the categories by default. Ok, no problem. It should be:
<?php query_posts($query_string . '&cat=-134'); ?>
right?
I'm pretty sure the category number is 134, but I could be wrong. The premium theme I'm using is called Risen, and there's a lot of different kinds of posts - so maybe what I think is a category is really a tag in a custom taxonomy - in which case ???
When I hover over it in the category listing I get this:
example.com/wp-admin/edit-tags.php?action=edit&taxonomy=risen_multimedia_category&tag_ID=134&post_type=risen_multimedia
I'm pretty sure I've found where I need to include my argument, and that is here in the template:
// Get posts
$multimedia_query = new WP_Query( array(
'post_type' => 'risen_multimedia',
'posts_per_page' => risen_option( 'multimedia_per_page' ) ? risen_option( 'multimedia_per_page' ) : risen_option_default( 'multimedia_per_page' ),
'paged' => risen_page_num() // returns/corrects $paged so pagination works on static front page
) );
I've tried adding
'tag' => -134
to this array to no avail.
Being a premium, and apperently tweaked, theme there is a lot of guessing here but I think you have talked yourself into the solution, except for one detail. Use tag__not_in not tag=-134
// Get posts
$multimedia_query = new WP_Query( array(
'post_type' => 'risen_multimedia',
'posts_per_page' => risen_option( 'multimedia_per_page' ) ? risen_option( 'multimedia_per_page' ) : risen_option_default( 'multimedia_per_page' ),
'paged' => risen_page_num() // returns/corrects $paged so pagination works on static front page
'tag__not_in' => array(134)
) );
tag_id=-134 might work (I'd have to test it) but tag expects the tag slug not an ID.
tag (string) - use tag slug
http://codex.wordpress.org/Class_Reference/WP_Query#Tag_Parameters

Wordpress WP_Query call posts AND pages

I have a feature slider set up that draws in posts that are tagged 'feature'
$my_query = new WP_Query(array(
'showposts' => 3,
'tag' => 'feature' ));
Is is possible to draw in posts AND pages?
I know you can draw pages with 'post_type'=>'page' but can you mix the two?
You can specify an array value for the post_type parameter, as such:
$my_query = new WP_Query(array(
'post_type' => array('post', 'page'),
'tag' => 'feature'
));
See this page for more info: WP Codex
#fivedigit Thanks but I went with this in the end:
$my_query = new WP_Query(array(
'post_type' => array('any'),
'tag' => 'feature'
));
Although your version may come in handy in the future!
For anyone having to edit older code that doesn't use an array passed into WP_Query, you can add &post_type=any to get posts and pages (and other content). Unfortunately I don't see a way to get posts and pages (without other types) without using an array, since post_type would then require an array as the examples above show. However, this should be good enough if you are searching for a particular category anyway.
Example (this from vSlider v4.1.2 where &post_type=any is added so that pages are included in the slider):
$recent = new WP_Query($randimg."cat=".$options['imgCat']."&showposts=".$options['slideNr']."&post_type=any");
Thanks to #fivedigit and #my-jonny-wood for the answers above that led me to figuring this out and fixing the slider on my site!

Problem with Categories in Wordpress!

My Original question was this:
I have a wordpress installation where
the Home page is set to display post
from a specific category. To achieve
this, I created a template "home.php"
and put the following code:
query_posts( 'cat=4&order=desc' );
When I visit the site's home page, it
display posts from the category but
the Navigation does not work? I have
permalink structure set
"/%category%/%postname%/"
Please let me know if I am doing
something wrong.
Update:
Now, I have solved this using this code:
$paged = (get_query_var('page')) ? get_query_var('page') : 1;
$args = array (
'cat' => 4,
'orderby' => 'date',
'order' => 'desc',
'posts_per_page' => '10'
);
query_posts($args . '&paged=' . $paged);
Now the problem is that first time, it displays everything fine but if you visit any other archive page, it start displaying posts from other categories. For example, the above code displays posts from Cat=>3 as well. And on page, where I want to display posts from Category = 3, it display posts from category 4 as well.
Please help...
Thanks
What do you mean by "Navigation"? What code are you using for the navigation? Are you limiting the amount of posts?
If you have problems with the pagination. You can use this:
<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts("cat=4&order=desc&paged=$paged"); ?>
Please, Clarify your problems to help you.

Resources