Taxonomy archive + query attachments = duplicate results [wordpress] - php

I'm trying to get the attachments of a specific term (in its archive page).
But the results are showing the resulting images 5 times instead of one.
I have multiple loops in this page - one to show related posts, another to show related products (custom post), and this one to show related images. Custom posts and posts are working nicely, but I can't show the attachments in the right way. :S
<?php $queried_object = get_queried_object();
$term_id = $queried_object->term_id;
$args = array(
'post_status' => 'inherit',
'numberposts' => 0,
'post__not_in' => array_merge($do_not_duplicate,get_option( 'sticky_posts' )),
'post_type' => 'attachment',
);
$args['tax_query'] = array(
array(
'taxonomy' => 't-arte',
'terms' => $term_id,
'field' => 'id',
),
); ?>
<?php $t = $data['t-arte'];
$array = explode(" ", $t);
$array = array_unique($array);?>
<?php $media_query = array_unique($array); ?>
<?php $media_query = get_posts($args);
if( !empty( $media_query ) ) :
foreach ($media_query as $media_query) :
global $post; $post = $media_query;
setup_postdata($media_query);
?>
<div id="archivespage-media-item">
<?php $attachments = get_posts( $args );
if ( $attachments ) {
foreach ( $attachments as $attachment ) {
echo '<div id="imagem">';
the_attachment_link( $attachment->ID, true );
echo '</div>';
}
}?>
</div>
<?php endforeach;else :?>
<p>Ainda não temos nenhuma imagem relacionada :(</p>
</div>
<?php endif; ?>
<?php wp_reset_query();?>'

I've done these and its working!
The result will show all attachments in a specific term inside term's archive page.
<?php $queried_object = get_queried_object();
$term_id = $queried_object->term_id;
global $wp_query;
$original_query['tax_query'] = array(
array(
'taxonomy' => 't-arte',
'terms' => $term_id,
'field' => 'id',
),);
$original_query = (array) $wp_query;
$attach_query = array(
'post_type'=> array( 'attachment' ),
'post_status' => array( null ));
$args = array_merge($original_query['query_vars'], $attach_query);
$media_query = new WP_Query( $args )?>
<?php if($media_query->have_posts()) :
while ($media_query->have_posts() ) : $media_query->the_post();
if( $post->ID == $do_not_duplicate ) continue; ?>
<div id="archivespage-media-item">
<div id="imagem">
<?php echo wp_get_attachment_link($attachment->ID, 'bigger-thumb');?>
</div>
</div>
<?php endwhile; else: ?>
//do stuff
</div>
https://wordpress.stackexchange.com/questions/29635/how-to-create-an-attachments-archive-with-working-pagination

Related

Get wordpress attachments ID's separated by commas of current post

i have created custom post type named "Person" and i need to get all attachments ID's separated by commas of current post in loop.
Here i have a code that loops the "person" post type posts and code that shows every posts attachments ID's, but thing is that that those ID returns like this "2713271227112710" i need it to return seperated by commas like this "2713,2712,2711,2710"
Here is my code:
<?php
$args = array(
'post_type' => 'person',
'post_status' => 'publish',
'posts_per_page' => 24,
'order' => 'ASC'
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); ?>
<div class="col-md-4">
<div class="person">
<?php if ( $post->post_type == 'person' && $post->post_status == 'publish' ) {
$attachments = get_posts( array(
'post_type' => 'attachment',
'posts_per_page' => -1,
'post_parent' => $post->ID,
'exclude' => get_post_thumbnail_id()
) ); ?>
<?php
if ( $attachments ) {
foreach ( $attachments as $attachment ) {
$thumbimg = wp_get_attachment_url( $attachment->ID, 'full', false );
$attachment_id = attachment_url_to_postid( $thumbimg );
echo $attachment_id; //here outputs the attachemnts ID's of current post
}
}
}
?>
<?php echo do_shortcode( '[vc_images_carousel images="'.$attachment_id.'" img_size="large" speed="3000" autoplay="yes" hide_pagination_control="yes" hide_prev_next_buttons="yes" wrap="yes"]' ); ?>
<h3><?php the_title(); ?></h3>
<?php the_content(); ?>
</div>
</div>
<?php endwhile;
wp_reset_postdata(); ?>
Use implode in combination with an array_map, to return an array of IDs, and then convert it to a string of IDs, separated by a comma.
So instead of this:
foreach ( $attachments as $attachment ) {
$thumbimg = wp_get_attachment_url( $attachment->ID, 'full', false );
$attachment_id = attachment_url_to_postid( $thumbimg );
echo $attachment_id; //here outputs the attachemnts ID's of current post
}
Try this:
$attachment_ids = implode(',', array_map(function($attachment){
return $attachment->ID;
}, $attachments));
echo $attachment_ids;

Display html for selected products or products categories

Hello I am using advanced custom field plugin for displaying some html only for selected products or only for products for selected categories in option page with relationship picker. My result instead is displaying for all products. This is what I have:
$products = get_field('products_picker', 'option');
$categories = get_field('categories_picker', 'option');
$prom_img = get_field('prom_img', 'option');
if ( $products ) {
foreach( $products as $p ):
if( $post->ID == $p->ID ):
<img src="<?php echo $prom_img['url']; ?>">
endif;
endforeach;
}
if ( $categories ) {
foreach( $categories as $term ):
$category = get_term( $term );
if( has_term( $category, 'product_cat', $post )) {
?>
<img src="<?php echo $prom_img['url']; ?>">
endif;
endforeach;
}
In similar cases I create a new wp_query passing the array with the post IDs like below:
$posts = get_field('products_picker', 'option');
$new_query = new WP_Query(array(
'post_type' => array('post'),
'post__in' => $posts,
'orderby' => 'post__in',
));
if ( $new_query->have_posts() ) :
while ( $new_query->have_posts() ) : $new_query->the_post();
//your code here
endwhile;
endif;
For the categories part you can use a query like this:
$categories = get_field('categories_picker', 'option');
$args = array(
'post_type' => 'post',
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'term_id',
'terms' => $categories[0]
)
)
);
$cat_query = new WP_Query($args);
I've not tested this in Wordpress, but the first thing I would try is to rename the $post variable that pulls the ACF field to '$posts' (plural).
Then change the foreach loop as follows.
$posts = get_field('products_picker', 'option');
$prom_img = get_field('prom_img', 'option');
if ( $posts ) {
foreach( $posts as $post ):
setup_postdata($post);
$sales_html = '<div class="promo-badge test"><img src=' . $prom_img['url'] . '></div>';
wp_reset_postdata();
endforeach;
}
That might fix it... I'm not sure you won't need to turn the foreach loop into an actual Wordpress style loop (the type with while(have_posts()): the_post(); etc. like in #kaize answer below)
See here if my solution didn't help: https://www.advancedcustomfields.com/resources/querying-relationship-fields/

WooCommerce - Fetch specific product category in page template

I would like to fetch WooCommerce product on the basis of product category on my page template.
Lets say I have a category mattress. I want to fetch all the products related to that mattress category.
Here is my code:
<?php
// Start the Loop.
while ( have_posts() ) : the_post();
// Include the page content template.
get_template_part( 'content', 'page' );
// If comments are open or we have at least one comment, load up the comment template.
if ( comments_open() || get_comments_number() ) {
comments_template();
}
endwhile;
?>
How can I achieve this?
Thanks.
— — ( Update 2 of the August 8th ) — —
Finally a good alternative is to use has_term(); wordpress function to filter all products of some category in an if statement. If you have multiple categories you can embed them in an array using also has_term(); function.
As I have said before below on a comment, I think that your problem is here:
// Include the page content template.
get_template_part( 'content', 'page' );
So I will replace it with some custom code to show you that the condition is working fine:
<?php
$args = array(
'post_type' => 'product',
'posts_per_page' => -1
);
$loop = new WP_Query( $args );
// Start the Loop
If($loop->have_posts()){
while ( $loop->have_posts() ) : $loop->the_post();
// Here it is your product category
if ( has_term( 'mattress', 'product_cat', $loop->post ); ) {
// from here I display products thumbnails and name
echo '<div class="woocommerce-product" style="padding:5px; float:left;">';
if (has_post_thumbnail( $product_id )) {
echo get_the_post_thumbnail($loop->post->ID, 'shop_catalog');
} else {
echo '<img src="'.woocommerce_placeholder_img_src().'" alt="Placeholder" width="300px" height="300px" />';
}
echo '<div class="product-name">' . $loop->post->post_name . '</div>';
echo '</div>';
}
endwhile;
}
// If needed
wp_reset_query();
wp_reset_postdata();
?>
It also works without condition: has_term( 'mattress', 'product_cat', $loop->post ); replacing $args array by this one:
$args = array(
'post_type' => 'product',
'posts_per_page' => -1, // you can set here number of post per page
'tax_query' => array( array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => 'mattress '
) )
);
This code is tested and fully functional and it goes on function.php file of your active child theme or theme.
Reference:
Display different custom fields for different categories in WooCommerce
WordPress Function Reference - has_term()
— — ( Update 1 - july 9th ) — —
Or you can also use has_category(); function to filter in an if statement this way:
<?php
global $post;
// Start the Loop
while ( have_posts() ) : the_post();
if ( has_category( 'mattress', $post ); ) {
// Include the page content template.
get_template_part( 'content', 'page' );
// If comments are open or we have at least one comment, load up the comment template.
if ( comments_open() || get_comments_number() ) {
comments_template();
}
}
endwhile;
?>
You could try this:
<?php
$args = array(
'post_type' => 'product',
'posts_per_page' => -1, // you can set here number of post per page
'tax_query' => array( array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => 'mattress '
) )
);
$loop = new WP_Query($args);
If($loop->have_posts()){
while($loop->have_posts()) {
$loop->the_post();
// Include the page content template.
get_template_part( 'content', 'page' );
// If comments are open or we have at least one comment, load up the comment template.
if ( comments_open() || get_comments_number() ) {
comments_template();
}
}
}
// If needed
wp_reset_query();
wp_reset_postdata();
?>
Below code should work.
<?php
$query = new WP_Query('posts_per_page=5&post_type=product&product_cat=mattress');
If($query->have_posts()){
while($query->have_posts()) {
$query->the_post();
// Your Code
}
}
wp_reset_query();
wp_reset_postdata();
?>
<ul>
<?php
$taxonomy = 'product_cat';
$orderby = 'title';
$show_count = 0;
$pad_counts = 0;
$hierarchical = 1;
$title = '';
$empty = 0;
$args = array(
'taxonomy' => $taxonomy,
'orderby' => $orderby,
'show_count' => $show_count,
'pad_counts' => $pad_counts,
'hierarchical' => $hierarchical,
'title_li' => $title,
'hide_empty' => $empty
);
$all_categories = get_categories( $args );
foreach ($all_categories as $cat) {
$category_id = $cat->term_id;?>
<li><?php echo $cat->name;?></li>
<?php }?>
</ul>

Retrieving post images from wordpress

my problem is that this code below retrieve images as attachments only for first four posts. For others it retrieves only post title. And each end every post is equal and stored same way in database and in backend of a wordpress site. And it retrieves first four and image of I think 16th post.
$myposts = get_posts(array(
'category' => $_POST["kategorija"],
'post_type' => 'post',
'posts_per_page' => -1
)
);
?>
<ul>
<?php
foreach ( $myposts as $post ) : setup_postdata( $post );
$title = $post->post_title;
$date = $post->post_date;
$content = $post->post_content;
$status = $post->post_status;
?>
<li>
<?php
$args = array(
'post_type' => 'attachment',
'numberposts' => -1,
'post_status' => null,
'post_parent' => $post->ID
);
$attachments = get_posts( $args );
if ( $attachments ) {
foreach ( $attachments as $attachment ) {
echo wp_get_attachment_image( $attachment->ID, 'thumbnail' );
}
}
?>
<h2><?php echo $title; ?> </h2>
<form enctype="multipart/form-data" action="oglas.php" method="POST">
<input type="hidden" name="idKategorije" value="<?php echo $post->ID; ?>" />
<input type="submit" value="selektuj" />
</form>
</li>
<?php
endforeach; ?>
</ul>
Well since you already get the posts in your first get_posts, I don;t see why you need another one.
This is from this page in the Codex:
====
Show attachments for the current post[edit]
Do this inside The Loop (where $post->ID is available).
<?php
$args = array( 'post_type' => 'attachment', 'posts_per_page' => -1, 'post_status' =>'any', 'post_parent' => $post->ID );
$attachments = get_posts( $args );
if ( $attachments ) {
foreach ( $attachments as $attachment ) {
echo apply_filters( 'the_title' , $attachment->post_title );
the_attachment_link( $attachment->ID , false );
}
}
?>

Get Post from another post type that shares the same taxonomy?

I've got Two custom Post Types set up in Wordpress, One being called Products and the other Suppliers. Slugs for these are 'product' and one being 'supplier'.
These two post types share a custom taxonomy called Suppliers which slug is 'supplier-tax'.This then has lots of children which are the different suppliers.
Basically, What I am trying to do is when you are on a single post page for a product, I need to pull in a post for the supplier as well. I thought that the best way to do this with how I have set it up is to select the appropiate supplier from the supplier taxonomy when on the product page. And this then queries and gets post from the 'Supplier' Post ttype with the same selected Taxonomy.
I wrote this query which brings in posts from the taxonomy, however It needs me to tell it which taxonomy and which slug etc to bring in plus it doesnt query the different post type which makes it useless, however it was a start:
<?php
$the_query = new WP_Query( array(
'post_type' => 'product',
'tax_query' => array(
'taxonomy' => 'supplier-tax',
'field' => 'slug',
'terms' => 'supplier_1',
),
) );
while ( $the_query->have_posts() ) :
$the_query->the_post(); ?>
<?php the_title(); ?>
<?php the_post_thumbnail('full'); ?>
<?php the_content(); ?>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
I've tried to adapt and include parts from queries I've fond on previous sources but I can't seem to crack it. This is my attempt at it:
<?php
$terms = wp_get_post_terms( $post->ID, 'supplier-tax' );
if($terms){
$supplier_terms = array();
foreach ($terms as $term){
$supplier_terms[] = $term->slug;
}
$original_query = $wp_query;
$wp_query = null;
$wp_query = new WP_Query( array(
'post_type' => 'supplier',
'tax_query' => array(
array(
'taxonomy' => 'supplier-tax',
'field' => 'slug',
'terms' => $supplier_terms, //the taxonomy terms I'd like to dynamically query
'posts_per_page' => '-1'
),
),
'orderby' => 'title',
'order' => 'ASC'
) );
if ( have_posts() ): ?>
<?php while (have_posts() ) : the_post(); ?>
<?php the_title(); ?>"><?php the_title(); ?>
<?php endwhile; ?>
<?php endif;
$wp_query = null;
$wp_query = $original_query;
wp_reset_postdata();
}
?>
Has anyone got any ideas on what I'm doing wrong or how I can make this work?
I managed to find a solution to my problem, whilst I don't think it is the cleanest markup, it works and does the job.
<?php
$the_query = new WP_Query( array(
'post_type' => 'product',
'tax_query' => array(
'taxonomy' => 'supplier-tax',
),
) );
while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php
$terms = get_the_terms( $post->ID, 'supplier-tax');
foreach ( $terms as $term ) {
$termID[] = $term->term_id;
}
echo $termID[0];
?>
<?php
$my_query2 = new WP_Query( array(
'post_type' => 'supplier',
'tax_query' => array(
'field' => 'slug',
'terms' => '$termID',
),
) ); ?>
<?php while ($my_query2->have_posts()) : $my_query2->the_post(); ?>
<?php the_title(); ?>
<?php the_post_thumbnail('full'); ?>
<?php the_content(); ?>
<?php endwhile; ?>

Categories