'wildcard' for all taxonomy terms wordpress - wordpress

probably a silly question, but I can't seem to find a 'wildcard' to query all the terms in a custom taxonomy.
here is the query
<?php
$args = array(
'posts_per_page' => -1,
'post_type' =>'job_listing',
'orderby' => 'ASC',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'job_listing_region',
'field' => 'slug',
'terms' => EVERYTHING,
),
array(
'taxonomy' => 'job_listing_category',
'field' => 'slug',
'terms' => EVERYTHING,
),
),
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
?>
What goes in where 'EVERYTHING' is in order to output for all the terms in this taxonomy?
Thanks in advance
NK

OK, it's a little around the houses but I have at least got a working version of what I need.
What I wanted was to dynamically query from two selects (two taxonomies) either a specific term from each (or both) and ANY term from each (or both). That was why I wanted the 'code / value' for something to put in the arguments for ALL terms.
What I did was run four conditional statements around the values given in from the query strings.
if one select == "all" and the other == "all" then we simply don't need to run the taxonomy query because we a querying everything anyway. So when we have just one taxonomy to query and the other is all, we simply miss it out.
This does feel a little junior, but it does work and seems solid so far.
Here is the code (i put the select values into query strings and read them in)
<!-- BEFORE WE START WE NEED THE POST ID PASSED IN THE QUERY STRING -->
<?php $queryStringValueforState = htmlspecialchars($_GET["state"]); ?>
<?php $queryStringValueforIndustry = htmlspecialchars($_GET["industry"]); ?>
<!-- SEARCH RESULTS -->
<h2>Results for: <?php echo $queryStringValueforState; ?> <?php echo $queryStringValueforIndustry; ?></h2>
<?php
// IF BOTH SELECTS ARE NOT INPUTTED (all)
if ($queryStringValueforState == "All regions" && $queryStringValueforIndustry == "All industries"){
$args = array(
'posts_per_page' => -1,
'post_type' =>'job_listing',
'orderby' => 'ASC'
);
}
// IF BOTH SELECTS HAVE INPUTTED VALUE
if ($queryStringValueforState !== "All regions" && $queryStringValueforIndustry !== "All industries"){
$args = array(
'posts_per_page' => -1,
'post_type' =>'job_listing',
'orderby' => 'ASC',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'job_listing_region',
'field' => 'slug',
'terms' => $queryStringValueforState,
),
array(
'taxonomy' => 'job_listing_category',
'field' => 'slug',
'terms' => $queryStringValueforIndustry,
),
),
);
}
// IF REGION SELECT HAS INPUTTED VALUE AND INDUSTRY HAS NOT
if ($queryStringValueforState !== "All regions" && $queryStringValueforIndustry == "All industries"){
$args = array(
'posts_per_page' => -1,
'post_type' =>'job_listing',
'orderby' => 'ASC',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'job_listing_region',
'field' => 'slug',
'terms' => $queryStringValueforState,
),
),
);
}
// IF INDUSTRY SELECT HAS INPUTTED VALUE AND REGION HAS NOT
if ($queryStringValueforState == "All regions" && $queryStringValueforIndustry !== "All industries"){
$args = array(
'posts_per_page' => -1,
'post_type' =>'job_listing',
'orderby' => 'ASC',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'job_listing_category',
'field' => 'slug',
'terms' => $queryStringValueforIndustry,
),
),
);
}
// END CONDITIONAL STATEMENTS AND CONTINUE WITH LOOP
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
?>
<div class="single_job_wrapper">
<div class="job-company-logo"></div>
<div class="job-content">
<h3><?php the_title(); ?></h3>
<?php echo apply_filters( 'the_job_description', get_the_content() ); ?>
<a class="button" href="<?php the_permalink(); ?>">Read More</a>
</div>
</div>
<?php endwhile; ?>

I manage to solve my similar issue with a default term value like this:
$query = new WP_Query([
'tax_query' => [
[
'taxonomy' => 'tipos',
'field' => 'name',
'terms' => $term ?: ['default_term1', 'default_term2'],
]
],
]);

Related

Wordpress filter by taxonomy and expired acf date field

I have a custom post type "events" with a custom taxonomy "events_cat" which has category-1, category-2 etc.
This is my code in taxonomy.php
<?php
$today = date('Ymd');
$cat_slug = get_queried_object()->slug;
$args = array(
'post_type' => 'events',
'nopaging' => true,
'meta_key' => 'expiry',
'tax_query' => array(
array(
'taxonomy' => 'events_cat',
'field' => 'slug',
'terms' => $cat_slug
)
),
'meta_query' => array(
array(
'key' => 'expiry',
'value' => $today,
'compare' => '>='
)
)
);
$events = new WP_Query( $args );
if ( $events->have_posts() ) :
?>
<?php while ( $events->have_posts() ) : $events->the_post(); ?>
// my code
<?php endwhile; wp_reset_postdata(); ?>
<?php endif; ?>
What I need is to filter my post by category and if it is expired or not at the same time.
I explain better: I have one page with all expired posts and another one with all not expired posts.
What I need is that if I click on a category in the "expired page" I just want the list of the expired posts and vice versa.
I was thinking of a condition to change the compare value depending on whether or not the post has expired but I can't figure out how I can do it.
EDIT
I tried such a thing but of course it doesn't work because $expiry is null.
if($expiry >= $today) {
$compare = '>=';
} else {
$compare = '<';
}
$args = array(
'post_type' => 'events',
'nopaging' => true,
'meta_key' => 'expiry',
'tax_query' => array(
array(
'taxonomy' => 'events_cat',
'field' => 'slug',
'terms' => $cat_slug
)
),
'meta_query' => array(
array(
'key' => 'expiry',
'value' => $today,
'compare' => $compare
)
)
);
Does anyone have a better idea on how this can be done or point me in the right direction?
I finally solved the problem by using the $_GET super global variable. I don't know if it's the most correct method but it's the only one I've found. If anyone has a better idea please let me know.
I added "?expired" to the category links of expired posts so I can do this:
$today = date('Ymd');
$compare = '>=';
if( isset($_GET['expired'])) {
$compare = '<';
}
args = array(
'post_type' => 'events',
'nopaging' => true,
'meta_key' => 'expiry',
'tax_query' => array(
array(
'taxonomy' => 'events_cat',
'field' => 'slug',
'terms' => $cat_slug
)
),
'meta_query' => array(
array(
'key' => 'expiry',
'value' => $today,
'compare' => $compare
)
)
);

my custom taxonomy does not work and only returns the most recent posts

I have a custom post type called cme-education which I am trying to query to get only the posts that have a term of covid-curriculum however, the tax query does not work and only returns the most recent post. See my code below. my file is called block_random-post-types.php
$args = array(
'post_type'=> 'cme-education',
// 'orderby'=>'rand',
'posts_per_page' => '2',
'post_status' => 'publish',
// 'exclude=' => $currentID,
'tax_query' => array(
'taxonomy' => 'covid-curriculum',
'field' => 'slug',
'terms' => array('covid-19'),
'operator' => 'IN'
),
);
$covid = new WP_Query($args);
while ($covid->have_posts()) : $covid->the_post();
?>
<h2><?php the_title(); ?></h2>
<?php echo "launch activiry"; ?>
<?php
endwhile;
//wp_reset_postdata();
?>
use below shared code:-
**
$args = array(
'post_type' => 'cme-education',
'posts_per_page' => '2',
'post_status' => 'publish'
'tax_query' => array(
array(
'taxonomy' => 'covid-curriculum',
'field' => 'slug',
'terms' => 'covid-19',
),
),
);
**

Group Woocommerce product by it's category filter by custom taxonomy

I have custom taxonomy device with my product with Woocommerce default product category.
I would like to list my product under each product category search by custom taxonomy.
For example:
Device have value: devise-1 and devise-2
When search with devise-1, the list will be:
category -1
product-1
product-2
category - 2
product-1
product-2
Hello how are you? I understand you'd like to export your posts by searching by custom category type. If so, I think it helps. I'm sorry for my bad english.
Sorry if I understand wrong your question.
$your_slug_custom_tax = ( isset($_GET['{your_var_custom_tax}']) ) ? $_GET['{your_var_custom_tax}'] : '';
$your_config = array(
'post_type' => 'product', //If woocommerce native
'posts_per_page' => '-1', //You can change for your preference
'post_status' => 'publish', //status of post or product for show
'tax_query' => array() // this is for you custom search
);
if($your_slug_custom_tax != ''){
$your_config['tax_query'][] = array(
'taxonomy' => '{your slug of taxonomy}',
'field' => 'slug',
'terms' => $your_slug_custom_tax // your taxonomy for show post.
);
}
$your_config = get_posts($your_config);
if ( $your_config ) {
foreach ( $your_config as $mypost ):
print_r($mypost);
endforeach;
}
I have found a solution which meets my requirement but I think it expensive. because it's query DB for each category.
`
<?php
$args = array(
'orderby' => 'title',
'order' => 'ASC',
'hide_empty' => TRUE,
'taxonomy' => 'product_cat'
);
$product_categories = get_terms($args);
foreach ($product_categories as $product_category) : ?>
<h2 class="title ">
<?= $product_category->name; // Print Product categories ?>
</h2>
<?php
$args = array(
'posts_per_page' => -1,
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => $product_category->slug
),
array(
'taxonomy' => 'device',
'field' => 'slug',
'terms' => $device_type,
'operator' => 'IN'
)
),
'post_type' => 'product',
'orderby' => 'sku,'
);
$products = new WP_Query($args);
// Inner Product loop
while ($products->have_posts()): $products->the_post(); ?>
<?php wc_get_template_part('content', 'product'); ?>
<?php endwhile; ?>
`
Anyone have a simple solution. Thanks

how to call the posts of a different custom taxonomy of a category name in wordpress?

I have written a code for displaying all the posts of a particular category from a particular custom taxonomy. Here $term is the name of the category of which the posts are to be displayed. The following code is being given below :
<?php
$args = array(
'posts_per_page' => '-1',
'post_type' => 'brand',
'taxonomy' => 'brand-category',
'terms' => $term,
'category_name' => $term,
'order' => 'DESC'
);
$results = new wp_Query($args);
if(have_posts()){
while($results->have_posts()){
$results->the_post();
$image = wp_get_attachment_image_src( get_post_thumbnail_id(), 'large');
?>
<div class="item">
<img src="<?php echo $image; ?>" alt="pvc hose">
</div>
<?php }
}
?>
The above code is not displaying the images which I have given in the particular category in the particular taxonomy brand-category. Can anyone suggest corrections to this code?
For custom taxonomies you should use the tax_queryparameter. Your args should be like:
$args = array(
'posts_per_page' => -1,
'post_type' => 'brand',
'order' => 'DESC',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'brand-category',
'field' => 'slug',
'terms' => $term,
),
array(
'taxonomy' => 'brand-category',
'field' => 'name',
'terms' => $term,
),
),
);
Hope it helps!

Use meta value as taxonomy term in Wordpress query

I need to dynamically change the taxonomy term using custom meta. Below is where I've been starting from... makes sense to me but doesn't work.
<?php
$dynamic = rwmb_meta( $post->ID, blr_queryslug, true );
$args = array(
'posts_per_page' => -1,
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => 'music'
),
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => '$dynamic'
)
),
'post_type' => 'product'
);
$the_query = new WP_Query( $args );
// The Loop
while ( $the_query->have_posts() ) {
$the_query->the_post();
woocommerce_get_template_part( 'content', 'product' );
}
wp_reset_postdata();
?>
Change
'terms' => '$dynamic'
to
'terms' => $dynamic
PHP does not replace variables inside single quotes.

Resources