I'm trying to create a query where I create multiple categories (taxonomies) in a custom post type, and then on the homepage query based on specific which is working fine. Currently I have 3 taxonomies:
current-specials
meineke-difference
featured
I have already written code that pulls these. The problem I'm running into is that on the homepage it needs to only pull these posts when they are also attached to the "featured" taxonomy. So an example of standard logic for this would be:
if taxonomy = current-specials AND featured then success else fail
But what it's doing is pulling them all because the current code is OR, and I need AND
Thoughts? (code below)
<?php
$post_type = 'coupons';
$tax = 'coupons_category';
$tax_terms = get_terms($tax);
if ($tax_terms):
foreach ($tax_terms as $tax_term):
echo '<div id="'.$tax_term->slug.'" class="coupon-box '.$tax_term->slug.'">';
$args = array(
'post_type' => $post_type,
"$tax" => array($tax_term->slug, 'featured'),
'post_status' => 'publish',
'posts_per_page' => -1,
'caller_get_posts' => 1
);
$myQuery = null;
$myQuery = new WP_Query($args);
if($myQuery->have_posts()):
while ($myQuery->have_posts()) : $myQuery->the_post();
$price = get_field('price_image', $myQuery->ID);
$print = get_field('print', $myQuery->ID);
$product = get_field('product_box_image', $myQuery->ID);
$title = get_the_title();
$content = get_the_content();
echo '<div class="fourty9 left box center">';
echo '<h1>'.$title.'</h1>';
echo '<p class="center"><img src="'.$price.'" /></p>';
echo '<p>'.$content.'</p>';
echo '<p class="center">Print Coupon</p>';
echo '<p class="center"><img src="'.$product.'" alt="Filter"></p>';
echo '</div>';
endwhile;
endif;
echo '</div>';
wp_reset_query();
endforeach;
endif;
?>
You may try this (tax - use taxonomy slug. Deprecated as of Version 3.1 in favor of 'tax_query')
$args = array(
'post_type' => 'coupons',
'posts_per_page' => -1,
'caller_get_posts' => 1,
'tax_query' => array(
array(
'taxonomy' => 'coupons_category',
'field' => 'slug',
'terms' => array( 'current-specials', 'featured' ),
'operator' => 'AND'
)
)
);
$query = new WP_Query( $args );
Related
I'm trying to solve one issue with one of the functions which I've created recently. Below you will find a piece of code which creates a dropdown field in which each represents specific 'partner company'. The code works perfectly.
function fnc_select_1() {
$args = array(
'post_type' => 'freecourses',
'orderby' => 'meta_value',
'status' => 'publish',
'order' => 'DESC',
'meta_key' => 'validation_date',
);
$the_query = new WP_Query($args);
if ($the_query->have_posts()):
while ($the_query->have_posts()) : $the_query->the_post();
if(have_rows('validation_date')):
while (have_rows('validation_date')) : the_row();
if( get_sub_field('company')) {
$firm_array[] = get_sub_field('company');
}
endwhile;
wp_reset_query();
else :
// no rows found
endif;
endwhile;
$firms = array_unique($firm_array);
natsort($firms);
echo '<select type="text" class="form-control" name="company" id="company">';
echo '<option value="">company</option>';
foreach ($firms as $firm) {
echo '<option value="'. $firm .'">';
echo $firm;
echo '</option>';
}
echo '</select>';
endif;
}
add_shortcode( 'select_1', 'fnc_select_1' );
Because of certain reasons I've decided to enhance this function by adding:
extra filtering based on custom taxonomy (called "type")
based on a page in which function will be displayed I wanted to limit / filter those companies by specific terms of this taxonomy.
So I've created this kind of code:
if (is_page(29557) ):
$childno = '205';
elseif (is_page(29640) ):
$childno = '206';
endif;
and simply to my wp_query args I thought I may add:
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'type',
'child_of' => $childno
),
),
but it doesn't work. It do not display the dropdown at all..
Where is the issue then?
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 want to display product by category with dropdown
and i made this code but why the value not going inside the ?
function rakitpc() {
$args = array(
'posts_per_page' => -1,
'product_cat' => 'Tshirts',
'hide_empty' => 0,
'post_status' => 'publish',
'post_type' => 'product',
'orderby' => 'title',
'echo' => '0'
);
$products = new WP_Query( $args );
$output = '<select>';
// foreach ( $products as $product ) {
while ( $products->have_posts() ) {
$products->the_post();
$aa = the_permalink($post);
echo $aa;
$bb = the_title($post);
$output .= "<option value=\"<?php the_permalink(); ?>\"> <?php the_permalink(); ?></option>";
}
wp_reset_query();
$output .='</select>';
return $output;
}
add_shortcode( 'gege', 'rakitpc' );
This is the output That I get:
My reference: How do I display a "category products drop down in a wordpress page" in Woocommerce 2.5.2
Try the following that will give you a dropdown with the permalinks as <option> values displaying the product names (see the screenshot at the end).
You will need to set your category name (or categories names) in the array inside the funtion.
function rakitpc() {
// HERE below, define your Product category names
$term_names = array('T-shirts');
// The WP_Query
$query = new WP_Query( array(
'posts_per_page' => -1,
'post_type' => 'product',
'post_status' => 'publish',
'hide_empty' => 0,
'orderby' => 'title',
'tax_query' => array( array(
'taxonomy' => 'product_cat', // for a Product category (or 'product_tag' for a Product tag)
'field' => 'name', // can be 'name', 'slug' or 'term_id'
'terms' => $term_names,
) ),
'echo' => '0'
) );
$output = '<select>';
// foreach ( $products as $product ) {
if ( $query->have_posts() ) :
while ( $query->have_posts() ) : $query->the_post();
$permalink = get_permalink($query->post->ID);
$title = $query->post->post_title;
$output .= '<option value="' . $permalink . '">' . $title . '</option>';
endwhile;
wp_reset_postdata();
$output .='</select>';
else :
$output = '<p>No products found<p>';
endif;
return $output;
}
add_shortcode( 'gege', 'rakitpc' );
Code goes in function.php file of your active child theme (or active theme). Tested and works.
I'm new to creating wordpress shortcodes and have one just about working the way I want. Missing something specific. Currently I am able to put the following on any page - [children] and it pulls a query of all posts from the custom post type "children" I would like to add the option to add the category id within the shortcode - something like [children category="8"] Here is the code I have so far:
add_shortcode( 'children', 'display_custom_post_type' );
function display_custom_post_type(){
$args = array(
'post_type' => 'children',
'post_status' => 'publish',
'orderby' => 'title',
'order' => 'ASC',
);
$string = '';
$query = new WP_Query( $args );
if( $query->have_posts() ){
while( $query->have_posts() ){
$query->the_post();
$string .= '<div id="childWrapper"><div id="childImage">' . get_the_post_thumbnail() . '</div><div style="clear: both;"></div><div id="childName">' . get_the_title() . '</div><div style="clear: both;"></div></div>';
}
}
wp_reset_postdata();
return $string;
}
Secondary - is it possible for it to show posts in multiple categories, but only where the posts are in each of the categories. For example showing a list of children, who fall under a category for critical care and surgery needed.
Any help would be greatly appreciated.
You should refer Shortcode function from WordPress Codex. Assuming your taxonomy name is category, I made some changes in your current code it should work as per your requirements.
/**
* [children category="5,7,8"]
*/
add_shortcode( 'children' , 'display_custom_post_type' );
function display_custom_post_type($atts) {
$atts = shortcode_atts( array(
'category' => ''
), $atts );
//If category is multiple: 8,9,3
$categories = explode(',' , $atts['category']);
$args = array(
'post_type' => 'children',
'post_status' => 'publish',
'orderby' => 'title',
'order' => 'ASC',
'posts_per_page'=> -1,
'tax_query' => array( array(
'taxonomy' => 'category',
'field' => 'term_id',
'terms' => $categories
) )
);
$string = '';
$query = new WP_Query( $args );
if( ! $query->have_posts() ) {
return false;
}
while( $query->have_posts() ){
$query->the_post();
$string .= '<div id="childWrapper"><div id="childImage">' . get_the_post_thumbnail() . '</div><div style="clear: both;"></div><div id="childName">' . get_the_title() . '</div><div style="clear: both;"></div></div>';
}
wp_reset_postdata();
return $string;
}
I'm having a little trouble with a custom taxonomy template. I inherited a site that was developed by someone else and they use "Types" plugin to add some custom taxonomies.
Goal:
to have an archive template that shows only posts with a certain taxonomy term in it at example-domain.com/people/harrison-ford
Problem:
This code is bringing in posts that do not have the taxonomy selected.
Here's my full code:
<?php
$year = get_post_meta($post->ID, 'year', true);
$post_type = 'post';
$tax = 'people';
$tax_terms = get_terms( $tax );
if ($tax_terms) {
$args = array(
'post_type' => $post_type,
'people' => 'harrison-ford',
"$tax" => $tax_term->slug,
'post_status' => 'publish',
'posts_per_page' => -1,
'caller_get_posts'=> 1,
'orderby' => 'date',
'order' => DESC
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) : ?>
<h2 class="wwNews"><?php echo $tax_term->name; ?> News</h2>
<?php while ( $my_query->have_posts() ) : $my_query->the_post(); ?>
<-- display stuff -->
<?php endwhile; // end of loop ?>
<?php endif; // if have_posts()
wp_reset_query();
}
?>
What are you expecting here? "$tax" is going to to be 'people' =>, which is going to overwrite 'harrison-ford' to the value of $tax_term->slug.
'people' => 'harrison-ford',
"$tax" => $tax_term->slug,
Furthermore, I don't know of any custom argument called people, I'm pretty sure that you want tax_query:
'tax_query' => array(
'taxonomy' => 'people',
'terms' => array('harrison-ford', $tax_term->slug)
)
Which will give you the results of all people matching harrison-ford and the value of $tax_term->slug within the taxonomy of people