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>';
}
Related
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!
I am trying to display a list based on the page. The list is built based on data in an ACF custom field, which is in a serialized array.
The list is for employees in a department. I am setting the parent page in Wordpress but want the employees to be listed on all child page. Employees may also need to be listed in multiple departments, so the ACF field is stored as a serialized array.
I have been able to get this to work if the current page is in the array, but am having problems when the data is for a parent, grandparent, etc. page. How can I iterate through this to find the first parent page that matches the criteria?
Relevant code (additional content is displayed, some included in case it is helpful in your answer):
$postid = get_the_ID();
$staffList = get_posts(array(
'post_type' => 'staff',
'meta_key' => 'position_order',
'orderby' => 'meta_value_num',
'order' => 'ASC',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'ministries', // name of custom field
'value' => $postid,
'compare' => 'LIKE',
),
array(
'key' => 'administration', // name of custom field
'value' => $postid,
'compare' => 'LIKE',
)
)
));
if(( $staffList ) != '') {
foreach ( $staffList as $staff ) : setup_postdata( $people );
$pid = $staff->ID;
$featured_img_url = get_the_post_thumbnail_url($pid, 'full');
$highres = get_field('high_res', $pid);
$normal = get_field('normal', $pid);
$biofile = get_field('bio_file', $pid);
$scontent = $staff->post_content;
$contact = apply_filters('the_content',$scontent);
?>
<div class="profile">
<div class="text-center">
<img src="<?php echo $featured_img_url; ?>" width="130"
alt="<?php echo $staff->post_title; ?>">
</div>
<div class="profile__name"><?php echo $staff->post_title; ?></div>
<?php echo $contact; ?>
<?php if ( $biofile != "" ) { ?>
<span class="icon-file"></span> <?php echo $people->post_title; ?> Bio
<?php } ?>
</div>
<?php
endforeach;
wp_reset_postdata();
?>
<?php } endif; ?>
I'm thinking the query needs to be placed in a function, but having a problem getting it to run correctly and then iterate through parent posts until it finds a results. The concept is:
if no employees for current $pageid, then get $parent_pageid and run query. if no employees found, find grandparent_pageid and run query, etc., until either employees are found or there are no more parent pages.
I've found this: Recursive function to get the parent caegory description that seems like a good way to iterate, just having a problem modifying for my needs.
Thanks for your help!
This function will loop through parent posts until it returns results:
function get_staff_posts($post) {
$staffList = new WP_Query( array(
'post_type' => 'staff',
'meta_key' => 'position_order',
'orderby' => 'meta_value_num',
'order' => 'ASC',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'ministries', // name of custom field
'value' => $post->ID,
'compare' => 'LIKE',
),
array(
'key' => 'administration', // name of custom field
'value' => $post->ID,
'compare' => 'LIKE',
),
),
));
$parent = $post->post_parent;
if( !$staffList->have_posts() && $parent != 0 )
get_staff_posts($parent);
return $staffList;
}
The function will return the query, so you could use it in your theme like this:
$staffList = get_staff_posts($post);
if( $staffList->have_posts() ):
while( $staffList->have_posts() ): $staffList->the_post();
// output
endwhile; wp_reset_postdata();
endif;
I have a custom post_type called special_listing which contains a custom field called listing_index. A special_listing with a listing_index of 20, should appear before one with an index of 15, then 10, and so on. However this doesn't appear to be working and listings appear in whatever order they choose.
I'm not a PHP or Wordpress guy, I mainly deal with ASP.Net and C# so this is a bit confusing for me. Is there some simple mistake I'm making?
Function:
// Get the first $count listings with the highest indices for a given $region_slug:
function get_listings($region_slug, $count) {
$args = array(
'post_type' => 'special_listing',
'meta_key' => 'listing_region_slug',
'meta_value' => $region_slug
);
$posts = get_posts($args);
sort_array_on_field($posts, 'listing_index', 'DESC');
truncate_array($posts, $count);
return $posts;
}
View:
<?php
$listings = get_listings(get_microsite_slug() . '-microsite-home-featured', 4);
$i = 0;
if (sizeof($listings) > 0) : while ($i < sizeof($listings)) : $listing = $listings[$i]; // Loop and set current listing
?>
<section>
<a href="<?php echo $listing->destination; ?>">
<h3><?php echo $listing->post_title; ?></h3>
<p><?php echo $listing->post_content; ?></p>
</a>
</section>
<?php $i++; ?>
<?php endwhile; ?>
<?php endif; ?>
My attempt:
I don't know how to return the listing_index value
function get_sorted_listings($region_slug, $count){
$args = array(
'post_type' => 'kodakalaris_listing',
'meta_query' => array(
'relation' => 'AND',
array (
'key' => 'listing_region_slug',
'value' => $region_slug
),
array (
'key' => 'listing_index',
'value' => ''
),
orderby: 'listing_index',
order: 'DESC'
)
);
$posts = get_posts($args);
truncate_array($posts, $count);
return $posts;
}
Update
Trying a new way and it now pulls the content in by the published date. Still not ordering by listing_index, but at least it's not completely random either. I began looking to meta_query. Won't this only return a result with a single value? I've also tried out this other SO answer, but I find it's implementation confusing.
<?php
$args = array(
'post_type' => 'kodakalaris_listing',
'meta_key' => 'listing_region_slug',
'meta_value' => get_microsite_slug() . '-microsite-home-featured',
'posts_per_page' => 4,
'order' => 'DESC',
'orderby' => 'listing_index'
);
$listings = new WP_Query($args);
if ($listings->have_posts()) : while ($listings->have_posts()) : $listings->the_post();
?>
...
...
<?php
endwhile;
endif;
?>
You don't need to sort after you query. WP_Query has orderby parameters:
function get_listings($region_slug, $count) {
$args = array(
'post_type' => 'special_listing',
'meta_key' => 'listing_region_slug',
'meta_value' => $region_slug,
'orderby' => 'meta_value_num'
);
$posts = new WP_Query($args);
return $posts;
}
You'll then want to use The Loop rather than a foreach loop, in your view.
So I was able to figure this out after reading this article, but basically assigning a name to each meta_query array allows you to call which one should take priority when using orderby.
$args = array(
'post_type' => 'special_listing',
'posts_per_page' => 4,
'orderby' => 'index_clause',
'meta_query' => array (
'site_clause' => array (
'key' => 'listing_region_slug',
'value' => get_microsite_slug() . '-microsite-home-featured'
),
'index_clause' => array (
'key' => 'listing_index',
)
)
);
Change the function to :
<?php
// Get the first $count listings with the highest indices for a given $region_slug:
function get_listings($region_slug, $count) {
$args = array(
'post_type' => 'special_listing',
'orderby' => 'listing_index',
'order' => 'DESC',
'meta_key' => 'listing_region_slug',
'meta_value' => $region_slug
);
$posts = get_posts($args);
return $posts;
}
I'm trying to query posts based on a custom field, and then display them in a loop. I've checked and double checked my code against the codex and other sources, but the query still does not appear to be working. What am I doing wrong?
Stripped down to the essentials, my code looks like this:
<?php
$args = array(
'meta_key' => 'my_custom_field'
);
$my_query = new WP_Query( $args );
?>
<?php if ( $my_query->have_posts() ) { ?>
<p>Success, we have posts!!!</p>
<?php } else { ?>
<p>Uh Oh, No posts!!!</p>
<?php } ?>
The conditional statement is dropping through and returning "Uh Oh, no posts".
I've checked the postmeta table, and there are definitely posts that contain the meta_key _my_custom_field. I have tried the query both with and without leading underscore.
What am I doing wrong?
I use this for search a date between two custom dates field in my custom post type "porfolio", i think that you are in a similar situation:
$args = array(
'post_type' => 'portfolio',
'posts_per_page' => '10',
'meta_query' => array(
array('key' => 'portfolio_start_date', 'value' => data_to_db2($ricerca_data), 'compare' => '<=', 'type' => 'NUMERIC'),
array('key' => 'portfolio_end_date', 'value' => data_to_db2($ricerca_data), 'compare' => '>=', 'type' => 'NUMERIC')
)
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {
$post_count = wp_count_posts();
while ( $the_query->have_posts() ) {
// DO WHAT YOU WANT
}
}
My advice is to use meta_query in $args array
Is there any easy way to list all taxonomies for custom post type with permalinks?
taxonomy=title&post_type=company
The following does not seams to work, it is listing the categories for posts only:
$args = array (
'type' => 'company', //your custom post type
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => 0 //shows empty categories
);
$categories = get_categories( $args );
foreach ($categories as $category) {
echo $category->name;
$post_by_cat = get_posts(array('cat' => $category->term_id));
echo '<ul>';
foreach( $post_by_cat as $post ) {
setup_postdata($post);
echo '<li>'.the_title().'</li>';
}
echo '</ul>';
}
try changing
'type' => 'company', //your custom post type
to
'post_type' => 'company', //your custom post type
Change
'type' => 'company', // your custom post type
to
'taxonomy' => 'yourtaxonomyname', // your custom taxonomy
see also http://codex.wordpress.org/Function_Reference/get_categories
This should work:
$index_query = new WP_Query(array('post_type' => 'company', 'posts_per_page' => '-1', 'order' => 'DESC'));
while ($index_query->have_posts()) : $index_query->the_post();
$taxonomy_ar = get_the_terms($post->ID, 'tax-name');
$output = '<span class="btn">';
foreach($taxonomy_ar as $taxonomy_term) {
$output .= ''.$taxonomy_term->name.' <span class="label">'.$taxonomy_term->count.'</span> ';
}
$output .= '</span>';
echo $output;
endwhile;