Custom Wordpress query using WP_Query() - wordpress

I'm trying to get a custom query going in wordpress.
Basically, I want to select all custom post types where the variable "name" has been set to "sean".
I have tried the following :
$my_loop = new WP_Query( array( 'post_type' => 'my_post', 'meta_value=sean',
'posts_per_page' => 15, 'orderby' => 'id', 'order' => 'DESC' ) );
I got this from the wordpress codex :
Display posts where the custom field value is 'blue', regardless of the custom field key:
$query = new WP_Query( 'meta_value=blue' );
Any Help would be appreciated
EDIT: I should add that I do indeed have a wordpress loop using :
while ( $my_loop->have_posts() ) {
$pdf_loop->the_post();.... etc
Thanks again,
Dave

You are mixing query string and array style arguments. Try either
new WP_Query(array(
'post_type' => 'my_post',
'meta_value' => 'sean',
'posts_per_page' => 15,
'orderby' => 'id',
'order' => 'DESC'
));
Or
new WP_Query('post_type=my_post&meta_value=sean&posts_per_page=15&orderby=id&order=DESC');

Related

Merge multiple WP_Query results into an array with no duplicates

I have a custom post type with its own custom template, and I want to display blog posts that are related to the custom post type title. However, I want to display a referenced post first, then search for any posts with tags, and then find relevant posts after that. Any one of these could result in no results or many. The total number of posts I want to display is 6.
`
$searchtag = strtolower(get_the_title());
$arg1 = array(
'post_type' => 'post',
'p' => $post_id,
);
$arg2 = array(
'post_type' => 'post',
'tag' => $searchtag,
'post_status' => 'publish',
'orderby' => 'post_date',
'order' => 'desc',
'posts_per_page' => 6,
);
$arg3 = array(
'post_type' => 'post',
's' => $searchtag,
'post_status' => 'publish',
'orderby' => 'relevance',
'posts_per_page' => 6,
);
// Get the posts
$query1 = new wp_query( $arg1 );
$query2 = new wp_query( $arg2 );
$query3 = new wp_query( $arg3 );
$related_query = new wp_query();
// Merge the unique posts
$related_query->posts = array_merge($query1->posts, $query2->posts, $query3->posts);
$related_query->post_count = $query1->post_count + $query2->post_count + $query3->post_count;
if($related_query->have_posts):
`
I read an article that stated that using post__not_in was taxing on the database and I should not incorporate it.
Do I need to add logic if the query->post_count = 0?
How can I maintain the order but ensure that there are no duplicates displayed?
I get duplicates in these results currently. I'd like to eliminate them while keeping the order of the posts by query1, then query2 then query3... displaying the first 6 posts.
The better way is make 3 queries with parameter:
'fields' => 'ids'
and there:
$postIds1 = get_posts($args1);
when merge got data:
$postIds = array_merge(array(0), $postIds1, $postIds2, $postIds3);
and when make forth request with
'post__in' => $postIds

Wordpress query custom meta key

I've added a postmeta (popular_posts) see image below. But when I query posts with meta key "popular_posts" like in below I've had no result:
new WP_Query(array( 'meta_key'=>'popular_posts' ))
Some one can explain me how to properly retrieve that have meta key "popular_posts" ?
This is the simple way to get post by their meta.
$myquery = new WP_Query( "post_type=post&meta_key=popular_posts");
Or You can use this :
$second_loop = get_posts( array(
'meta_key' => 'popular_posts',
'meta_value !=' => '',
) );
$popular_posts_args = array(
'post_type' => 'post',
'orderby' => 'meta_value',
'order' => 'DESC',
'meta_query' => array(
'meta_value' => array(
'key' => 'popular_posts',
'type' => 'NUMERIC'
)));
$popular_posts = new WP_Query($popular_posts_args);
As your meta key store numeric value. It is better to define the type in the argument. Then you can loop through $popular_posts

Show post based on the value of another custom field

How do you display posts based on the value of one of its custom fields? My code below doesn't seem to work:
$args = array(
'post_type' => 'sample-cpt',
'meta_query ' => array(array('key'=>'cpt_display', 'value' => 1))
);
$samples = new WP_Query($args);
This still returns all posts even if the value of cpt_display is a 0. Am I missing something?
Try this..
Adding the meta_key = key as part of the args,
then include it again in the meta_query array
$args = array(
'post_type' => 'sample-cpt',
'meta_key' => 'key',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'key',
'value' => 1
)
)
);
$samples = new WP_Query($args);
http://codex.wordpress.org/Class_Reference/WP_Query
I found the answer. The value of 'meta_value' needs to be an array not a string. This is a huge deviation from the WP_Query Codex and needs to be fixed.
$args = array(
'post_type' => 'sample-cpt',
'meta_key' => 'cpt_display',
'meta_value' => array(1)
);
$samples = new WP_Query($args);
wp_reset_query();
Now it only shows posts with the custom field value of 1. Looking at the request query, it shows that by using an array, the request gets converted into using the SQL IN() to search for posts instead of '=':
...postmeta.meta_key='cpt_display' AND CAST(postmeta.meta_value AS CHAR) IN ('1'))...
Source: Display custom post type by custom field date range

Changing Wordpress post order with URL string

I am trying to add a dropdown to sort my custom posts.
I have tried the solutions here - http://ak.net84.net/php/filter-dropdown-for-wordpress/ - and here - http://blog.rutwick.com/use-jquery-to-reorder-your-wp-posts-on-the-fly
I can’t get either of these to work and I can’t even get my posts to sort by adding this to the end of my URL - ?orderby=title&order=DESC.
Out of curiosity, I went over to DigWP and tried this - http://digwp.com/category/admin/?orderby=title&order=DESC which worked and sorts the posts by title and in descending order.
So I am wondering why it won’t work on my site? Here is the code that is getting my posts.
<?php $my_query = new WP_Query( array(
'post_type' => 'project',
'post_status' => 'publish',
'paged' => get_query_var('paged'),
));
while ( $my_query->have_posts() ) : $my_query->the_post(); ?>
I'm guessing there is something wrong with the query or perhaps wp_query won't allow ordering posts in this way?
Any help appreciated.
If you want to get the query string variable, i.e. ?orderby=title
$my_query = new WP_Query( array(
'post_type' => 'project',
'post_status' => 'publish',
'orderby' => get_query_var('orderby'), // will return orderby query string variable
'order' => 'DESC',
'paged' => get_query_var('paged'),
));

Where to enter custom fields meta_key for customized archives?

I have a post_type called books, which has custom fields for author name, publisher name, etc.
I want to create pages where publisher or authors are grouped. For example, "Penguin" or "McGraw Hill". When a user clicks on "Penguin" out of the Publisher dropdown, she should see all the books by Penguin for example.
To do this, I want to try a query like this:
$args = array(
'meta_key' => 'publisher'
,'meta_key_value' => 'Penguin'
,'taxonomy' => 'book'
,'orderby' => 'date'
,'posts_per_page' => 48
,'paged' => get_query_var('page')
);
$my_query = new WP_Query( $args );
My question is: where should I put this query? In the "category.php" or "archive.php"? And what will the link in the header look like?
Thanks!
Neither. You should put this in 'archive-book.php'. You can generate your link using get_post_type_archive_link and your link (without pretty permalinks) will look like '?post_type=book'.
You should also set up your query like so:
$args = array(
'meta_key' => 'publisher',
'meta_key_value' => 'Penguin',
'post_type' => 'book',
'orderby' => 'date',
'posts_per_page' => 48,
'paged' => get_query_var('page')
);
$my_query = new WP_Query( $args );
'book' in your case is not a Taxonomy, but rather a Custom Post Type. Let me know if this helps.

Resources