Thank you jarvis and John Huebner,
in my case I prefer the jarvis’s solution because directly in template file I can apply the both query quickly!
https://drive.google.com/file/d/1nQlxM8UZVU3uGwjZlRFlqnwmZoAUO4oC/view?usp=sharing
@hube2
Solved with your help, today I did the function I needed! Thanks!
I write a piece, maybe someone can help.
The function takes the title of a post-type related to products (woocommerce) and inserts this title as a woocommerce attribute when save_post action !
add_action('acf/save_post', 'update_post_meta', 20);
function update_post_meta( $post_id ) {
$post_type = get_post_type($post_id);
global $product;
if ( 'product' == $post_type ) {
//TAKE RELATED INFO IN POST TYPE
$posts = get_field('serie_di_appartenenza');
if( $posts ):
foreach( $posts as $p ):
$scrittore=get_field("scrittore_appartenenza", $p->ID);
if( $posts ):
foreach( $scrittore as $s ):
$scrittore_ereditato= get_the_title( $s->ID );
//ADD INFO IN GLOBAL ATTRIBUTE WOOCOMMERCE
$term_taxonomy_ids = wp_set_object_terms( $post_id, $scrittore_ereditato, 'pa_scrittore-woo', true );
endforeach;
endif;
endforeach;
endif;
//ADD INFO IN CURRENT POST
$data = Array(
'pa_scrittore-woo'=>Array(
'name'=>'pa_scrittore-woo',
'value'=>$scrittore_ereditato,
'is_visible' => '1',
'is_variation' => '1',
'is_taxonomy' => '1'
),
);
//UPDATE META
update_post_meta( $post_id, '_product_attributes',$data);
}
}
Auto Solved 🙂
This is the code that can serve others if they have the same problem
<?php
//take relationship field of this doctor
$location= get_field('location');
if( $location):
foreach( $location as $l ):
//array of post type doctor and take the post with key location and compare with the ID of location of this doctor $l->ID
$doctors = get_posts(array(
'post_type' => 'doctor',
'posts_per_page' => '-1',
'post__not_in' => [get_queried_object_id()],
'meta_query' => array(
array(
'key' => 'location', // name of custom field
'value' => $l->ID, // the ID of this doctor
'compare' => 'LIKE'
)
)
));
?>
<?php if( $doctors ): ?>
<?php foreach( $doctors as $doctor ): ?>
//
<div class="col-md-2">
<a href="<?php echo get_permalink( $doctor->ID ); ?>">
<img src="<?php echo get_the_post_thumbnail_url( $doctor->ID ); ?>">
<h2><?php echo get_the_title( $doctor->ID ); ?></h2>
</a>
</div>
<?php endforeach; ?> //second foreach
<?php endif; ?> //second if
<?php endforeach; ?> //first foreach
<?php endif; ?> //first if
Yes, it works !
Thanks!
Got it. Thank you!
But it would be logical to create a group of fields that are used for different templates and if I have a template that I do not want the fields -1 to be able to do it quickly, without me having to redo the same fields just to exclude one.
I resolved the problem adding a new custom post type called ANNO and 1 relationship field called anno_issue from year to issue_number.
The code is this for query :
` <?php
// args
$args = array(
‘numberposts’ => -1,
‘post_type’ => ‘anno’,
‘posts_per_page’ => 100,
);
// query
$the_query = new WP_Query( $args );
?>
<?php while( $the_query->have_posts() ) : $the_query->the_post();
mh_before_page_content();
mh_magazine_page_title();?>
<?php
/*
* Query posts for a relationship value.
* This method uses the meta_query LIKE to match the string “123” to the database value a:1:{i:0;s:3:”123″;} (serialized array)
*/
$posts = get_posts(array(
‘post_type’ => ‘issue_number’, //nome post type da dove recupero le info
‘posts_per_page’ => -1,
‘meta_query’ => array(
array(
‘key’ => ‘anno_issue’, // nome custom field all’interno di post che mi da l’info
‘value’ => ‘”‘ . get_the_ID() . ‘”‘, // matches exaclty “123”, not just 123. This prevents a match for “1234”
‘compare’ => ‘LIKE’
)
)
));
?>
<?php /*ciclo stampa articoli */ ?>
<?php if( $posts ): ?>
<?php foreach( $posts as $post ): ?>
<?php /*layout 1 del tema per articoli */ ?>
<article >
—HTML CODE DISPLAYNG YOUR LOOP—
</article>
<?php /*fine layout 1 */ ?>
<?php endforeach; ?>
<?php endif; ?>
<?php /*fine ciclo stampa articoli */ ?>
<?php endwhile; // end of the loop. ?>
Thanks James…I solved the problem !
You must obviously change according to your needs!
Thanks
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.