WP_Query posts get term list outside of loop? - php

Maybe I'm going about this incorrectly but I'm having an issue with getting info outside of the while loop:
<?php
$title = get_field('car_list_title');
$field = get_field('tax_field_selector');
$query = new WP_Query( array(
'post_type' => 'cars',
'taxonomy' =>'make',
'term' => $field->name,
'posts_per_page' => -1,
'orderby' =>'title',
'order' =>'ASC'
) );
$taxonomy = get_terms( array(
'taxonomy' => 'location',
'hide_empty' => true
) );
if ( $field || $query->have_posts() ) :
?>
<div class="c-cars">
<h2 class="c-cars_title u-t--underline--lightblue">
<?= $title; ?>
</h2>
<?php foreach( $taxonomy as $tax ) :
$tax_name = $tax->name;
?>
<div class="c-cars_row">
<h4 class="c-cars_location-title">
<?= $tax_name; ?>
</h4>
<div class="c-cars_cars">
<?php while ( $query->have_posts() ) : $query->the_post();
$title = get_the_title();
$link = get_permalink();
$image = get_field('car-picture');
$image_alt = get_field('car_alt');
$image_title = get_field('car_title');
$post_id = get_the_ID();
$terms = get_the_terms( $post_id, 'location', array( 'order' => 'DESC', 'hide_empty' => true));
$location = $terms[0]->name;
?>
<?php if( $location === $tax_name ) : ?>
<div class="c-cars_car">
<a href="<?= $link; ?>">
<img class="c-cars_car-image" src="<?= $image; ?>" alt="<?= $image_alt; ?>" title="<?= $image_title; ?>">
</a>
<h4 class="text-center">
<a href="<?= $link; ?>">
<?= $title; ?>
</a>
</h4>
</div>
<?php endif; ?>
<?php endwhile; wp_reset_postdata(); ?>
</div>
</div>
<?php endforeach; ?>
</div>
<?php endif; ?>
So what happens here is I get a list of the locations and all the cars in those locations:
Location 1:
Car
Car
Car
Location 2:
Car
Car
Car
Location 3:
Location 4:
Car
Car
Car
The problem here is, as an example, Location 3 shows up even though there's no "posts" in that term.
The while loop is only cars of a specific model, sorted into what location they are at.
I'm not really sure how to filter out the empty locations.
I do:
<?php if( $location === $tax_name ) : ?>
Inside of the loop and that filters them out of the locations but still leaves the location title because it's outside of the while loop. If I were able to do this earlier up in the code it may work but I can't get the list of active terms outside of the while loop.
I'm kind of lost right now. Any ideas or suggestions? Thanks!

you can check using condition if has a term so show title either it will be blank try it below condition and mentioned in a comment if it's work or not.
has_terms
https://developer.wordpress.org/reference/functions/has_term/
function check if the post has terms.
if( has_term( $location, $tax_name ) ) {
// do something
}

Okay, I have updated your answer please try it below code. I have just get post count of terms and applied condition if terms has post count so show terms title name or if there is no post count of terms so the title will show blank.
<?php
$title = get_field('car_list_title');
$field = get_field('tax_field_selector');
$query = new WP_Query( array(
'post_type' => 'cars',
'taxonomy' =>'make',
'term' => $field->name,
'posts_per_page' => -1,
'orderby' =>'title',
'order' =>'ASC'
) );
$taxonomy = get_terms( array(
'taxonomy' => 'location',
'hide_empty' => true
) );
if ( $field || $query->have_posts() ) :
?>
<div class="c-cars">
<h2 class="c-cars_title u-t--underline--lightblue">
<?= $title; ?>
</h2>
<?php foreach( $taxonomy as $tax ) :
$tax_name = $tax->name;
$tax_post_count = $tax->count;
?>
<div class="c-cars_row">
if ( $tax_post_count > 0 ) : ?>
<h4 class="c-cars_location-title">
<?= $tax_name; ?>
</h4> <?php
else : ?>
<h4 class="c-cars_location-title">
<?= $tax_name = ''; ?>
</h4> <?php
endif; ?>
<div class="c-cars_cars">
<?php while ( $query->have_posts() ) : $query->the_post();
$title = get_the_title();
$link = get_permalink();
$image = get_field('car-picture');
$image_alt = get_field('car_alt');
$image_title = get_field('car_title');
$post_id = get_the_ID();
$terms = get_the_terms( $post_id, 'location', array( 'order' => 'DESC', 'hide_empty' => true));
$location = $terms[0]->name;
?>
<?php if( $location === $tax_name ) : ?>
<div class="c-cars_car">
<a href="<?= $link; ?>">
<img class="c-cars_car-image" src="<?= $image; ?>" alt="<?= $image_alt; ?>" title="<?= $image_title; ?>">
</a>
<h4 class="text-center">
<a href="<?= $link; ?>">
<?= $title; ?>
</a>
</h4>
</div>
<?php endif; ?>
<?php endwhile; wp_reset_postdata(); ?>
</div>
</div>
<?php endforeach; ?>
</div>
<?php endif; ?>

Related

Woocommerce how to display products which has two categories, one of them should be common

I am developing custom Woo shop theme. I want to display in front-page.php NEW COLLECTION products, it should be one block, and inside few smaller blocks with products ant their another vcategories, because, they will have common category NEW and another category depends BEDROOM/BATHROOM/CLOTHING, for now i have reahced display by one category.
<div class="col-md-12 row">
<?php
$args = array(
'post_type' => 'product',
'posts_per_page' => 4,
'product_cat' => 'clothing'
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
global $product;
$colors = explode(',',$product->get_attribute('color'));
$price = $product->get_price_html();
?>
<div class="col-md-3 d-flex flex-column">
<div class="homepage__single-product">
<a href="<?php the_permalink(); ?>">
<img src="<?php echo the_post_thumbnail_url(); ?>">
<p class="homapage__single-product-title"><?php the_title(); ?></p>
<p class="homapage__single-product-price"><?php echo $price ?></p>
<p class="homepage__single-product-color">
<?php foreach ($colors as $color) {
echo '<span class="attribute-color '. strtolower(trim($color)) .'"></span>';
}
?>
</p>
</a>
</div>
</div>
<?php
endwhile;
wp_reset_query();
?>
</div>
I hope that my question will be clear, i am adding image with design for better understanding. I think that it should be somethimg like this code, but this code should have NEW as product_cat in $args, and then inside loop somehow do same block but with other categories.
design shows how it should be
You can use tax_query to get products from multiple categories. check below code.
<div class="col-md-12 row">
<?php
$prod_categories = array( 'bedroom', 'bathrooms', 'clothing'); //category slugs
$args = array(
'post_type' => 'product',
'posts_per_page' => 4,
);
if (!empty($prod_categories)) {
$args['tax_query'] = array(
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => $prod_categories,
'operator' => 'IN',
));
}
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
global $product;
$colors = explode(',',$product->get_attribute('color'));
$price = $product->get_price_html();
?>
<div class="col-md-3 d-flex flex-column">
<div class="homepage__single-product">
<a href="<?php the_permalink(); ?>">
<img src="<?php echo the_post_thumbnail_url(); ?>">
<p class="homapage__single-product-title"><?php the_title(); ?></p>
<p class="homapage__single-product-price"><?php echo $price ?></p>
<p class="homepage__single-product-color">
<?php foreach ($colors as $color) {
echo '<span class="attribute-color '. strtolower(trim($color)) .'"></span>';
}
?>
</p>
</a>
</div>
</div>
<?php
endwhile;
wp_reset_query();
?>
</div>

wordpress has_post_thumbnail if/else statement not working

I have the following code, it seems to ignore the if/else for the displaying the thumbnail. It will display all thumbnails should a post have a thumbnail set however it will not show the placeholder if there is no thumbnail. Am I blind or is there something wrong with this?
<?php $x = 0;
$displayed = [];
function runQuery($args) {
global $displayed;
global $x;
$query = new WP_Query( $args );
if ($query->have_posts()) : while ($query->have_posts()) : $query->the_post();
$cat_terms = get_the_terms($post->id, 'product_cat');
$datagroups = '';
if ( in_array( get_the_ID(), $displayed ) ){
continue;
}
// update array with currently displayed post ID
$displayed[] = get_the_ID();
foreach ($cat_terms as $key => $cat) {
if (count($cat_terms) == ($key + 1)) {
$datagroups .= '"' . $cat->name . '"';
} else {
$datagroups .= '"' . $cat->name . '", ';
}
}
?>
<div class="flex-item product-single flex-cols-4" data-groups='[<?php echo $datagroups; ?>]' data-date-created="<?php the_modified_date('Y-m-d') ?>" data-title="<?php the_title() ?>">
<figure class="picture-item__inner">
<figcaption class="picture-item__title">
<a data-open="product-<?php echo $x; ?>">
<div class="product-wrap">
<div class="product-image">
<?php if( has_post_thumbnail() ) { ?>
<?php the_post_thumbnail('medium');?>
<?php } else { ?>
<img src="<?php echo get_template_directory_uri(); ?>/armco-old/assets/img/products/placeholder.jpg" alt="Coming Soon" />
<?php } ?>
</div>
<div class="product-title">
<p><?php the_title(); ?></p>
</div>
</div>
</a>
</figcaption>
</figure>
</div>
<?php $x ++;
endwhile;
endif;
wp_reset_postdata();
}?>
<?php if ( $terms && !is_wp_error( $terms ) ) {
foreach ( $terms as $term ) {
$args = array(
'post_type' => 'products',
'posts_per_page' => -1,
'orderby' => 'menu_order',
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => $term->slug,
),
),
'order' => 'asc',
);
runQuery($args);
}
} ?>
</div>
Any help would be appreciated as I cannot see what the issue is, when I remove the first if statement and just tell it to echo out the image it works with brining in the image however as soon as it's inside the else statement it doesn't work.
Due to whatever reasoning my original code did not work, so I swapped it to check if the get_the_post_thumbnail() function was empty or not:
<?php if( !empty(get_the_post_thumbnail()) ) { ?>
<?php the_post_thumbnail('medium');?>
<?php } else { ?>
<img src="<?php echo get_template_directory_uri(); ?>/armco-old/assets/img/products/placeholder.jpg" alt="Coming Soon" />
<?php } ?>

Php with WordPress how to call up permalinks to two separate posts

i'm building a recent posts function into a wordpress site, i've got it to call up two different featured images but they are both linking to the same post, can anyone see where i am going wrong?
<?php
$args = array(
'posts_per_page' => 2,
'order_by' => 'date',
'order' => 'desc'
);
$post = get_posts( $args );
if($post) {
$post_id = $post[0]->ID;
if(has_post_thumbnail($post_id)){
?>
<div class="grid_24">
<div class="grid_12">
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
<?php
echo get_the_post_thumbnail($page->ID, 'medium');
?>
</a>
</div>
<div class="grid_12">
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
<?php
echo get_the_post_thumbnail( $post_id,'medium');
?>
</a>
</div>
</div>
<?php
}
}
?>
you can use echo get_the_permalink($post->ID) to get the uri for the posts
So it looks like in your case you'd need
echo get_the_permalink($post[0]->ID);
and
echo get_the_permalink($post[1]->ID);
in the href
However you're probably better off creating a foreach loop to go through the posts from the get_posts function
https://developer.wordpress.org/reference/functions/get_the_permalink/
https://developer.wordpress.org/reference/functions/get_posts/
Okay, first of all, you are not looping the query you have made ( e.g $posts = get_posts( $args ); ) you are just displaying the 1st post's thumbnail and the thumbnail of the current page.
You need to loop the post like this :
<?php
$args = array(
'posts_per_page' => 2,
'order_by' => 'date',
'order' => 'desc'
);
$posts = get_posts( $args );
?>
<?php if ( !empty( $posts ) ) :?>
<div class="grid_24">
<?php foreach ( $posts as $post ) : ?>\
<?php if( has_post_thumbnail( $post->ID ) ) ?>
<div class="grid_12">
<a href="<?php echo esc_url( get_permalink( $post->ID ) ) ?>">
<?php echo get_the_post_thumbnail( $post->ID, 'size_here'); ?>
</a>
</div>
<?php endif; ?>
<?php endforeach?>
</div>
<?php endif;

Wordpress Query posts wrap each item in a div rather than an li and show descendants of current page

I am using the following to wrap through a list of posts as I want to display them within divs.
Despite using
global $post;
$currentPage = $post->ID;
and
'post_parent' => $currentPage,
Which has worked fine elsewhere. I can't get the page to only show children and grandchildren of this page.
<?php
global $post;
$currentPage = $post->ID;
// Get posts (tweak args as needed)
$args = array(
'post_parent' => $currentPage,
'post_type' => 'page',
'orderby' => 'menu_order',
'order' => 'ASC'
);
$posts = get_pages( $args );
?>
<?php foreach (array_chunk($posts, 1, true) as $posts) : ?>
<div class="column small-4 medium-4 large-4">
<?php foreach( $posts as $post ) : setup_postdata($post); ?>
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
<?php endforeach; ?>
</div>
<?php endforeach; ?>
I am using the code within a custom template.
I have also tried
<?php
global $post;
$currentPage = $post->ID;
$args=array(
'child_of' => $currentPage,
'post_type' => 'page'
);
$my_query = null;
$my_query = new WP_Query($args);
echo $currentPage;
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<?php $img = wp_get_attachment_image_src( get_post_meta($post->ID, 'image_or_video', true)); ?>
<?php $alt_text_for_logo = get_post_meta($post->ID, 'article_name', true); ?>
<?php $short_description = get_post_meta($post->ID, 'article_short_description', true); ?>
<div class="column small-12 medium-6 large-4 box">
<div>
<a href="<?php the_permalink() ?>" title="<?php the_title_attribute(); ?>" itemprop="url">
<?php if( $img ): ?>
<img src="<?php echo $img[0]; ?>" alt="<?php echo $alt_text_for_logo; ?>" />
<?php endif; ?>
<span><?php the_title(); ?></span>
</a>
</div>
</div>
<?php endwhile; } ?>
<?php wp_reset_query();?>
but this lists the pages I want followed by what appears to be 10 other random pages from the site's root though it doesn't list every page from the site.
Originally posted this solution as a comment, because I wasn't sure it was the only change that was necessary. Turned out it is, so here's the solution:
<?php
global $post;
$currentPage = $post->ID;
// Get posts (tweak args as needed)
$args = array(
'child_of' => $currentPage,
'post_type' => 'page',
'orderby' => 'menu_order',
'order' => 'ASC'
);
$posts = get_pages( $args );
?>
<?php foreach (array_chunk($posts, 1, true) as $posts) : ?>
<div class="column small-4 medium-4 large-4">
<?php foreach( $posts as $post ) : setup_postdata($post); ?>
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
<?php endforeach; ?>
</div>
<?php endforeach; ?>
The function get_pages doesn't seem to have post_parent as a valid argument. So you need to use child_of instead.
Reference to Codex
<?php
global $post;
$currentPage = $post->ID;
// Get posts (tweak args as needed)
$args = array(
'child_of ' => $currentPage,
'post_type' => 'page',
'orderby' => 'menu_order',
'order' => 'ASC'
);
$posts = get_pages( $args );
?>
https://codex.wordpress.org/Function_Reference/get_pages
To list All sub pages of current page
<?php
global $post;
$currentPage = $post->ID;
$args=array(
'post_parent' => $currentPage,//change
'post_type' => 'page',
'orderby' => 'menu_order',
'order' => 'ASC'
);
$my_query = null;
$my_query = new WP_Query($args);
echo $currentPage;
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<?php $img = wp_get_attachment_image_src( get_post_meta($post->ID, 'image_or_video', true)); ?>
<?php $alt_text_for_logo = get_post_meta($post->ID, 'article_name', true); ?>
<?php $short_description = get_post_meta($post->ID, 'article_short_description', true); ?>
<div class="column small-12 medium-6 large-4 box">
<div>
<a href="<?php the_permalink() ?>" title="<?php the_title_attribute(); ?>" itemprop="url">
<?php if( $img ): ?>
<img src="<?php echo $img[0]; ?>" alt="<?php echo $alt_text_for_logo; ?>" />
<?php endif; ?>
<span><?php the_title(); ?></span>
</a>
</div>
</div>
<?php endwhile; } ?>
<?php wp_reset_query();?>
only change on code
$args=array(
'post_parent' => $currentPage,//change
'post_type' => 'page',
'orderby' => 'menu_order',
'order' => 'ASC'
);
which display all child pages of current page with their Menu order
About Us
Contact Us
Gallery
Testimonial
Blog

Get categories for a single post in a custom post type

I'm trying to get an unformatted list (preferably a list of slugs) of the categories for a single post in a custom post type loop. This list will eventually serve as a class for a div ($CATEGORYSLUGSWILLEVENTUALLYGOHERE).
I've found a few different methods of getting a list of ALL of the categories for a custom post type, but not the ones specific to a single one. Here's what I have so far:
<?php $projects_loop = new WP_Query( array( 'post_type' => 'projects', 'orderby' => 'menu_order' ) ); ?>
<?php while ( $projects_loop->have_posts() ) : $projects_loop->the_post(); ?>
<div class="box <?php $CATEGORYSLUGSWILLEVENTUALLYGOHERE; ?>">
<div class="port-item-home">
<?php the_post_thumbnail( 'portfolio-home' ); ?>
<p><?php the_title(); ?></p>
</div>
</div>
<?php endwhile; ?>
And here's what I've tried so far to get the category list:
<?php
$args = array(
'orderby' => 'name',
'parent' => 0,
'taxonomy' => 'project-type'
);
$categories = get_categories( $args );
echo '<p> '.print_r(array_values($categories)).'something</p>'
?>
I have it returning the array - but the array is showing that it'll display all categories instead of the ones pertaining to that specific post.
I also tried:
<?php
//list terms in a given taxonomy (useful as a widget for twentyten)
$taxonomy = 'project-type';
$tax_terms = get_terms($taxonomy);
?>
<?php
foreach ($tax_terms as $tax_term) {
echo $tax_term->name;
}
?>
And that also displays all categories instead of the ones pertaining to the post.
Any suggestions??
Got it! Found this article that helped me out:
https://wordpress.org/support/topic/how-to-get-the-category-name-for-a-custom-post-type
<!-- The Query -->
<?php
$args = array(
'post_type' => 'my_post_type',
'posts_per_page' => -1,
'orderby' => 'menu_order' );
$custom_query = new WP_Query( $args );
?>
<!-- The Loop -->
<?php
while ( $custom_query->have_posts() ) :
$custom_query->the_post();
$terms_slugs_string = '';
$terms = get_the_terms( $post->ID, 'my_post_type' );
if ( $terms && ! is_wp_error( $terms ) ) {
$term_slugs_array = array();
foreach ( $terms as $term ) {
$term_slugs_array[] = $term->slug;
}
$terms_slugs_string = join( " ", $term_slugs_array );
}
?>
<div class="box<?php echo $terms_slugs_string ?>">
<div class="port-item-home">
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail( 'portfolio-home' ); ?>
</a>
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</div>
</div>
<?php endwhile; ?>

Categories