Ok, this is probably an easy one. But I can't seem to figure it out for some reason.
I have a custom post type called: Beachevents.
There i have a couple of events. I also have a custom taxonomy called: Thema.
When making my beachevent pages (not posts) i created some types of thema's (themes). Like: Strand Spellen (the slug is strand-spellen).
Now I want to make a loop that display's only strand-spellen with thumbnail and all that stuff.
Does anyone know how I go about this?
I tryed some codes like these but do don't do the trick.
$args = array(
'post_type' => 'beachevents',
'posts_per_page'=> -1,
'tax_query' => array(
array(
'taxonomy' => 'strand-spellen',
'field' => 'slug',
'terms' => 'all'
)
)
);
$products = new WP_Query( $args );
if( $products->have_posts() ) {
while( $products->have_posts() ) {
$products->the_post();
?>
<div class='content'>
<h2><?php the_title(); ?></h2>
</div>
<?php
}
}
else {
echo 'There seems to be a problem, please try searching again or contact customer support!';
}
Thanks!
You're close!
In your tax_query, taxonomy needs to refer to 'beachevents' and terms needs to refer to 'strand-spellen'.
So, your code will look like this:
'tax_query' => array(
array(
'taxonomy' => 'thema',
'field' => 'slug',
'terms' => 'strand-spellen'
)
)
For more information on building your queries, you may find the WP_Query documentation useful - there's a section in there on taxonomy queries.
Thanks to Tim for his help. Here is my full code for people who encounter this same problem.
<?php $args = array(
'post_type' => 'beachevents',
'posts_per_page'=> -1,
'orderby' => 'title',
'order' => 'ASC',
'tax_query' => array(
array(
'taxonomy' => 'thema',
'field' => 'slug',
'terms' => 'strand-spellen'
)
)
);
$products = new WP_Query( $args );
if( $products->have_posts() ) {
while( $products->have_posts() ) {
$products->the_post();
?>
<div class='content'>
<h2><?php the_title(); ?></h2>
</div>
<?php
}
}
else {
echo 'There seems to be a problem, please try searching again or contact customer support!';
} ?>
Including ordered by title and ASC. Hope I coded it correctly...
Related
(first of all i'm new with php)
i have a wordpress blog where i have written about some ppl and i added their names as tags in a costum taxonomy to avoid multi language field with polylang.
I was looking for a loop that allow to show, for every tag, the title of post where tag exist.
Example => post's name: "what a chef can do?" (tagged Alex P.)
result: -Alex P. "what a chef can do?" 05/10/2020 - "Tomatos in the world" 15/12/2020.
For every person(tag) i have to make an Accordion.
i've started to write code but i'm not looking for a solution and i'm getting lost.
<?php $tag_args = array(
'orderby' => 'title',
'order' => 'ASC'
);
?>
<ul id="Genre-List">
<li>DEFAULT</li>
<?php
$terms = get_terms(
array(
'taxonomy' => 'chef',
'hide_empty' => false,
)
);
// Check if any term exists
if ( ! empty( $terms ) && is_array( $terms ) ) {
// Run a loop and print them all
print_r($terms);
foreach ( $terms as $term ) {
?>
<li><a href="<?php echo esc_url( get_term_link( $term ) ) ?>">
<?php echo $term->name; ?>
</a></li>
<?php
}
}
?>
Thanks if anyone can help about this.. im really getting mad.
If you are looking to get all posts which have any custom taxonomy term associated with it, you will need to query posts using a tax_query rather than simply retrieve all of the terms.
// get 'chef' taxonomy
$terms = get_terms([
'taxonomy' => 'chef',
'hide_empty' => false,
]);
// get any posts with one of the 'chef' taxonomy terms associated with it
$query = new WP_Query([
'post_type' => 'post',
'tax_query' => [[
'taxonomy' => 'chef',
'field' => 'term_id',
'terms' => array_column($terms, 'term_id'),
'operator' => 'IN',
]],
]);
// loop and output posts
while ($query->have_posts()) {
$query->the_post();
var_dump(get_the_title());
}
I'm trying to display all posts from taxonomy. I have posts type(Docs) and taxonomy (type_docs) I'm trying to list all posts from that post type. My code is like this but it is not working
<?php
$custom_terms = get_terms('type_docs');
foreach($custom_terms as $custom_term) {
wp_reset_query();
$args = array('post_type' => 'docs',
'tax_query' => array(
array(
'taxonomy' => 'type_docs',
'field' => 'slug',
'terms' => $custom_term->slug,
),
),
);
$loop = new WP_Query($args);
if($loop->have_posts()) {
echo '<h2>'.$custom_term->name.'</h2>';
while($loop->have_posts()) : $loop->the_post();
echo ''.get_the_title().'
<br>';
endwhile;
}
}
?>
I’m trying to only list all posts from taxonomy, can somebody help with this?
I don't get exactly why you want to do thing like that but here is the solution:
<?php
// Get your custom terms
$custom_terms = get_terms('type_docs');
// Prepare the query
$args = array(
'post_type' => 'docs',
'posts_per_page' => -1,
'tax_query' => array()
);
// Prepare the array to receive terms tax_query
$terms_query = array();
foreach($custom_terms as $custom_term) {
// Add terms to the tax query
$args['tax_query'][] = array(
'taxonomy' => 'type_docs',
'field' => 'slug',
'terms' => $custom_term->slug,
);
}
// Get the posts (or you could do your new WP_Query)
$posts = get_posts($args);
// Display
foreach($posts as $post) : setup_postdata($post); ?>
<?php the_title(); ?><br>
<?php endforeach; ?>
I've got Two custom Post Types set up in Wordpress, One being called Products and the other Suppliers. Slugs for these are 'product' and one being 'supplier'.
These two post types share a custom taxonomy called Suppliers which slug is 'supplier-tax'.This then has lots of children which are the different suppliers.
Basically, What I am trying to do is when you are on a single post page for a product, I need to pull in a post for the supplier as well. I thought that the best way to do this with how I have set it up is to select the appropiate supplier from the supplier taxonomy when on the product page. And this then queries and gets post from the 'Supplier' Post ttype with the same selected Taxonomy.
I wrote this query which brings in posts from the taxonomy, however It needs me to tell it which taxonomy and which slug etc to bring in plus it doesnt query the different post type which makes it useless, however it was a start:
<?php
$the_query = new WP_Query( array(
'post_type' => 'product',
'tax_query' => array(
'taxonomy' => 'supplier-tax',
'field' => 'slug',
'terms' => 'supplier_1',
),
) );
while ( $the_query->have_posts() ) :
$the_query->the_post(); ?>
<?php the_title(); ?>
<?php the_post_thumbnail('full'); ?>
<?php the_content(); ?>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
I've tried to adapt and include parts from queries I've fond on previous sources but I can't seem to crack it. This is my attempt at it:
<?php
$terms = wp_get_post_terms( $post->ID, 'supplier-tax' );
if($terms){
$supplier_terms = array();
foreach ($terms as $term){
$supplier_terms[] = $term->slug;
}
$original_query = $wp_query;
$wp_query = null;
$wp_query = new WP_Query( array(
'post_type' => 'supplier',
'tax_query' => array(
array(
'taxonomy' => 'supplier-tax',
'field' => 'slug',
'terms' => $supplier_terms, //the taxonomy terms I'd like to dynamically query
'posts_per_page' => '-1'
),
),
'orderby' => 'title',
'order' => 'ASC'
) );
if ( have_posts() ): ?>
<?php while (have_posts() ) : the_post(); ?>
<?php the_title(); ?>"><?php the_title(); ?>
<?php endwhile; ?>
<?php endif;
$wp_query = null;
$wp_query = $original_query;
wp_reset_postdata();
}
?>
Has anyone got any ideas on what I'm doing wrong or how I can make this work?
I managed to find a solution to my problem, whilst I don't think it is the cleanest markup, it works and does the job.
<?php
$the_query = new WP_Query( array(
'post_type' => 'product',
'tax_query' => array(
'taxonomy' => 'supplier-tax',
),
) );
while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php
$terms = get_the_terms( $post->ID, 'supplier-tax');
foreach ( $terms as $term ) {
$termID[] = $term->term_id;
}
echo $termID[0];
?>
<?php
$my_query2 = new WP_Query( array(
'post_type' => 'supplier',
'tax_query' => array(
'field' => 'slug',
'terms' => '$termID',
),
) ); ?>
<?php while ($my_query2->have_posts()) : $my_query2->the_post(); ?>
<?php the_title(); ?>
<?php the_post_thumbnail('full'); ?>
<?php the_content(); ?>
<?php endwhile; ?>
I've been running in to a problem.
I'm editing a custom wordpress theme. I want to make some adjusments to the FAQ page (created with custom post type).
Right now every subject of question (= also category) is showing only 5 answers (these are posts).
I wanted to know, how I could increate this number, so instead of 5, show 10 or rather, show all answers per subject.
This is the code:
archive-faq.php
<?php $terms = get_terms( 'faq_category' ); ?>
<?php foreach( $terms as $term ):
$args = array (
'post_type' => 'faq',
'tax_query' => array(
array(
'taxonomy' => 'faq_category',
'field' => 'id',
'terms' => $term->term_id,
),
),
);
$query = new WP_Query( $args ); ?>
<h1><?php echo $term->name; ?></h1>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<div class="toggle-holder">
<h5 class="toggle"><span><?php the_title(); ?></span></h5>
<div class="toggle-box">
<div>
<?php the_content(); ?>
</div>
</div>
</div>
<?php endwhile; ?>
<?php endforeach; ?>
<?php wp_reset_postdata(); ?>
An important part is:
<div class="toggle-box">
<div>
<?php the_content(); ?>
</div>
</div>
Where the_content() , is showing the posts that are in a certain category.
However it's nowhere to be found why , the page is only showing up to 5 posts (answers) and not any more ?
Things I've tried:
$query = new WP_Query( $args );
$query_posts('post_per_page=3'); ?>
Also:
putting 'showposts' => 8 under 'terms' => $term->term_id,
And I also tried:
$query = new WP_Query( $args ); ?>
<?PHP
query_posts( array(
'workcap' => $all_post_terms,
'showposts' => 8,
'caller_get_posts' => 1,
'post__not_in' => $do_not_duplicate ) ); ?>
->> In summary:
Why does the page only show up to 5 posts ?
And how do I change this property ?
PS. If you want to see the page: http://goo.gl/UnWRTz
The argument is posts_per_page and not post_per_page. Its a typo?
Try this:
$args = array (
'post_type' => 'faq',
'posts_per_page' => -1,
//for now try without this taxonomy, if it works try with it.
/*'tax_query' => array(
array(
'taxonomy' => 'faq_category',
'field' => 'id',
'terms' => $term->term_id,
),
),*/
);
Btw, you can find more info here: https://codex.wordpress.org/Class_Reference/WP_Query
I'm trying to create a loop that loads posts from a custom taxonomy and a specific category , i have used this code below but it doesn't show anything
<ul class="row">
<?php
$latest_apps = new WP_Query(array(
'post_type' => array('product','post'),
'posts_per_page' => 30,
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'category',
'field' => 'id',
'terms' => array( get_option("free_apps_category") )
),
array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => array( get_option("premium_apps_category") )
)
)
));
$c = 0;
while ($latest_apps->have_posts()) : $latest_apps->the_post();
$c++;
// post is even
if( $c % 2 == 0) {
?>
<?php the_post_thumbnail("post"); ?>
</li>
<?php
}
// post is odd
else {
?>
<li class="content-box-post">
<?php the_post_thumbnail("post"); ?>
<?php
}
endwhile;
?>
</ul>
i have used static id numbers instead of get_option('free_apps_category') and get_option('premium_apps_category') but it still does not work
please help , thank u so much
regards
If it does not show you nothing, it is because you have no posts that match your condition.
And reading the code fast One can see that your query is for a post either normal or under product post type that is both on the premium_apps_category AND free_apps_category . Are you sure that you actually have one ?