Group by a custom value field wordpress posts - php

I have several posts with a custom field named "series". I want to group all posts by this custom field and below of this i want to list all posts which have not this custom field.
First i wanted to get grouped custom field value and then to be able to query again for all posts with this custom value key and value. But even trying to get the unique custom values does not work.
What i tried is this:
<?php
function query_group_by_filter($groupby){
global $wpdb;
return $wpdb->postmeta . '.meta_key = "series"';
}
?>
<?php add_filter('posts_groupby', 'query_group_by_filter'); ?>
<?php $states = new WP_Query(array(
'meta_key' => 'series',
'ignore_sticky_posts' => 1
));
?>
<?php remove_filter('posts_groupby', 'query_group_by_filter'); ?>
<ul>
<?php
while ( $states->have_posts() ) : $states->the_post();
$mykey_values = get_post_custom_values( 'series' );
foreach ( $mykey_values as $key => $value ) {
echo "<li>$key => $value ( 'series' )</li>";
}
endwhile;
?>
</ul>
Whats wrong with this code?

WP_Meta_Query
All posts with custom value:
<?php
$args = array(
'post_type' => 'my-post-type',
'post_status' => 'publish',
'posts_per_page' => -1,
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'series',
'value' => 'my-val',
'compare' => '='
),
array(
'key' => 'series',
'value' => '',
'compare' => '!='
)
)
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) : ?>
<ul>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<li id="post-<?php the_ID(); ?>">
<?php the_title(); ?>
</li>
<?php
endwhile;
wp_reset_postdata(); ?>
</ul>
<?php endif; ?>
And without values:
<?php
$args = array(
'post_type' => 'my-post-type',
'post_status' => 'publish',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'series',
'value' => '',
'compare' => '='
)
)
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) : ?>
<ul>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<li id="post-<?php the_ID(); ?>">
<?php the_title(); ?>
</li>
<?php
endwhile;
wp_reset_postdata(); ?>
</ul>
<?php endif; ?>

Related

ACF not showing below products

I can't seem to work out why my ACF fields will not display below my woo-commerce products on the featured page when they display fine above them?
Products
<div id="post-load" class="row products">
<?php
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'nopaging' => true,
'tax_query' => array(
array(
'taxonomy' => 'product_visibility',
'field' => 'name',
'terms' => 'featured',
'operator' => 'IN'
),
),
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
global $product; ?>
<?php wc_get_template_part( 'content', 'product' ); ?>
<?php endwhile; ?>
</div>
ACF Example (This works fine if placed above the products)
<?php
$features = get_field('features');
if( $features ): ?>
<div class="col-md-12"><h3 class="txtc"><?php echo esc_html( $features['title'] ); ?></h3></div>
<?php endif; ?>

Product category in repeater fields

Im trying to display specific product categories in an ACF repeater field.
This keeps outputting all product categories.
How do I go to output single category per repeater field?
<?php if( have_rows('product_categories') ): ?>
<ul class="products">
<?php while( have_rows('product_categories') ): the_row(); ?>
<?php
$product_category_ids = get_sub_field('project_category');
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'tax_query' => array(
'taxonomy' => 'product_cat',
'terms' => $product_category_ids
),
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
wc_get_template_part( 'content', 'product' ); ?>
<?php
endwhile;
} else {
echo __( 'No products found' );
}
wp_reset_postdata();
?>
<?php endwhile; ?>
</ul>
This worked for me:
<?php if( have_rows('product_categories') ): ?>
<ul class="products">
<?php while( have_rows('product_categories') ): the_row(); ?>
<?php
$product_cats = get_sub_field('product_category');
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => '12',
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => $product_cats,
'operator' => 'IN',
'field' => 'slug'
),
)
);
$loop = new WP_Query($args);
while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>
<?php wc_get_template_part( 'content', 'product' ); ?>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
<?php endwhile; ?>
</ul>

Pagination on "get_term_children" - Wordpress

I would like to get a pager in my page, can i create a pager with a for each or while without parameter like "posts_per_page" ?
I get term children of my taxonomy and i would like to display 3 by 3
My code :
$childGalerie = get_term_children(15,'wpmf-category');
foreach ( $childGalerie as $oneGalerie ) :
$maGalerie = get_term($oneGalerie, 'wpmf-category');
?>
<h2 class="info-galerie"><span></span><?php echo $maGalerie->name; > </h2>
<div class="galerie-photo">
<?php
global $post;
$args = array(
'posts_per_page' => -1,
'post_type' => 'attachment',
// 'post_status' => 'inherit',
'tax_query' => array(
array(
'taxonomy' => 'wpmf-category',
'field' => 'term_id',
'terms' => $oneGalerie
),
),
);
$media = get_posts( $args );
foreach ( $media as $post ) : setup_postdata( $post );
?>
<div class="wrapper-galerie">
<?php echo wp_get_attachment_image( $media->ID, 'galerie->photo' ); ?>
</div>
<?php
endforeach;
wp_reset_postdata();
?>
</div>
<?php
endforeach;
Can you help me please , ty :D

Display wordpress category name and the related posts

I want to display category name on top and then the posts belonging to the particular category below it.
This should happen for all of my 20 categories and the results show be displayed on one page.
This is something I was trying but it doesn't work.
<?php $catquery = new WP_Query( 'cat=finance-training-seminars&posts_per_page=-1&post-type=dt_portfolio' ); ?>
<ul>
<?php while($catquery->have_posts()) : $catquery->the_post(); ?>
<li><?php the_title(); ?></li>
<?php endwhile; ?>
</ul>
<?php wp_reset_postdata(); ?>
You can use get_terms to list all categories and then create a query for each category and show posts related to it, something like this:
<?php
$categories = get_terms( array( 'taxonomy' => 'category' ) );
foreach( $categories as $cat ) :
$posts = new WP_Query( array(
'post_type' => 'dt_portfolio',
'showposts' => -1,
'tax_query' => array(
array(
'taxonomy' => 'category',
'terms' => array( $cat->term_id ),
'field' => 'term_id'
)
)
) ); ?>
<h3><?php echo $cat->name; ?></h3>
<ul>
<?php while( $posts->have_posts() ) : $posts->the_post(); ?>
<li><?php the_title(); ?></li>
<?php endwhile; wp_reset_postdata(); ?>
</ul>
<?php endforeach; ?>
Try this,
<?php
$rCateogry = get_the_terms( $post->ID, 'CUSTOM_TAXONOMY_NAME' );
$related = get_posts(array( 'post_type' => 'CUSTOM_POST_TYPE', 'tax_query' => array(array( 'taxonomy' => 'CUSTOM_TAXONOMY_NAME', 'field' => 'id', 'terms' => $rCateogry, )), 'showposts' => -1, 'order' => 'DESC', 'post__not_in' => array($post->ID) )); ?>
<section id="discover">
<h2>Related Posts</h2>
<div class="singlepost">
<?php if( $related ) foreach( $related as $post ) {
setup_postdata($post); ?>
<h4><?php the_title(); ?></h4>
<?php the_excerpt(); ?>
<?php $cats = array();
foreach($rCateogry as $c){
$cat = get_category( $c );
echo $cat->name;
} ?>
<?php }
wp_reset_postdata(); ?>
</div>
</section>
Replace "CUSTOM_TAXONOMY_NAME" with the taxonomy name and "CUSTOM_POST_TYPE" with post type name

Show most recent post - if "yes" chosen as radio button value

Here is the setup for my radio button:
I can't seem to get the posts that are valued at yes to display in the loop..
Here is my loop:
<?php
$args = array(
'numberposts' => 1,
'post_type' => 'event',
'posts_per_page' => '1',
'meta_key' => 'sponsored_event',
'meta_value' => 'yes'
);
$the_query = new WP_Query( $args );
?>
<?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div class="sponsored-event">
<div class="sponsored-image" style="background-image: url(<?php the_field( 'event_image' ); ?>);"></div>
<div class="sponsored-info">
<h2>Sponsored Event</h2>
<h1><strong><?php the_title(); ?></strong></h1>
<p><strong>Date</strong></p><br>
<p class="place"><?php the_field( 'event_location' ); ?></p>
<p class="time"><?php the_field( 'event_time' ); ?></p>
<p><?php the_field( 'excerpt' ); ?></p>
</div>
</div>
<?php endwhile; else: ?>
<?php endif; ?>
This is what worked for me:
<?php
$args = array(
'post_type' => 'event',
'showposts' => 1,
'orderby' => 'date',
'meta_query' => array(
array(
'key' => 'sponsored_event',
'value' => 1,
'compare' => 'LIKE'
)
)
);
$the_query = new WP_Query( $args );
?>
<?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<a href="<?php the_permalink(); ?>"><div class="sponsored-event">
<div class="sponsored-image" style="background-image: url(<?php the_field( 'event_image' ); ?>);">
</div>
<div class="sponsored-info">
<h2>Sponsored Event</h2>
<h1><strong><?php the_title(); ?></strong></h1>
<p><strong>Date</strong></p><br>
<p class="place"><?php the_field( 'event_location' ); ?></p>
<p class="time"><?php the_field( 'event_time' ); ?></p>
<p><?php the_field( 'excerpt' ); ?></p>
</div>
</div></a>
<?php endwhile; else: ?>
<?php endif; ?>
if ( have_posts() ) should reference the query: if ( $the_query->have_posts() ).
Also, as has already been mentioned, you should use 'posts_per_page' => 1, instead of numberposts.
Try using a meta_query to accomplish this. Update your $args with something like the following:
$args = array(
'post_type' => 'event',
'posts_per_page' => '1',
'meta_query' => array(
array(
'key' => 'sponsored_event',
'compare' => 'LIKE',
'value' => 'yes',
),
),
);
You also need to update if ( have_posts() ) to if ( $the_query->have_posts() ).
More information about meta_query can be found here: https://codex.wordpress.org/Class_Reference/WP_Meta_Query
Instead of using both post_per_page and number posts you can use any one of your choice
Try any of the Methods as available below. As per the ACF the Method 1 is suggested but as for as the WordPress you can retrieve information based on Method 2 also.
Method:1
$args = array(
'posts_per_page' => 1,
'post_type' => 'event',
'meta_key' => 'sponsored_event',
'meta_value' => 'yes'
);
$the_query = new WP_Query( $args );
<?php if ($the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
//Your Code over here to Manipulate
<?php endwhile; else: ?>
<?php endif; ?>
Method:2
Try the meta query with compare as = operator in the meta_query.
$args = array(
'post_type' => 'event',
'posts_per_page' => '1',
'meta_query' => array(
array(
'key' => 'sponsored_event',
'compare' => '=',
'value' => 'yes',
),
),
);
I tested the code on my machine and it works fine! Just (as already mentioned from others) the if() statement is wrong, change it to:
if ( $the_query->have_posts() )
But now I have a really silly question... After creating the ACF field, have you saved some posts (events) with the meta field (yes or no) ? If not, WP wont find anything because the postmeta is not saved into the DB.
Did you took a look into the DB if the postmeta is saved correctly?

Categories