Use meta value as taxonomy term in Wordpress query - wordpress

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.

Related

WordPress: How can get all product of brand from specific a category with WooCommerce Plugin?

I am trying to print all products of a brand on from category page.
I find the wp_query but I can't print the current brand name of the brand to show all products in this page.
// get products
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'pwb-brand',
'field' => 'name',
'terms' => array ('NAME?????')
)
)
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
wc_get_template_part( 'content', 'product' );
endwhile;
} else {
echo __( 'not found anyhting.' );
}
wp_reset_postdata();
For example, i need to get all product of brand name (XXX) that fount in category name (YYY)
This is answer after search :
$args = array(
'post_type' => array('post','product'),
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'product_cat',
'terms' => 82,
'field' => 'id'
),
array(
'taxonomy' => 'brand',
'terms' => 81,
'field' => 'id'
),
)
);
query_posts($args);
if ( have_posts() ) {
while ( have_posts() ) {
the_post();
the_title();
}
}

Show user post count while using "ultimate member" and "co-author"

I'm using the plugin ultimate member and co-author
and in the author page in the post tab
i'm trying to show the numbers of posts the user has written with this line
$args = array(
'post_type' => 'post',
'tax_query' => array(
array(
'taxonomy' => 'author',
'field' => 'slug',
'terms' => $user_login
)
),
);
$author_query = new WP_Query( $args );
if ( $author_query->have_posts() ) :
while ( $author_query->have_posts() ) : $author_query->the_post();
// Do your presentation
endwhile;
endif;
Would appreciate your help for the correct code
You will need a counter.
$args = array(
'post_type' => 'post',
'tax_query' => array(
array(
'taxonomy' => 'author',
'field' => 'slug',
'terms' => $user_login
)
),
);
$author_query = new WP_Query( $args );
$count=0;
if ( $author_query->have_posts() ) :
while ( $author_query->have_posts() ) : $author_query->the_post();
// Do your presentation
$count++;
endwhile;
endif;
echo $count;

'wildcard' for all taxonomy terms 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'],
]
],
]);

Filter tax_query in my wordpress

I would like to know how i filters property area , i Try :
<?php
$args = array(
'post_type' => 'estate_property',
'post_status' => 'publish',
'tax_query' => array(
'taxonomy' => 'property_area',
'field' => 'slug',
'terms' => 'pigalle',
),
);
$selection = new WP_Query($args);
?>
But all show ! WHY ? lol
Thanks
Read up on Taxonomy Parameters
The tax_query parameter is a multi-dimensional array. You need to wrap it with another array:
<?php
$args = array(
'post_type' => 'estate_property',
'post_status' => 'publish',
'tax_query' => array(
array(
'taxonomy' => 'property_area',
'field' => 'slug',
'terms' => 'pigalle'
)
)
);
$selection = new WP_Query($args);
?>

Use variable inside of WP_query

I am trying to echo $zipcode_term inside of the 'tax_query' but it is not working...
wp_reset_query();
$zipcode_term = get_the_term_list( get_the_ID(), 'zipcode' );
$args = array(
'tax_query' => array(
array(
'taxonomy' => 'zipcode',
'field' => 'slug',
'terms' => '.$zipcode_term.'
)
)
);
$query = new WP_Query( $args );
I have echoed $zipcode_term and it displays it correctly so I know that part is working, how can I express it inside of the array correctly?
should 'terms' => '.$zipcode_term.' have the apostrophe and concatenation around your variable?
'terms' => $zipcode_term
?

Resources