Where to enter custom fields meta_key for customized archives? - wordpress

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.

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 WP QUERY with OR condition

On my website, there are a couple of custom post types with default blog posts. On the blog posts, there are many categories.
One of the post types is webinar. I like to query all posts from the post type webinar and those blog posts that have category id 43
I tried following way but does not work.
1:
$query = new WP_Query(array(
'post_type' => array('webinar'), // I need all post from webinar
'cat' => '43', // I need all blog post under this category (Post type post category id 43)
);
Result: No post found
2:
$query = new WP_Query(array(
'post_type' => array('webinar','post'), // I need all post from webinar
'cat' => '43', // I need all blog post under this category (Post type post category id 43)
);
Result: Only posts of category id 43, no post from post types webinar
I need something like the following:
$query = new WP_Query(array(
array(
'relation' => 'OR',
array(
'post_type' => array('webinar')
),
array(
'post_type' => 'post',
'cat' => '43'
)
)
));
How can I do this?
Solution 1
// This could be cached in the site option
// in case this code will take a long time.
$webinar_terms = get_terms([
'taxonomy' => '{webinar_tax_name}',
'hide_empty' => true,
]);
$allowed_terms_ids = [];
foreach ( $webinar_terms as $term )
{
$allowed_terms_ids[] = $term->term_id;
}
// Add post cat to allowed terms.
$allowed_terms_ids[] = 43;
$query = new WP_Query([
'post_type' => ['webinar','post'],
'cat' => $allowed_terms_ids,
]);
NOTE
This will work only in case all webinar posts will be assigned to at least 1 {webinar_tax_name} term.
Solution 2
You may use posts_request hook and update SQL for 1 of your queries in your example. But I would prefer 1st solution as it will be more clear for other devs.
$posts = get_posts([
'post_type' => 'webinar',
'post_status' => 'publish',
'numberposts' => -1
]);
$post_data = get_posts( [
'post_type' => ['webinar', 'post'],
'posts_per_page' => - 1,
'tax_query' => array(
'taxonomy' => 'category',
'field' => 'term_id',
'terms' => '43'
),
] );
Would something like this work? I have not tested this so may need playing with a bit.
If you dont want to use get_posts then can always use new WP_Query but will need to ammend

WP_Query arguments setting for ID

I would like to do the WP_Query with the arguments setting as below, plus "WHERE ID > $post_id" (specific post id).
Pls advise how to add this WHERE clause into the $arg setting?
Thanks a lot.
$args = array (
'cat' => $category_id,
'nopaging' => false,
'posts_per_page' => '5',
'order' => 'DESC',
'orderby' => 'ID'
);
$query = new WP_Query( $args );
If you want to query "all post with an ID superior to X", I think you need to do like this :
$post_ids = range(25, 50); //we need an array of post IDs
$args = [
'posts_per_page' => 25, // if you want to limit/paginate the query
'post__in' => $post_ids
];
$posts = get_posts($args);
This will query all post from IDs 25 to 50 included. Pagination may be useful if you have a very large range of IDs to query. source.
Note that this will not generate a WHERE SQL clause, but a WHERE IN. To get a real MySQL WHERE, I think you'll to go with custom SQL query, as WP_Query doesn't provide a lot of operators on the post IDs.
oops, didn't see the > sign
you can use post__in with posts ids as #Mtxz answered above
'post__in' => array( )

wordpress get post where category is not equal to blog

i am trying to get the latest post which is not a blog . I know how to get a post by category name but how to get the post which is not equal to a specific category name . Here is the code i use to get for a category name
$query = array (
'category_name' => 'Blog',
'posts_per_page' => 1,
'post_status' => array('publish')
);
As stated in the Codex (http://codex.wordpress.org/Class_Reference/WP_Query#Category_Parameters) you can use 'category__not_in' and feed it the id of the category you want to exclude.
$args = array(
'category__not_in' => array($id), // Pass in one or more IDs here.
'posts_per_page' => 1,
'post_status' => 'publish', // no need to pass it in a array
'orderby' => 'date' // Since you wanted the latest post, this is default value so not really needed
);
You can find out the ID of the category by hovering the category-link in the WP-backend and go to the category and then check the adress in your adress-bar, it should look something like this:
/wp-admin/edit-tags.php?action=edit&taxonomy=category&tag_ID=2
There's a category_not_in query parameter. To use it you'll need the ID, not the slug, of the category you want to exclude from your result.
http://codex.wordpress.org/Class_Reference/WP_Query#Category_Parameters
$BlogObj = get_category_by_slug('Blog');
$query = array (
'category_not_in' => array($BlogObj->term_id),
'posts_per_page' => 1,
'post_status' => array('publish')
);
You may try this:
$query = array (
'category_not_in' => array(get_cat_ID('Blog'),
'posts_per_page' => 1,
'post_status' => 'publish'
);
Think we need to combine answers of #the-alpha and #o-jones here.
$BlogObj = get_category_by_slug('Blog');
$query = array (
'category__not_in' => array($BlogObj->term_id),
'posts_per_page' => 1,
'post_status' => array('publish')
);
Simply you have to change 'category__not_in' instead of 'category_not_in'. Adding this for future users may come around.

Custom Wordpress query using WP_Query()

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');

Resources