Home › Forums › General Issues › Query posts based on a select list meta
Hello !
I have a poor level in PHP.
I have a problem to display regular posts outside the main loop in my sidebar.
I have a select list meta “themes” in my posts.
I don’t succeed in displaying in my sidebar the posts with the same “themes” meta as the current post (single.php).
Thank you a lot for those who could help me.
Best regards !
EDIT :
I use the DIVI builder and try to realize that query as shortcode in a TXT module.
I can’t understand why the results don’t display properly in this right place.
Thank you again
I succeeded to have a beginning of resukt with :
function sidebar_related() {
$type = get_field( "themes", get_the_ID() );
$args = array(
'post_type' => 'post',
'posts_per_page' => -1,
'orderby' => 'date',
'order' => 'DESC',
'meta_query' => array(
array(
'key' => 'themes',
'value' => $type
)
),
);
$my_posts = new WP_Query($args); ?>
<?php if ( $my_posts->have_posts() ) : while ( $my_posts->have_posts() ) : $my_posts->the_post(); ?>
<a href="<?php the_permalink(); ?>"><?php the_title() ;?></a>
<?php endwhile; else : ?>
<p><?php _e( 'No Posts To Display.' ); ?></p>
<?php endif;
}
add_shortcode('related-terms', 'sidebar_related');
Is it possible not to display in the list the title of the current post ?
Thank you !
Ok, i resolve the problem not to display in the list the title of the current post :
<?php
function sidebar_related() {
$type = get_field( "themes", get_the_ID() );
$args = array(
'post_type' => 'post',
'posts_per_page' => -1,
'orderby' => 'date',
'post__not_in' => array(get_the_ID()),
'order' => 'DESC',
'meta_query' => array(
array(
'key' => 'themes',
'value' => $type
)
),
);
$my_posts = new WP_Query($args); ?>
<?php if ( $my_posts->have_posts() ) : while ( $my_posts->have_posts() ) : $my_posts->the_post(); ?>
<a href="<?php the_permalink(); ?>"><?php the_title() ;?></a>
<?php endwhile; else : ?>
<p><?php _e( 'No Posts To Display.' ); ?></p>
<?php endif;
}
add_shortcode('related-terms', 'sidebar_related');
?>
To complete, anyone has an idea to display my shortcode [related-terms] in the right place ?
I can’t understand why is out of its module…
I succeeded in realize what i wanted. But i don’t know if it’s safe way :
<?php function sidebar_related() {
<strong>ob_start();</strong>
$type = get_field( "themes", get_the_ID() );
$args = array(
'post_type' => 'post',
'posts_per_page' => -1,
'orderby' => 'date',
'post__not_in' => array(get_the_ID()),
'order' => 'DESC',
'meta_query' => array(
array(
'key' => 'themes',
'value' => $type
)
),
);
$my_posts = new WP_Query($args); ?>
<ul>
<?php if ( $my_posts->have_posts() ) : while ( $my_posts->have_posts() ) : $my_posts->the_post(); ?>
<li><a href="<?php the_permalink(); ?>"><?php the_title() ;?></a></li>
<?php endwhile; ?>
</ul>
<?php endif;
wp_reset_postdata();
<strong>$output_string = ob_get_contents();
ob_end_clean();
return $output_string;</strong>
}
add_shortcode('related-terms', 'sidebar_related');
Any idea ?
You must be logged in to reply to this topic.
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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.