Here is my code:
$args = array(
'posts_per_page' => - 1,
'post_type' => array('post-type-name'),
'tax_query' => $taxquery,
'orderby' => 'meta_value_num',
'meta_key' => 'date_of_issue',
'order' => 'DESC',
);
global $post;
$post_list = get_posts($args);
if ($post_list) {
foreach ($post_list as $post_item) { ?>
<?php
$fields = CFS()->get('anouncements', $post_item->ID);
if ( ! empty ( $fields ) ){
foreach ($fields as $field_eti) { ?>
<div class="item"><?php echo $field_eti['date_of_issue']; ?></div>
<?php }
}
?>
<?php
}
}
I'm using Custom Field suite plugin. I want to order those fields by this date which is one of the custom fields of that loop ($field_eti['date_of_issue'];). How you see in my $args I added that meta_key and ordered it by custom field value which is date. But ordering isn't right.
Try this: I have updated my answer.
foreach ($post_list as $post_item) {
setup_postdata($post_item); // Set postdata
?>
<?php
$fields = CFS()->get('anouncements');
$temp=array();
if ( ! empty ( $fields ) ){
foreach ($fields as $field_eti) {
$temp[] = strtotime($field_eti['date_of_issue']);
?>
<div class="item"><?php echo $field_eti['date_of_issue']; ?></div>
<?php }
$new_field_eti[] = arsort($temp);
print_r($new_field_eti);
}
?>
<?php
}
Related
Looking to output a Advanced Custom Fields repeater in random order. I have a repeater field "profiles" containing 5 "profile" post objects.
My code today is:
// Randomize and shuffle the rows
$rows = get_sub_field('profiles');
shuffle($rows);
$rand_repeater_fields = array_rand( $rows , 4 ); ?>
<?php if( have_rows('profiles') ):
$stage_index = 0; ?>
<?php while ( have_rows('profiles') ) : the_row();
// print rows only if in array
if (in_array(get_row_index() - 1, $rand_repeater_fields)) { ?>
<?php $post_object = get_sub_field('profile'); //row w. post object start
if( $post_object ):
$post = $post_object;
setup_postdata( $post ); ?>
<?php the_permalink();?>
<?php wp_reset_postdata(); ?>
<?php endif; ?> //row w. post object end
<?php // increment index
$stage_index++;
} ?>
<?php endwhile; ?>
This code successfully output 4 of 5 rows in random order, which means that it almost works as I wish.
How do I update this snippet to output ALL fields of the repeater fields, in random order, even if they are 3 or 10 in total?
Thankful for any suggestions!
<?php
$args = array(
'post_type' => 'page',
'post_status' => 'publish',
'posts_per_page' => -1,
'orderby' => 'rand',
'meta_query' => array(
array(
'key' => 'profiles',
'compare' => 'EXISTS'
)
)
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
$profiles = get_field('profiles');
if( $profiles ):
foreach( $profiles as $profile ):
echo '<div class="profile">';
echo '<h2>' . $profile['profile'] . '</h2>';
echo '</div>';
endforeach;
endif;
}
}
wp_reset_postdata();
?>
How can I get the parent and child term of a custom post type. I have code below that partly works, the problem is it does not generate child and parent term in same div but different div as loop. Due to lack of knowledge on PHP, I am not really sure how to fix it.
What I want if I have parent and child as;
- USA
- New York
The out come should be;
USA, New York
but with below code it generates as
,USA
,New York
Code;
<div class="address">
<?php
$terms = get_terms(array(
'taxonomy' => 'people-country',
'hide_empty' => false,
));
?>
<?php foreach ($terms as $term) : ?>
<?php
$re = explode('-', $term->name);
$args = array (
'post_type' => 'people', //
'posts_per_page' => 2,
'order' => 'ASC',
'tax_query' => array(
array(
'taxonomy' => 'people-country',
'field' => 'id',
'terms' => $term->term_id,
),
),
);
$query = new WP_Query( $args );
if( $query->have_posts() ){
while( $query->have_posts() ){
$query->the_post();
$title = get_the_title();
// $info = get_post_meta(get_the_ID(), '_post_info', true);
// $link = get_term_meta($term->term_id, 'link', true);
?>
<div class="text">
<span class="number"><?php echo ",".$re[0] ?>
</span>
</div>
<?php
}
}
endforeach; ?>
</div>
I just solved it by changing it with below code;
<?php
$cats = get_the_terms(get_the_ID(), 'people-country');
$names = array();
if ($cats) foreach ($cats as $cat) {
if ($cat->parent) {
array_push($names, $cat->name);
}
}
if ($cats) foreach ($cats as $cat) {
if (!$cat->parent) {
array_push($names, $cat->name);
}
}
?>
<?php echo implode(', ', $names) ?>
I'm using this code...
function include_post_types() {
$post_types = get_post_types( array (
'show_ui' => true
),
'objects' );
$return = '';
foreach ( $post_types as $post_type ) {
$my_sitemap_options = get_option( 'my_sitemap_settings' );
$post_type_name = $post_type->name;
if ($my_sitemap_options['post_types'] && in_array($post_type_name, $my_sitemap_options['post_types'])) {
$the_excluded = $post_type_name;
$return .= "'" . $the_excluded . "', ";
}
}
return $return;
}
... to return a list of custom post types that I have selected from an options page. This works fine, and if I do this...
echo included_post_types();
...I see this...
'clothes', 'shoes', 'jackets',
...which is what I excpected.
But the problem is when I try to use included_post_types() in a loop to only show posts with those post types, like this:
$sitemap_post_args = array(
'post_type' => array(included_post_types()),
'posts_per_page' => -1,
'orderby' =>'post_type',
'order' =>'asc'
);
$loop = new WP_Query($sitemap_post_args);
global $post_type;
global $post;
echo '<ul>';
$last_post_type = '';
while($loop->have_posts()): $loop->the_post();
$current_post_type = $post->post_type;
$current_post_type_object = get_post_type_object( $current_post_type );
if($current_post_type != $last_post_type) echo "<br /><li><strong>" . $current_post_type_object->labels->name . "</strong></li>";?>
<li><?php echo get_the_title(); ?></li>
<?php echo "\n";
$last_post_type = $current_post_type;
endwhile;
wp_reset_query();
echo '</ul>';
It simply doesn't display anything on my page, but it doesn't throw an error either.
I'm almost certain the problem is this line:
'post_type' => array(included_post_types()),
I even tried it like this...
'post_type' => included_post_types(),
...but it did not work.
If I try this...
'post_type' => array('clothes', 'shoes', 'jackets', ),
...it works, but I need to be able to use the function name.
Any suggestions would be helpful.
please replace below code with yours. It should be help.
function include_post_types() {
$post_types = get_post_types( array (
'show_ui' => true
),
'objects' );
$return = array();
foreach ( $post_types as $post_type ) {
$my_sitemap_options = get_option( 'my_sitemap_settings' );
$post_type_name = $post_type->name;
if ($my_sitemap_options['post_types'] && in_array($post_type_name, $my_sitemap_options['post_types'])) {
$the_excluded = $post_type_name;
$return[] = $the_excluded;
}
}
return $return;
}
and also replace below code that show posts
$post_type_list = include_post_types();
$sitemap_post_args = array(
'post_type' => $post_type_list,
'posts_per_page' => -1,
'orderby' =>'post_type',
'order' =>'asc'
);
I'm trying to combine 2 custom post types: 1) CPT = event 2) CPT = location, in the same foreach loop.
e.g.
<?php
$events = get_posts( array( post_type => event));
$locations = get_posts( array( post_type => location));
foreach($events as $event ) {
foreach($locations as $location ) {
echo $event->post_title;
echo $location->post_title;
}
}
?>
This however will only duplicate each post title. I also tried the following but it didn't work.
<?php
foreach($events as $index => $event ) {
$event->post_title;
$event->post_title[$index];
}
I'm not sure what you want as an output. This should give you a list of all titles:
foreach($events as $event ) {
$titles[]=$event->post_title;
}
foreach($locations as $location ) {
$titles[]=$location->post_title;
}
echo '<ul>';
foreach($titles as $title ) {
echo '<li>'.$title.'</li>';
}
echo '</ul>';
First thing you should do is switch to using WP_Query instead of get_posts and you can do the following quick dirty example:
// The Query args
$args = array(
'post_type' => array( 'event', 'location' )
);
// The Query
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) {
echo '<ul>';
while( $the_query->have_posts() ){
$post = $the_query->the_post();
echo '<li>' . get_the_title() . '<li>';
}
echo '</ul>';
}
I guess I found what you need:
$args = array(
'post_type' => 'event'
);
/* Get events */
$events = get_posts( $args );
foreach($events as $event ) {
echo '<article><h2>';
$event->post_title;
echo '<span>';
/*get location of event*/
$args2 = array(
'post_type' => 'location',
'meta_key' => '_location_ID',
'meta_value' => get_post_meta($event->ID,'_location_ID')
);
$locations = get_posts( $args2 );
foreach($locations as $location ) {
echo $location->post_title;
}
echo '</span></h2></article>';
}
I know how to get all of the Wordpress terms, but I need a "filtered" down version of the results. Is it possible to get all of the terms that are in the result of a Wordpress query? I have this query here:
<?php
$args=array(
'post_type' => 'gw_activity',
'post_status' => 'publish',
'orderby' => 'date',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'activity_category',
'value' => 'mindful_life',
'compare' => '='
)
),
'posts_per_page' => 10
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
$all_terms = array();
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<?php $terms = wp_get_post_terms( $my_query->post->ID, array( 'gw_activity_tag' ) ); ?>
<?php
foreach ( $terms as $term ) {
$all_terms[] = $term->name;
}
?>
<?php endwhile; }
wp_reset_query();
?>
<!-- End Custom Query -->
<?php
$unique_terms = array_unique( $all_terms );
$result = array_unique($unique_terms);
foreach ($result as $value) {
echo $value . '<br />';
}
?>
But I can't figure how to run the query & put a "Where" clause in it, like you can with MySQL. Any help / suggestion or even point me in the right direction would be greatly appreciated. I'm stuck
Check this function: wp_get_post_terms()
Assuming your post supports two taxonomies called tax_a and tax_b, you can try something like this, exactly where you wrote your comment:
<?php $terms = wp_get_post_terms( $query->post->ID, array( 'tax_a', 'tax_b' ) ); ?>
<?php foreach ( $terms as $term ) : ?>
<p><?php echo $term->taxonomy; ?>: <?php echo $term->name; ?></p>
<?php endforeach; ?>
This will print all the terms for each post retrieved by the query.
EDIT
If what you want is all the terms in all the posts retrieved by the query, you can store the values in an array and then use a function like array_unique(), like this:
$all_terms = array();
foreach ( $terms as $term ) {
$all_terms[] = $term->name;
}
// ... and outside the WHILE loop
$result = array_unique( $all_terms );
foreach ( $result as $term ) {
echo $term . '<br/>;
}