I am trying to display a custom post type that has custom taxonomy, but I am not having any luck. Nothing is showing up. I appreciate any help.
Post type = gallery
Custom Taxonomy slug = photoarea
The 'photoarea' I want to display = fourth
<?php
$args = array(
'post_type' => 'gallery',
'tax_query' => array(
array(
'taxonomy' => 'photoarea',
'field' => 'fourth',
)
),
'posts_per_page' => 10,
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post();
the_post_thumbnail();
endwhile; endif;
wp_reset_query();
?>
You may use below code snippet:
$the_query = new WP_Query( 'post_type=gallery&photoarea=fourth');
and then your while loop.
if my understanding is right, that you need to get the custom taxonomy with following code,
instead of field you must use term to get the posts in Fourth
<?php
$args = array(
'post_type' => 'gallery',
'tax_query' => array(
array(
'taxonomy' => 'photoarea',
'field' => 'slug',
'terms' => 'fourth'
)
),
'posts_per_page' => 10,
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post();
the_post_thumbnail();
endwhile; endif;
wp_reset_query();
?>
$args = array('post_type' => 'gallery','posts_per_page'=>'-1','tax_query' => array(array(
'taxonomy' => 'photoarea',
'field' => 'term_id',
'terms' => $your_term_id,
),
),
);
Related
In my website, I have two different post-types. One of them is publication with custom category-type as publication-category and the other is service with custom category-type as service-category. I am publishing some brochures in Publication page those are under different services. What I am trying to do is displaying these brochures in Services page by same category type. It is like, if the brochure is published by Education services, then this brochure should be displayed in Education service page.
I am currently using ACF plugin to do so, but whenever there is new brochure, I have to go to service page and add it there. Today I tried below code but it displays all brochures from different category-types not the same category-type.
Perhaps you guys can help me how I can arrange the code a way that works for my above request.
<?php
$custom_taxterms = wp_get_object_terms(
$post->ID,
'publication-category',
array( 'fields' => 'ids' )
);
$args = array(
'post_type' => 'publication',
'post_status' => 'publish',
'posts_per_page' => 10,
'orderby' => 'rand',
'order' => 'ASC',
'tax_query' => array( array(
'taxonomy' => 'publication-category',
'field' => 'id',
'terms' => $custom_taxterms
)),
'post__not_in' => array( $post->ID ),
);
$related_items = new WP_Query( $args );
if ( $related_items->have_posts() ) :
echo '<ul>';
while ( $related_items->have_posts() ) : $related_items->the_post();
?>
<li><?php the_title(); ?></li>
<?php
endwhile;
echo '</ul>';
endif;
wp_reset_postdata();
?>
If you're on services page, so why do you use the 'publication-category' in
wp_get_object_terms(
$post->ID,
'publication-category',
array( 'fields' => 'ids' )
);
Seems like you have to use
$custom_taxterms = get_the_terms($post->ID, 'service-category');
$terms = [];
foreach ($custom_taxterms as $term) {
$terms[] = $term->slug
}
$args = array(
'post_type' => 'publication',
'post_status' => 'publish',
'posts_per_page' => 10,
'orderby' => 'rand',
'order' => 'ASC',
'tax_query' => array( array(
'taxonomy' => 'publication-category',
'field' => 'slug',
'terms' => $terms
)),
);
And create same slugs for both terms in both categories. From my understanding. It's quite difficult to understand your architecture
I find the solution by changing service-category taxonomy to publication-category as same with other post-type and creating relationship with below code from : https://wordpress.stackexchange.com/questions/139571/display-posts-with-same-taxonomy-term?rq=1
Thanks everyone
<?php
//get the post's venues
$custom_terms = wp_get_post_terms($post->ID, 'publication-category');
if( $custom_terms ){
// going to hold our tax_query params
$tax_query = array();
// add the relation parameter (not sure if it causes trouble if only 1 term so what the heck)
if( count( $custom_terms > 1 ) )
$tax_query['relation'] = 'OR' ;
// loop through venus and build a tax query
foreach( $custom_terms as $custom_term ) {
$tax_query[] = array(
'taxonomy' => 'publication-category',
'field' => 'slug',
'terms' => $custom_term->slug,
);
}
// put all the WP_Query args together
$args = array( 'post_type' => 'publication',
'posts_per_page' => 20,
'tax_query' => $tax_query );
// finally run the query
$loop = new WP_Query($args);
if( $loop->have_posts() ) {
while( $loop->have_posts() ) : $loop->the_post(); ?>
<div class="listing-title"><?php the_title(); ?></div>
<div class="listing-image">
</div>
<?php
endwhile;
}
wp_reset_query();
}?>
I would like to display e.g. the title of a all posts with a custom post type and a specific taxonomy.
I tried following:
global $post;
$myposts = get_posts(array(
'post_type' => 'my-post-type',
'tax_query' => array(
array(
'taxonomy' => 'post-type-category',
'terms' => 'new-posts')
)
)
);
foreach ($myposts as $mypost) {
echo $mypost->post_title;
}
you can use WP Query :
<?php
$args = array(
'post_type' => 'my-post-type',
'tax_query' => array(
array(
'taxonomy' => 'post-type-category',
'field' => 'slug',
'terms' => 'new-posts',
),
),
);
// The Query
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
the_title() ; // title
the_excerpt(); // excerpt
the_post_thumbnail(); // post thumbnail
}
wp_reset_postdata();
} else {
// no posts found
}
I have created some taxonomies and post_types trough custom post type UI plugin.
I pass taxonomy_id with a form to a page and I receive it correctly [var_dump($_POST)] shows me number example 30. I want to show posts from that custom post type category: I tried the code below but it returns nothing.
$args = [
'tax_query' => array(
array(
'taxonomy' => 'school_type',
'field' => 'term_id',
'terms' => array('30','22'),
// 'operator' => 'IN'
),
'post_type' => 'school',
)
];
if($q->have_posts()):
while($q->have_posts()){
$q->the_post();
echo the_title();
}
else:
echo 'nothing';
endif;
Can anyone help me?
You should have a $arg variable like this and then use the WP_Query() object to get your posts.
$args = array(
'post_type' => 'school',
'posts_per_page'=>30,
'post_status' => 'publish',
'tax_query' => array(array(
'taxonomy' => 'school_type',
'field' => 'term_id',
'terms' => array('30','22'),
);
$q = new WP_Query($args);
if($q->have_posts()):
while($q->have_posts()){
$q->the_post();
echo the_title();
}
else:
echo 'nothing';
endif;
Put $q = new WP_Query( $args ); before your if line
change your permalink to post type and update permalinks, hope this will help.
You have the post_type in the tax_query array, plus you're targeting $q when it doesn't exist (not that I can see. anyway).
Try something like this :-
$args = array(
'post_type' => 'school',
'tax_query' => array(
array(
'taxonomy' => 'school_type',
'field' => 'term_id',
'terms' => array('30','22'),
),
),
'numberposts' => -1
);
$q = new WP_Query($args);
if($q->have_posts()):
while($q->have_posts()){
$q->the_post();
echo the_title();
}
else:
echo 'nothing';
endif;
I'm trying to overwrite the default 'Blog pages show at most' which is set to 5 posts. I have a custom post type called 'FAQs', which has the argument 'posts_per_page' => 999 in the query for getting all the posts of this type, however I can't seem to override the default restriction in the WordPress setting. My code for the FAQ query is below, which works on my local machine (MAMP) but not when I upload it to live. how do I show all posts of that type?
<?php
wp_reset_query();
// Query for getting custom taxonomy 'FAQ Type' of custom post type 'FAQs'
$cat_args = array (
'taxonomy' => 'faq_type',
'exclude' => array(12),
'posts_per_page' => 999,
//'show_all' => true,
'orderby' => 'simple_page_ordering_is_sortable'
);
$categories = get_categories ( $cat_args );
foreach ( $categories as $category ) {
//wp_reset_query();
$cat_query = null;
// Query for getting posts of custom post type 'FAQs'
$args = array (
'post_type' => 'faq',
'faq_type' => $category->slug,
'posts_per_page' => 999,
//'show_all' => true,
'orderby' => 'simple_page_ordering_is_sortable',
);
$cat_query = new WP_Query( $args );
if ( $cat_query->have_posts() ) { ?>
<?php echo "<h2>". $category->name ."</h2>"; ?>
<ul id="resident-accordion" class="accordion white-bg-accordion" data-accordion data-allow-all-closed="true" role="tablist">
<?php
while ( $cat_query->have_posts() ) {
$cat_query->the_post();
?>
<li class="accordion-item faq-content <?php //if ($firstLoop === true) { echo "is-active"; }?>" data-accordion-item>
<?php the_title(); ?>
<div class="accordion-content" data-tab-content>
<?php the_content(); ?>
</div>
</li>
<?php
} //wp_reset_query();
wp_reset_postdata(); //End WHILE
echo "</ul>";
} //End IF
wp_reset_postdata();
//wp_reset_query();
} //End FOR
?>
You can try to use this code below :
<?php
$cat_args = array(
'taxonomy' => 'faq_type',
'exclude' => array(7),
'orderby' => 'simple_page_ordering_is_sortable'
);
$categories = get_terms( $cat_args );
foreach ( $categories as $category )
{
$args = array(
'post_type' => 'faq',
'posts_per_page' => -1, // load all posts
'orderby' => 'simple_page_ordering_is_sortable',
'tax_query' => array(
array(
'taxonomy' => 'faq_type',
'field' => 'slug',
'terms' => $category->slug
)
)
);
$cat_query = new WP_Query( $args );
// enter the rest of your code below
}
Or you can use get_posts() to receive posts list.
<?php
$cat_args = array(
'taxonomy' => 'faq_type',
'exclude' => array(7),
'orderby' => 'simple_page_ordering_is_sortable'
);
$categories = get_terms( $cat_args );
foreach ( $categories as $category )
{
$posts = get_posts(array(
'numberposts' => -1, // get all posts.
'tax_query' => array(
array(
'taxonomy' => 'faq_type',
'field' => 'slug',
'terms' => $category->slug,
'operator' => 'IN',
),
),
'post_type' => 'faq',
));
// enter the rest of your code below
}
Cheers!
You found your answer by now I hope, but you were so close.
Instead of:
'posts_per_page' => 999,
try:
'posts_per_page' => -1,
see: Pagination Parameters
Hi I have a custom post type called 'support-team-doc' and a custom taxonomy called 'support_team_docs' which have categories as follows:
Support Teams Group
-- Accounts
-- IT
-- Marketing
-- Risk & Compliance
Each category has numerous posts; I do not want posts from the sub-categories to display on any of the categories pages; atm any posts in the sub categories 'accounts, it, marketing, risk-compliance' are being displayed like this: 'support-teams-group'; I have tried the following:
<?php
$termsTextarea = get_queried_object();
$args = array(
'post_type' => 'support-team-doc',
'tax_query'=>
array(
'taxonomy' => 'support_team_docs',
'field' => 'slug',
'terms' => $termsTextarea->slug,
'include_children' => false,
),
);
$query1 = new WP_Query( $args );
while ( $query1->have_posts() ) : $query1->the_post();
get_template_part( 'content', 'support_team_docs' );
endwhile; ?>
I do not know what I am doing wrong.
tax_query is a multi-dimensional array... you're missing one array element in your current code. Try this:
<?php
$termsTextarea = get_queried_object();
$args = array(
'post_type' => 'support-team-doc',
'tax_query'=>
array(
array(
'taxonomy' => 'support_team_docs',
'field' => 'slug',
'terms' => $termsTextarea->slug,
'include_children' => false
)
)
);
$query1 = new WP_Query( $args );
while ( $query1->have_posts() ) : $query1->the_post();
get_template_part( 'content', 'support_team_docs' );
endwhile; ?>