Wordpress WP_Query call posts AND pages - wordpress

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!

Related

Can I use WP_Query to return pages by category?

I'm creating a WordPress site for a theater. I have several performer pages featuring performers with images, bios, etc. I would like to feature one performer on the homepage as a "Featured Performer." I created a category in pages called "Featured-HP." I am trying to return just this one performer to use on the homepage. It is not working at all. Can this even be done?
Here is my code:
$args = array(
'post_type' => 'page',
'category' => 231,
'posts_per_page' => -1
);
// The Query
$featured_performer = new WP_Query( $args );
Then to display it...
while ( $featured_performer->have_posts() ) :
$featured_performer->the_post();
// CODE HERE TO SHOW IMAGE AND NAME
endwhile;
Nothing gets returned, though, so, I haven't even gotten to the image and name. Any help or advice would be appreciated. Thanks!
One of the category parameters in WP_Query is cat for single category id. So instead of category replace it with cat.
As follows:
$args = array(
'post_type' => 'page',
'cat' => 231,
'posts_per_page' => -1
);
You can also dig in a little bit deeper into the Category Parameters

Change wordpress to display search results in chronological order

This is a Thesis theme site using the core WP search functionality. Not looking for a new plugin solution, just how to alter the search results to change order of posts from relevance to chronological order. Any help is greatly appreciated.
Some of native wp search results, like terms do not have any type time info stored. You can use WP_User_Query with order of records. It will be not exactly what you expected but will have chronological order:
$args = array(
'search' => 'search_patern_from_form',
'orderby' => 'id',
'order' => 'DESC'
);
$user_query = new WP_User_Query( $args );
See order by section of WP_User_Query codex page

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 search custom fields

I have a custom field with meta_name is product_id. I am trying to determine how to search that field when using the wordpress search.
I know the wordpress search results page uses query_string which contains the "s" value for the keyword searched I just don't know how to change my code below to search the custom field mentioned.
$loop = new WP_Query("$query_string . '&posts_per_page=-1&post_type=product'"); ?>
think this section in the codex should have the answers you need with regards to adding in custom field parameters to you query:
http://codex.wordpress.org/Function_Reference/WP_Query#Custom_Field_Parameters
Not sure whether that will allow you to search within a particular custom field though. Can't be certain but I think you'd have to make some changes on the search form end of things. See how you get on with adding in the custom field parameters.
Off the top of my head I would think you would need something like:
$query = new WP_Query( array(
'meta_key' => 'product_id',
'meta_value' => $query_string,
'posts_per_page' => '-1',
'post_type' => 'product'
) );
Not tested though!

Resources