using post ID inside reverse ACF relationship querie - php

I’m using relationship field on one of my custom post type (releases).
all my realease have a relationship field with “artists” post, so all my releases are associated with one artist.
now i’m trying to perform a reverse query to display all the “releases” associated with the artist selected in my relationship field.
here is my code so far :
<?php
$releases = get_posts(array(
'post_type' => 'releases',
'numberposts'=> -1,
'post__not_in' => array( $post->ID ),
'meta_query' => array(
array(
'key' => 'artist',
'value' => 191,
'compare' => 'LIKE'
)
)
));?>
<?php if( $releases ): ?>
<?php foreach( $releases as $release ): ?>
<div class="col-xs-2 text-center">
<a href="<?php echo get_permalink( $release->ID ); ?>" class="related_releases" title="<?php echo get_the_title($release->ID); ?>">
<img src="<?php echo get_the_post_thumbnail_url($release->ID, 'full'); ?>">
</a>
</div>
<?php endforeach; ?>
<?php endif; ?>
with this code, all the releases from artist ID “191” are displayed on y custom post type.
the problem is that I’m trying to display the releases from the artist associated with the “release” post.
what I’m trying to do is to get the ID of the artist inside my querie, like this (width “get_the_ID()” instead of “191”)
<?php
$releases = get_posts(array(
'post_type' => 'releases',
'numberposts'=> -1,
'post__not_in' => array( $post->ID ),
'meta_query' => array(
array(
'key' => 'artist',
'value' => get_the_ID(),
'compare' => 'LIKE'
)
)
));?>
but I get the ID of the “release” post, not the ID of the artist.
can anyboby help me with this ?
hope you understand my issue, sorry for my bad english

Will this work instead in your array
'value' => get_the_author_meta( $user_id ),

Try below code. i think this will work.
<?php
$artist = get_post_meta (get_the_ID(),'artist');
$releases = get_posts(array(
'post_type' => 'releases',
'numberposts'=> -1,
'post__not_in' => array( $post->ID ),
'meta_query' => array(
array(
'key' => 'artist',
'value' => $artist[0],
'compare' => 'LIKE'
)
)
));?>
<?php if( $releases ): ?>
<?php foreach( $releases as $release ): ?>
<div class="col-xs-2 text-center">
<a href="<?php echo get_permalink( $release->ID ); ?>" class="related_releases" title="<?php echo get_the_title($release->ID); ?>">
<img src="<?php echo get_the_post_thumbnail_url($release->ID, 'full'); ?>">
</a>
</div>
<?php endforeach; ?>
<?php endif; ?>

I've found a solution :
<?php
$posts = get_field('artist'); if( $posts ):
foreach(array_slice($posts, 0, 1) as $post):
setup_postdata($post);
$post_ID = get_the_ID();
endforeach;
wp_reset_postdata();
endif;
$releases = get_posts(array(
'post_type' => 'releases',
'numberposts'=> -1,
'post__not_in' => array( $post->ID ),
'meta_query' => array(
array(
'key' => 'artist',
'value' => $post_ID,
'compare' => 'LIKE'
)
)
));?>
<?php if( $releases ): ?>
<?php foreach( $releases as $release ): ?>
<div class="col-xs-2 text-center">
<a href="<?php echo get_permalink( $release->ID ); ?>" class="related_releases" title="<?php echo get_the_title($release->ID); ?>">
<img src="<?php echo get_the_post_thumbnail_url($release->ID, 'full'); ?>">
</a>
</div>
<?php endforeach; ?>
<?php endif; ?>

Related

WordPress: posts_per_page not working on archive pages

I've a custom loop which should display 3 projects per category in a random order.
The problem is, that the loop always shows all projects in that category.
I've tested the the code on a single page and it works how it should and only showed 3 projects.
So I guess it has something to do with the archive page?!
Here's ist the loop itself:
<?php
$args_thema = array (
'post_type' => array( 'project' ),
'orderby' => 'rand',
'posts_per_page' => 3,
'paged' => $paged,
//'ignore_sticky_posts' => 1,
'tax_query' => array(
array(
'taxonomy' => 'project-category',
'field' => 'slug',
'terms' => $category->slug,
),
),
);
$query_thema = new WP_Query( $args_thema ); if ( $query_thema->have_posts() ) : ?>
<ul class="">
<?php $i = 0; while ( $query_thema->have_posts() ) : $query_thema->the_post(); $i++; // echo $i; ?>
<li class="">
<a class="url uid" href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>">
<?php the_title(); ?>
</a>
</li>
<?php endwhile; ?>
</ul>
<?php else : ?>
<?php endif; wp_reset_postdata(); ?>
And here's the full code (I guess not that relevant):
<?php
$args = array('hide_empty' => '0', 'taxonomy' => 'project-category', 'orderby' => 'name', 'order' => 'ASC', 'parent' => 0 );
$categories = get_categories($args);
foreach($categories as $category) {
?>
<div class="row">
<div class="col-lg-4">
<?php
$term_child = get_term_by('slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
$args_child = array('hide_empty' => '0', 'taxonomy' => 'project-category', 'orderby' => 'name', 'order' => 'ASC', 'parent' => $category->term_id );
?>
<h4><?php echo $category->name; ?></h4>
</div>
<div class="col-lg-8">
<?php
$args_thema = array (
'post_type' => array( 'project' ),
'orderby' => 'rand',
'posts_per_page' => 3,
'paged' => $paged,
//'ignore_sticky_posts' => 1,
'tax_query' => array(
array(
'taxonomy' => 'project-category',
'field' => 'slug',
'terms' => $category->slug,
),
),
);
$query_thema = new WP_Query( $args_thema ); if ( $query_thema->have_posts() ) : ?>
<ul class="">
<?php $i = 0; while ( $query_thema->have_posts() ) : $query_thema->the_post(); $i++; // echo $i; ?>
<li class="">
<a class="url uid" href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>">
<?php the_title(); ?>
</a>
</li>
<?php endwhile; ?>
</ul>
<?php else : ?>
<?php endif; wp_reset_postdata(); ?>
</div>
</div>
<?php } ?>
Sorry, it was an extra function which sets the archive posts to 99.
Istead of is_post_type_archive I used is_archive

Wordpress list categories with min 5 posts on it

I list my categories in a page using the following code:
<?php
$terms = get_terms( array(
'taxonomy' => 'category',
'hide_empty' => true,
) );
$count = count($terms);
$categories = array();
if ($count > 0) :
foreach ($terms as $term) {
$args = array(
'post_type' => 'post',
'posts_per_page' => 1,
'show_count' => 1,
'orderby' => 'rand',
'post_status' => 'publish',
'tax_query' => array(
array(
'taxonomy' => 'categorys',
'field' => 'slug',
'terms' => $term->slug
)
)
);
$post_from_category = new WP_Query( $args );
if( $post_from_category->have_posts() ){
$post_from_category->the_post();
}else{}
$term->slug;
$term->name; ?>
<article id="post-<?php the_ID(); ?>" <?php post_class('thumb-block'); ?>>
<a href="<?php echo bloginfo('url'); ?>?categories=<?php echo $term->slug; ?>" title="<?php echo $term->name; ?>">
<header class="entry-header">
<span class="category-title"><?php echo $term->name; ?></span>
</header><!-- .entry-header -->
</a>
</article><!-- #post-## -->
<?php }
endif; ?>
My question is: How can I list the categories which contains minimum 5 posts only?
Thank you very much!
Inside your foreach loop you can just check to make sure $term->count is greater than or equal to 5. There's also a few other things I noticed:
You define a $categories array but don't appear to use it again.
Try not to mix if: endif; and if{} statements - stick to a syntax for ease-of-maintenance.
You can just check to see if $terms gets set to a truthy value instead of checking its count.
Are you sure your Tax Query is set properly? It's checking the taxonomy "categorys" which is a typo of "categories"
You don't need an empty else{} loop after your ->have_posts() check
You don't need to instantiate the $term object variables before the article.
You should consider using home_url() instead of bloginfo('url')
All that said, this should get you started:
<?php
$term_args = array(
'taxonomy' => 'category',
'hide_empty' => true,
);
if( $terms = get_terms( $term_args ) ){
foreach( $terms as $term ){
if( $term->count >= 5 ){
$args = array(
'post_type' => 'post',
'posts_per_page' => 1,
'show_count' => 1,
'orderby' => 'rand',
'post_status' => 'publish',
'tax_query' => array(
array(
'taxonomy' => 'categorys',
'field' => 'slug',
'terms' => $term->slug
)
)
);
$post_from_category = new WP_Query( $args );
if( $post_from_category->have_posts() ){ $post_from_category->the_post(); ?>
<article id="post-<?php the_ID(); ?>" <?php post_class( 'thumb-block' ); ?>>
<a href="<?= home_url( "?categories={$term->slug}" ); ?>" title="<?= $term->name; ?>">
<header class="entry-header">
<span class="category-title"><?= $term->name; ?></span>
</header>
</a>
</article>
<?php }
}
}
}
?>

I have issue with loop

IN my code I am fetching data behalf of category id .but problem is that <div id="<?php echo $term_id = $term->term_id;?>" class="tabcontent">. $term_id does not change in this div.Here is my code .
<?php
//loop the names of the slugs for the portfolio_categories
$terms = get_terms( array ( 'taxonomy' => 'portfolio_categories', 'hide_empty' => false, 'parent' => 0, 'orderby' => 'date', 'order' => 'DESC' ));
foreach ($terms as $term) {
$slug= $term->slug;
$term_id = $term->term_id;
$args = array(
'post_type' => 'Portfolio',
'tax_query' => array(
array(
'taxonomy' => 'portfolio_categories',
'terms' => $slug,
'field' => 'slug',
)
),
'orderby' => 'ID',
'order' => 'DESC',
'posts_per_page' => -1
);
}
?>
<?php
$posts_query = new WP_Query( $args );
if (have_posts()) :?>
<div id="<?php echo $term_id = $term->term_id;?>" class="tabcontent">
<?php
while ( $posts_query->have_posts() ) : $posts_query->the_post();?>
<?php echo '<div class="col-1-3">';?>
<?php echo $term_id = $term->term_id;?>
<?php $thumbnail = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), "size" );?>
<div class="wrap-col" >
<a class="fancybox" href="<?php echo $thumbnail[0]; ?>" data-fancybox-group= gallery>
<img src="<?php echo $thumbnail[0];?>"></a> </div>
</div>
<?php echo '</div>';?>
<?phpendwhile;endif;?>
</div>
Hi every one I have solved this issue .problem was only .div was not manage in under for loop. so this is answer if you trying to get custom post by custom category.
<?php
$taxonomy = 'portfolio_categories';
$args = array(
'hide_empty' => false
);
global $terms;
global $terms;
$terms = get_terms( 'portfolio_categories', $args );
?>
<div class="tab">
<?php $i = 0; ?>
<?php foreach ( $terms as $term ) { ?>
<?php $i++; ?>
<button class="tablinks" onclick="openCity(event, '<?php echo $term->term_id;?>')"
<?php if ($i == 1) { echo 'id="defaultOpen"'; } ?> ><?php echo $term->name;?></button>
<?php } ?>
</div>
<?php
//loop the names of the slugs for the portfolio_categories
$terms = get_terms(array('taxonomy' => 'portfolio_categories', 'hide_empty' => false, 'parent' => 0, 'orderby' => 'date', 'order' => 'DESC'));
foreach ($terms as $term) {
$slug = $term->slug;
$term_id = $term->term_id;
$args = array(
'post_type' => 'Portfolio',
'tax_query' => array(
array(
'taxonomy' => 'portfolio_categories',
'terms' => $slug,
'field' => 'slug',
)
),
'orderby' => 'ID',
'order' => 'DESC',
'posts_per_page' => -1
);
//}//Instead closing of here
?>
<?php
$posts_query = new WP_Query($args);
if (have_posts()) :
?>
<div id="<?php echo $term_id = $term->term_id; ?>" class="tabcontent">
<?php while ($posts_query->have_posts()) : $posts_query->the_post(); ?>
<?php echo '<div class="col-1-3">'; ?>
<?php echo $term_id = $term->term_id; ?>
<?php $thumbnail = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), "size"); ?>
<div class="wrap-col" >
<a class="fancybox" href="<?php echo $thumbnail[0]; ?>" data-fancybox-group= gallery>
<img src="<?php echo $thumbnail[0]; ?>"></a> </div>
<?php echo '</div>'; ?>
<?php
endwhile;
endif;?>
</div>
<?php }//Close foreach here
?>
Inorder to get every term->term_id close the foreach at the end
//loop the names of the slugs for the portfolio_categories
$terms = get_terms(array('taxonomy' => 'portfolio_categories', 'hide_empty' => false, 'parent' => 0, 'orderby' => 'date', 'order' => 'DESC'));
foreach ($terms as $term) {
$slug = $term->slug;
$term_id = $term->term_id;
$args = array(
'post_type' => 'Portfolio',
'tax_query' => array(
array(
'taxonomy' => 'portfolio_categories',
'terms' => $slug,
'field' => 'slug',
)
),
'orderby' => 'ID',
'order' => 'DESC',
'posts_per_page' => -1
);
//}//Instead closing of here
?>
<?php
$posts_query = new WP_Query($args);
if (have_posts()) :
?>
<div id="<?php echo $term_id = $term->term_id; ?>" class="tabcontent">
<?php while ($posts_query->have_posts()) : $posts_query->the_post(); ?>
<?php echo '<div class="col-1-3">'; ?>
<?php echo $term_id = $term->term_id; ?>
<?php $thumbnail = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), "size"); ?>
<div class="wrap-col" >
<a class="fancybox" href="<?php echo $thumbnail[0]; ?>" data-fancybox-group= gallery>
<img src="<?php echo $thumbnail[0]; ?>"></a> </div>
</div>
<?php echo '</div>'; ?>
<?php
endwhile;
endif;
}//Close foreach here
?>
</div>

Show most recent post - if "yes" chosen as radio button value

Here is the setup for my radio button:
I can't seem to get the posts that are valued at yes to display in the loop..
Here is my loop:
<?php
$args = array(
'numberposts' => 1,
'post_type' => 'event',
'posts_per_page' => '1',
'meta_key' => 'sponsored_event',
'meta_value' => 'yes'
);
$the_query = new WP_Query( $args );
?>
<?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div class="sponsored-event">
<div class="sponsored-image" style="background-image: url(<?php the_field( 'event_image' ); ?>);"></div>
<div class="sponsored-info">
<h2>Sponsored Event</h2>
<h1><strong><?php the_title(); ?></strong></h1>
<p><strong>Date</strong></p><br>
<p class="place"><?php the_field( 'event_location' ); ?></p>
<p class="time"><?php the_field( 'event_time' ); ?></p>
<p><?php the_field( 'excerpt' ); ?></p>
</div>
</div>
<?php endwhile; else: ?>
<?php endif; ?>
This is what worked for me:
<?php
$args = array(
'post_type' => 'event',
'showposts' => 1,
'orderby' => 'date',
'meta_query' => array(
array(
'key' => 'sponsored_event',
'value' => 1,
'compare' => 'LIKE'
)
)
);
$the_query = new WP_Query( $args );
?>
<?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<a href="<?php the_permalink(); ?>"><div class="sponsored-event">
<div class="sponsored-image" style="background-image: url(<?php the_field( 'event_image' ); ?>);">
</div>
<div class="sponsored-info">
<h2>Sponsored Event</h2>
<h1><strong><?php the_title(); ?></strong></h1>
<p><strong>Date</strong></p><br>
<p class="place"><?php the_field( 'event_location' ); ?></p>
<p class="time"><?php the_field( 'event_time' ); ?></p>
<p><?php the_field( 'excerpt' ); ?></p>
</div>
</div></a>
<?php endwhile; else: ?>
<?php endif; ?>
if ( have_posts() ) should reference the query: if ( $the_query->have_posts() ).
Also, as has already been mentioned, you should use 'posts_per_page' => 1, instead of numberposts.
Try using a meta_query to accomplish this. Update your $args with something like the following:
$args = array(
'post_type' => 'event',
'posts_per_page' => '1',
'meta_query' => array(
array(
'key' => 'sponsored_event',
'compare' => 'LIKE',
'value' => 'yes',
),
),
);
You also need to update if ( have_posts() ) to if ( $the_query->have_posts() ).
More information about meta_query can be found here: https://codex.wordpress.org/Class_Reference/WP_Meta_Query
Instead of using both post_per_page and number posts you can use any one of your choice
Try any of the Methods as available below. As per the ACF the Method 1 is suggested but as for as the WordPress you can retrieve information based on Method 2 also.
Method:1
$args = array(
'posts_per_page' => 1,
'post_type' => 'event',
'meta_key' => 'sponsored_event',
'meta_value' => 'yes'
);
$the_query = new WP_Query( $args );
<?php if ($the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
//Your Code over here to Manipulate
<?php endwhile; else: ?>
<?php endif; ?>
Method:2
Try the meta query with compare as = operator in the meta_query.
$args = array(
'post_type' => 'event',
'posts_per_page' => '1',
'meta_query' => array(
array(
'key' => 'sponsored_event',
'compare' => '=',
'value' => 'yes',
),
),
);
I tested the code on my machine and it works fine! Just (as already mentioned from others) the if() statement is wrong, change it to:
if ( $the_query->have_posts() )
But now I have a really silly question... After creating the ACF field, have you saved some posts (events) with the meta field (yes or no) ? If not, WP wont find anything because the postmeta is not saved into the DB.
Did you took a look into the DB if the postmeta is saved correctly?

Woocommerce get products by specific categories

I'm developing themes for woocommerce, I need help to retrieve information from products by categories, example,
I want to display products from 'Shirt' Categories limit by 3 items, here's the code from woo themes that show products by featured products,( i tried to change to display by categories and not work)
<ul class="featured-products products">
<?php
$args = array( 'post_type' => 'product', 'posts_per_page' => 6, 'meta_query' => array( array('key' => '_visibility','value' => array('catalog', 'visible'),'compare' => 'IN'),array('key' => '_featured','value' => 'yes')) );
$i = 0;
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); $_product; $i++;
if ( function_exists( 'get_product' ) ) {
$_product = get_product( $loop->post->ID );
} else {
$_product = new WC_Product( $loop->post->ID );
}
?>
<li class="product <?php if ($i%3==0) echo ' last'; if (($i-1)%3==0) echo ' first'; ?>">
<div class="inner">
<?php woocommerce_show_product_sale_flash( $post, $_product ); ?>
<a href="<?php echo get_permalink( $loop->post->ID ) ?>" title="<?php echo esc_attr($loop->post->post_title ? $loop->post->post_title : $loop->post->ID); ?>">
<?php if (has_post_thumbnail( $loop->post->ID )) echo get_the_post_thumbnail($loop->post->ID, 'shop_catalog'); else echo '<img src="'.woocommerce_placeholder_img_src().'" alt="Placeholder" />'; ?>
<h3><?php the_title(); ?></h3>
<span class="price"><?php echo $_product->get_price_html(); ?></span>
</a>
<?php woocommerce_template_loop_add_to_cart( $loop->post, $_product ); ?>
<?php smpl_product_more_details(); ?>
</div>
</li>
<?php endwhile; ?>
</ul>
I'm new for this,
Thanks in advance
To get featured product by a specific category you just need to use wc_get_products with featured set to true and the category you specify. See below code.
<?php
// Get featured products by category. on this case its "shirts" which is the slug of the category.
$query_args = array(
'featured' => true,
'category' => array( 'shirts' ),
);
$products = wc_get_products( $query_args );
You can see the full tutorial here https://jameshwartlopez.com/plugin/get-featured-products-of-a-category/
$prod_categories = array(10, 27);
$product_args = array(
'numberposts' => $limit,
'post_status' => array('publish', 'pending', 'private', 'draft'),
'post_type' => array('product', 'product_variation'),
'orderby' => 'ID',
'suppress_filters' => false,
'order' => 'ASC',
'offset' => 0
);
if (!empty($prod_categories)) {
$product_args['tax_query'] = array(
array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => $prod_categories,
'operator' => 'IN',
));
}
$products = get_posts($product_args);
Replace the $args with the following code
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'terms' => array_map( 'sanitize_title', explode( ',', 'ENTER_CATEGORY' ) ),
'field' => 'slug',
'operator' => $atts['operator']
)
)
);
You just need to replace the ENTER_CATEGORY word with the category name you want to display.
Let me know if this fulfills your requirements.
Please use below Query
<ul class="products">
<?php
$args = array( 'post_type' => 'product', 'posts_per_page' => 1, 'product_cat' => 'shoes', 'orderby' => 'rand' );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>
<h2>Shoes</h2>
<li class="product">
<a href="<?php echo get_permalink( $loop->post->ID ) ?>" title="<?php echo esc_attr($loop->post->post_title ? $loop->post->post_title : $loop->post->ID); ?>">
<?php woocommerce_show_product_sale_flash( $post, $product ); ?>
<?php if (has_post_thumbnail( $loop->post->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" />'; ?>
<h3><?php the_title(); ?></h3>
<span class="price"><?php echo $product->get_price_html(); ?></span>
</a>
<?php woocommerce_template_loop_add_to_cart( $loop->post, $product ); ?>
</li>
<?php endwhile; ?>
<?php wp_reset_query(); ?>

Categories