Wordpress query odererd by acf-field with title - wordpress

I need help with a Wordpress query to get posts.
I have an ACF-numeric-Field called "year".
I need a query to get posts who have entered a year, and the year is echoed as title.
Someting like
1925
Post A
Post B
Post D
1926
Post C
Post E
This is waht I have so far to get posts with a "year" set and ordered by.
<?php
$args = array(
'numberposts' => -1,
'post_type' => 'artikel',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'year',
'compare' => 'EXISTS'
),
array(
'key' => 'year',
'value' => 1900,
'type' => 'NUMERIC',
'compare' => '>'
)
)
);
$the_query = new WP_Query( $args );
?>
<?php if( $the_query->have_posts() ): ?>
Can someone help me how to put the year as the title?

If $the_query has values than you can do this
// assuming that all posts are sorted by year. 1925, 1926, 1930 etc...
$the_query = new WP_Query($args);
// will contain all posts sorted by year
$posts_by_year = [];
// start looping through the posts
if ($the_query->have_posts()) {
while ($the_query->have_posts()) {
$the_query->the_post();
// get year ACF field
$year = get_field('year');
// check if $posts_by_year already has year, if not create and add the post, if exists just add the post
if (isset($posts_by_year[$year])) {
$posts_by_year[$year][] = get_post();
} else {
$posts_by_year[$year] = [
get_post()
];
}
}
}
wp_reset_postdata();
// check if we have any posts
if (!empty($posts_by_year)) {
foreach ($posts_by_year as $year => $posts) {
echo '<div class="year-block">';
echo '<strong class="year-title">' . $year . '</strong>';
echo '<ul class="posts-by-year">';
foreach ($posts as $post) {
echo '<li>';
echo '<strong class="post-title">' . $post->post_title . '</strong>';
echo '</li>';
}
echo '</ul>';
echo '</div>';
}
}
Of course change the html formating how ever you want

You can try this one:
<?php
$args = array(
'posts_per_page' => -1,
'post_type' => 'artikel',
'meta_key' => 'year',
'orderby' => 'meta_value_num',
'order' => 'DESC',
);
$the_query = new WP_Query( $args );
?>
<?php if( $the_query->have_posts() ): ?>
Hope it will be work!

Related

Group WordPress posts by category

after several unsuccessful searches, I ask my question here.
Indeed, I'm trying to display a list of posts grouped by categories:
CAT A
post1
post2
post3
CAT B
post4
post5
post6
post7
...
Here is the code I tried.
I can display the categories, but not the posts
<?php
$terms = get_terms( 'secteur', array(
'orderby' => 'count',
'hide_empty' => 0
) );
foreach( $terms as $term ) {
$args = array(
'post_type' => 'client',
'posts_per_page' => '-1',
'secteur' => $term->slug
);
$query = new WP_Query( $args );
echo'<h3>' . $term->name . '</h3>';
// Start the Loop
while ( $query->have_posts() ) : $query->the_post();
$secteur_dactivite = get_field( 'secteur_dactivite' );
echo '<div class="cat-'.esc_html( $secteur_dactivite->slug ). '"><img src="'.get_field( 'logo' ).'"></div>';
endwhile;
wp_reset_postdata();
}
?>
You need to use tax_query as an WP query attribute, instead of secteur.
Try replacing that with:
$args = array(
'post_type' => 'client',
'posts_per_page' => '-1',
'tax_query' => array(
array(
'taxonomy' => 'secteur',
'field' => 'slug',
'terms' => $term -> slug,
),
),
);
thank you very much for your response.
Unfortunately, this does not change the display. The titles are displayed but not the articles.
<h3>CAT1</h3
<h3>CAT2</h3>
<h3>CAT3</h3>
If it helps, I can display all the items with the following code :
<?php
$posts = get_posts(array(
'numberposts' => -1,
'post_type' => 'client',
'post_status' => 'publish'
));
if($posts)
{
echo '<div class="row all-item">';
foreach($posts as $post)
{
echo '<div"><img src="'.get_field( 'logo' ).'"></div>';
}
echo '</div>';
}
?>
Thank you very much for your answers.
But nothing has worked for me, and I can’t come up with any solutions.
I think the problem lies in the configuration of my taxonomy.
This is my custom post type configuration (client):
https://imgur.com/uDom3PH
my taxonomy configuration (secteur) :
https://imgur.com/WRwsSbR
my custom field (secteur_dactivite) :
https://imgur.com/NKQ4GPn
Thanks again for your help

Meta Query value >= (current date) not working with ACF

I am working on a loop with ACF fields inside of it. What I am trying to do is to loop my custom post type, but when the date is already passed the loop has to NOT show the event that already happened.
My code is made as follows:
<?php
$today = date('Ymd');
$posts = get_posts(array(
'post_type'=>'rasweekenden',
'post_status'=>'publish',
'posts_per_page'=>5,
'meta_query' => array(
array(
'key' => 'eventdatum',
'value' => $today,
'type' => 'DATE', // specify it for numeric values
'compare' => '>='
)
),
'meta_key' => 'eventdatum',
'orderby' => 'meta_value_num',
'order' => 'ASC',
));
if($posts)
{
echo '<ul>';
foreach($posts as $post)
$date_event = date("Ymd", strtotime(get_field('eventdatum', get_the_ID())));
$date_today = date('Ymd');
if ($date_event >= $date_today){
{
the_field('rasweekend_heading', $post->ID);
}
}
echo '</ul>';
}
?>
eventdatum is a ACF field assigned to my Custom Post type rasweekenden.
This is made with the Datepicker from ACF with a value output of Ymd.
I think it has something to do with my meta_query but I'm not sure.
Since your query is getting posts. I would remove the if statement from the loop, as you've already done this anyway on the query.
if($posts) {
echo '<ul>';
foreach($posts as $post) {
echo '<li>';
the_field('rasweekend_heading', $post->ID);
echo '</li>;
}
echo '</ul>';
}

How to get post using taxonomy terms in wordpress

I have a post type called "rationale" and my taxonomy name is "company_list". In taxonomy there are list of company. Each company have many rationale.
I want to get latest rationale for each company. How can i do this ?
I try below code but it show all company list but data is duplicate
<?php
//$taxonomy = 'our_work_thematic';
$myquery = array (
'post_type' => 'rationale',
'paged'=>$paged,
'posts_per_page' => -1,
);
$loop = new WP_Query($myquery);
if( $loop->have_posts() ):
while( $loop->have_posts() ):
$loop->the_post(); global $post; ?>
<?php $terms = get_the_terms( $post->ID, 'company_list' );
foreach($terms as $term) {
$termlinks = get_term_link($term);
echo '<p class="post-content--cat">';
echo '' . $term->name . '';
echo '</p>';
}?>
<?php endwhile; ?>
<?php endif; ?>
You need to get latest term and use tax_query
$args_query = array(
'post_type' => array('rationale'),
'paged' => $paged,
'posts_per_page' => -1,
);
$terms = get_terms(array(
'taxonomy' => 'post_tag',
'hide_empty' => false,
));
if (!empty($terms) && is_array($terms)) {
$args_query['tax_query'] => array(
array(
'taxonomy' => 'company_list',
'field' => 'term_id',
'terms' => array($terms['0']->term_id), // single or array with id's
),
),
}
$query = new WP_Query($args_query);
if ($query->have_posts()) {
while ($query->have_posts()) {
$query->the_post();
$terms = get_the_terms($post->ID, 'company_list');
foreach ($terms as $term) {
$termlinks = get_term_link($term);
echo '<p class="post-content--cat">';
echo '' . $term->name . '';
echo '</p>';
}
}
} else {
// no post found
}
wp_reset_postdata();
try this
$terms = get_terms( array(
'taxonomy' => 'your taxonomy name',
'hide_empty' => false,
'orderby' => 'term_id',
'order' => 'asc',
) );
foreach ($terms as $terms_row) {
$terms_row->slug;
echo "<pre>";
print_r($terms_row);
echo "</pre>";
}
Thanks

Get all posts beginning with letter A

How can i get all poasts beginning with the letter A (in the post_title)?
My idea was to use regex but this code dont work.
$my_custom_query_args = array(
'cat' => '1',
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 25,
'offset' => 0,
'value' => '^'.$letter.'',
'compare' => 'REGEXP'
);
Using post_where , this action code be use in custom template.
add_action( 'posts_where', 'startswithaction' );
function startswithaction( $sql ){
global $wpdb;
$startswith = get_query_var( 'A' );
if( $startswith ){
$sql .= $wpdb->prepare( " AND $wpdb->posts.post_title LIKE %s ", $startswith.'%' );
}
return $sql;
}
OR you can get all records starts with letter A by SQL query and pass the post ids in WP_QUERY.
//get all post IDs for posts start with letter A, in title order,
//display posts
global $wpdb;
$first_char = 'A';
$postids = $wpdb->get_col($wpdb->prepare("
SELECT ID
FROM $wpdb->posts
WHERE SUBSTR($wpdb->posts.post_title,1,1) = %s
ORDER BY $wpdb->posts.post_title",$first_char));
if ($postids) {
$args=array(
'post__in' => $postids,
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => -1,
'caller_get_posts'=> 1
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
echo 'List of Posts Titles beginning with the letter '. $first_char;
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<p><?php the_title(); ?></p>
<?php
endwhile;
}
wp_reset_query();
}

how to get related posts list in home page

how can i get list of latest posts with their relative posts by tags?
example:
Latest post post title 1
related post
related post
Latest post post title 2
related post
related post
use this in Index.php
<?php
$args = array(
'numberposts' => 100,
'offset' => 0,
'category' => 0,
'orderby' => 'post_date',
'order' => 'DESC',
'include' => ,
'exclude' => ,
'meta_key' => ,
'meta_value' =>,
'post_type' => 'post',
'post_status' => 'draft, publish, future, pending, private',
'suppress_filters' => true );
$recent_posts = wp_get_recent_posts( $args, ARRAY_A );
?>
<ul>
<?php
foreach( $recent_posts as $recent )
{
echo '<li>'.$recent["post_title"];
$tags = wp_get_post_tags($recent["ID"]);
if ($tags)
{
$first_tag = $tags[0]->term_id;
$args=array(
'tag__in' => array($first_tag),
'post__not_in' => array($recent["ID"]),
'posts_per_page'=>100,
'caller_get_posts'=>1
);
$my_query = new WP_Query($args);
if( $my_query->have_posts() )
{
echo '<ul>';
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<li><?php the_title(); ?></li>
<?php
endwhile;
echo '</ul>';
}
wp_reset_query();
}
echo '</li>';
}
?>
</ul>
you can do something like this
your main loop contain recent post so during each loop use following function to get it's tag
$tag_ids = wp_get_post_tags( $post->ID, array( 'fields' => 'ids' ) );
then you can use another loop of using those tags
$query = new WP_Query( 'tag_id='.$tag_ids );
now $query has content you want.

Resources