Query posts by custom taxonomy ID - wordpress

I have a custom post type called portfolio and a custom taxonomy called build-type (acting as categories)
I am trying to query portfolio posts by build-type ID e.g. all Portfolio posts in "Hotels" (id=4 for that taxonomy)
// gets the ID from a custom field to show posts on a specific page
$buildType = get_post_meta($post->ID, 'build_type_id', true);
// run query
query_posts(array(
'post_type' => 'portfolio',
'showposts' => -1,
'tax_query' => array(
'taxonomy' => 'build-type',
'terms' => $buildType,
'field' => 'term_id'
),
'orderby' => 'title',
'order' => 'ASC'
));
Currently it's calling all portfolio posts and not just those with the build-type ID
For 'field' => 'term_id' should I be using term_id, tag_ID, id or something else?
Anyone know how to get this working?
Thanks in advance!

I solved it with help from: https://wordpress.stackexchange.com/questions/30476/query-posts-by-custom-taxonomy-id
tax-query needs to be an array of arrays
The final solution is:
// gets the ID from a custom field to show posts on a specific page
$buildType = get_post_meta($post->ID, 'build_type_id', true);
// run query
query_posts(array(
'post_type' => 'portfolio',
'showposts' => -1,
'tax_query' => array(
array(
'taxonomy' => 'build-type',
'terms' => $buildType,
'field' => 'term_id',
)
),
'orderby' => 'title',
'order' => 'ASC' )
);
On github here:
https://gist.github.com/1275191

I'm not a WP-gury and I have invested hours and hours trying to solve the same problem. Eventually I found this blog post: http://richardsweeney.com/blog/wordpress-3-0-custom-queries-post-types-and-taxonomies/
The answer is somewhat semi-bad: apparently you can't filter like this for custom post types (it is only possible for posts), which is a shame!
What I did work was this:
$args['custom_tax'] = 'custom_tax_slug';
query_posts($args);
Hope it helps!
//Mike

Related

Wordpress output links to previous and next posts from custom query

I'm using the advanced custom fields plugin for wordpress to create a group of custom post types that have a date set within them.
I'm trying to show the previous post, and the next post, based on the date stored in the custom field. The links need to link to posts that have a date set in the future (so don't show links to posts with dates that have gone by)/
I can get a list of all the posts that are in the future, and out put these using the following code;
<?php
$rightnow = current_time('Ymd');
$args = array(
'post_type' => 'Courses',
'posts_per_page' => '25',
'meta_query' => array(
array(
'key' => 'date_of_the_course_single_day',
'compare' => '>=',
'value' => $rightnow,
)
),
'meta_key' => 'date_of_the_course_single_day',
'orderby' => 'meta_value',
'order' => 'ASC',
'post_status' => 'publish'
);
$posts = get_posts($args);
foreach ( $posts as $post ) {
?>
Output details of post here....
<?php
}
?>
What I thought I could do, is the get the current post's position in the array, to then get details of the posts one before and one after... but I haven't got a clue how to do this.
I've experimented with the wordpress next_post_link and previous_post_link functions, but these seem to work based on when the post was added to wordpress, rather than based on my custom date field.
Am I going about this the complete wrong way? Any tips or pointers would be much appreciated!
Use WP_Query plus paginate_links
$rightnow = current_time('Ymd');
// Query Args
$args = array(
'post_type' => 'Courses',
'posts_per_page' => '25',
'meta_query' => array( array(
'key' => 'date_of_the_course_single_day',
'compare' => '>=',
'value' => $rightnow,
) ),
'meta_key' => 'date_of_the_course_single_day',
'orderby' => 'meta_value',
'order' => 'ASC',
'post_status' => 'publish'
);
$query = new WP_QUery( $arg );
$posts = $query->get_posts();
// Paginate Args
$page_args = array(
'base' => 'your_custom_page_url'.'%_%', // Make sure you got this current depending on your setup
'format' => '/%#%', // requires pretty permalinks
'total' => $query->max_num_pages,
'current' => 0,
'prev_text' => __('«'),
'next_text' => __('»'),
);
foreach ( $posts as $post ) {
// Output
}
echo paginate_links( $page_args );
You have to verify that the base and format of paginate args are correct of it won't properly worked.

Can't get tax_query to work

I have a custom WP Query where I list custom post types. I am trying to set up filters to filter listings by custom taxonomy.
My query setup is as follows:
// WP_Query arguments
$args = array (
'post_type' => 'opps',
'post_status' => 'publish',
'pagination' => true,
'posts_per_page' => $postsperpage,
'posts_per_archive_page' => $postsperpage,
'ignore_sticky_posts' => true,
'order' => 'DESC',
'orderby' => 'date',
'cache_results' => true,
'update_post_meta_cache' => true,
'update_post_term_cache' => true,
'paged' => $paged,
'tax_query' => array(
array(
'taxonomy' => 'sector',
'field' => 'slug',
'terms' => 'business-services'
),
)
);
So in the above example I should get only posts with taxonomy 'sector' with slug 'business-services', but ALL posts from custom post types 'opps' get listed instead.
I have looked everywhere on stackoverflow and beyond but I just can't figure out what I've been doing wrong.
Your code looks fine.
I have had this issue before because wordpress does not sync taxonomy terms until late in the init phase. Have you tried running this code in a template, after get_header() or wp_head() has been called? If not try that, it should work then. I know if I've echoed it out in functions.php for testing it will not work because wordpress has not synced tax terms yet.
You should really have a look at the codex (WP_Query) when creating custom queries. As your code stands, it will fail. You have the following issues
pagination is not a valid parameter for WP_Query.
You cannot use posts_per_page and posts_per_archive_page together
You don't need to set parameter values that are set by default. Unnecessary parameters used in your code which is by default the default values are order, orderby, post_status, cache_results, update_post_meta_cache and update_post_term_cache
There is no sticky post functionality for custom post types. just remove the ignore_sticky_post parameter
Your code should look something like this ( I hope your variables is defined )
// WP_Query arguments
$args = array (
'post_type' => 'opps',
'posts_per_page' => $postsperpage,
'paged' => $paged,
'tax_query' => array(
array(
'taxonomy' => 'sector',
'field' => 'slug',
'terms' => 'business-services'
),
)
);

Wordpress get_pages based on page category

I need to get_pages or get an array of pages in WP based on it 's category
I know WP doesn't come with categories on pages but I'm using this in the functions.php to get categories on the pages.
add_action('admin_init', 'reg_tax');
function reg_tax() {
register_taxonomy_for_object_type('category', 'page');
add_post_type_support('page', 'category');
}
Now I need to use these categories in get_pages or WP_Query to get in the pages with a category.
<div class="productNav">
<ul>
<?php
$product_page_args = array(
'post_type' => 'page',
'order' => 'ASC',
'orderby' => 'menu_order',
'child_of' => $post->ID,
'category_name' => 'pillar-product'
);
//$product_pages = get_pages($product_page_args);
$product_pages = new WP_Query($product_page_args);
foreach ($product_pages as $product_page){
?>
<li><?php echo $product_page->post_title; ?></li>
<?php
}
?>
</ul>
</div>
Respectfully this answer does NOT work anymore in 2021. This is an obsolete answer and includes all of the pages. Please use the other answer.
Try this: (won't work on wordpress 5.8)
$product_page_args = array(
'post_type' => 'page',
'order' => 'ASC',
'orderby' => 'menu_order',
'child_of' => $post->ID,
'taxonomy' => 'category',
'field' => 'slug',
'term' => 'pillar-product'
);
The accepted answer for this question won't work anymore and includes all of the pages because 'taxonomy', 'field' and 'term' must be inside an array which resides inside 'tax_query' array, for this query to work.
For more comprehensive answer on how to query wordpress pages based on specific category see the following link:
How to query pages based on specific categories
Here's the correct answer for this question, fully tested on wordpress 5.8.
$product_page_args = array(
'post_type' => 'page',
'order' => 'ASC',
'orderby' => 'menu_order',
'child_of' => $post->ID,
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => array('pillar-product'),
)
)
);

Get multiple posts by multiple slugs

I have an array with multiple slugs (post names) and I want to get all posts where the slug is in the array.
I use Event Organsier and want to get all events these slugs. I haven't worked with WordPress for a couple of years so I need some help.
$events = eo_get_events(array(
'numberposts'=>5,
'event_start_after'=> $year.'-'.$month.'-'.$list_day,
'event_end_before'=> $year.'-'.$month.'-'.$list_day,
'showpastevents'=>false,//Will be deprecated, but set it to true to play it safe.
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => array('slug-example-1', 'slug-example-2'),
'operator' => 'IN'
)
)
));
This should (imo) return all posts that has a slug that is slug-example-1 or slug-example-2.
I don't even know if tax_query/taxonomy is the right thing to use.
You can use the post_name__in argument in conjunction with get_posts(), it's available since Version 4.4.
E.g. :
$args = [
'post_name__in' => [ 'slug-1', 'slug-2' ],
'post_type' => 'page',
'post_status' => 'publish',
];
$result = get_posts( $args );

Select all posts which do NOT have a certain tag

In WordPress, how can I select all posts in a certain category (custom taxonomy), which do NOT have a certain tag (custom taxonomy).
The following code select all posts in a certain category (custom taxonomy), which DO have a certain tag (custom taxonomy). How does this have to be modified?
$postquery = new WP_Query( array(
'post_type' => 'myposttype',
'mycategory' => $cat,
'posts_per_page' => $numposts,
'orderby' => 'date',
'tax_query' => array( array(
'taxonomy' => 'mytag',
'field' => 'slug',
'terms' => array('select by this tag', 'and this tag', 'and this tag'),
) )
)
);
(I assume it would be better to just list all allowed tags, but that's not really feasible in my case, because new tags are still added.)
add 'operator'=>'NOT IN' to the 'tax_query' array

Resources