Use variable inside of WP_query - wordpress

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
?

Related

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'],
]
],
]);

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.

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);
?>

Get all posts that have NO terms with WP_Query

I am writing a wordpress loop, and I want to get all the posts that have NO terms assigned to them. Is there an easy way to do it? Or do I really have to get all the term ID's and do an tax query like this :
// Get all the term id's
$terms = array();
$terms = getAllTheTerms();
// Create an arguments which get all the posts that do not have a term with any
// of the id's.
$args = array(
'post_type' => 'post',
'tax_query' =>
array(
'taxonomy' => 'actor',
'field' => 'id',
'terms' => $terms,
'operator' => 'NOT IN'
)
);
$query = new WP_Query( $args );
This seems like a stupid query because database wise it would be very easy to get all the posts without a query.
$terms = get_terms( $taxonomy, array('fields'=>'ids')); /* GET ALL TERMS FROM A TOXNOMY */
$args = array(
'tax_query' => array(
array(
'taxonomy' => $taxonomy,
'field' => 'term_id',
'terms' => $terms,
'operator' => 'NOT IN' /* DO THE MAGIC - Get all post that no in the taxonomy terms */
)
)
);
$the_query = new WP_Query( $args );
The question was: "I am writing a wordpress loop, and I want to get all the posts that have NO terms assigned to them. Is there an easy way to do it?"
Here is the answer: the following code replaces the code of the original poster and demonstrates the best solution.
$args = [
'post_type' => 'post',
'tax_query' => [
[
'taxonomy' => 'actor',
'operator' => 'NOT EXISTS',
],
],
];
$query = new WP_Query($args);
You may try this
$args = array(
'post_type' => 'post',
'tax_query' =>
array(
'taxonomy' => 'actor',
'field' => 'slug',
'terms' => '',
)
);
$query = new WP_Query( $args );

Resources