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

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?

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; ?>

pagination doesn't work on a single page of WordPress

Pagination doesn't work on a single page of WP.
In the code below, everything looks great except the pagination shows the same contents on the next page.
I tried every possible solution I found on the internet, but none of them worked. I'm relatively new to WP and php, so if you could pinpoint the code that might be wrong, that'd be really helpful.
<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged'
) : 1;
$args = array(
'posts_per_page' => 3,
'paged' => $paged,
'post_type' => 'book',
'offset' => 1,
'tax_query' => array(
array(
'taxonomy' => 'news_cat',
'field' => 'slug',
'terms' => array( 'disney' )
),
),
);?>
<?php $the_query = new WP_Query( $args );
?>
<?php if ($the_query->have_posts()) ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<a href="<?php echo ('/test/'.get_the_ID()); ?> "ontouchstart="" >
<li>
<div>
<figure><img src="<?php the_field('thumb'); ?>" alt="">
</figure>
</div>
<span><em><?php the_field('category'); ?></em><?php the_field('date'); ?></span>
<p><?php the_title(); ?></p>
</li>
</a>
<?php endwhile; ?>
<?php $GLOBALS['wp_query']->max_num_pages = $the_query->max_num_pages;
$args = array (
'prev_text' => '',
'next_text' => '',
'show_all' => false,
'mid_size' => 1,
'type' => 'list'
);
the_posts_pagination($args);?>
I expect the pagination to work correctly instead of showing the same contents on all pages.
As you see, I want to make the pagination work only on the posts with "disney" slug.
You can replace the code instead of your old code
<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged'
) : 1;
$args = array(
'posts_per_page' => 3,
'paged' => $paged,
'post_type' => 'book',
'offset' => 1
),
);?>
<?php $the_query = new WP_Query( $args );
?>
<?php if ($the_query->have_posts()) ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<a href="<?php echo ('/test/'.get_the_ID()); ?> "ontouchstart="" >
<li>
<div>
<figure><img src="<?php the_field('thumb'); ?>" alt="">
</figure>
</div>
<span><em><?php the_field('category'); ?></em><?php the_field('date'); ?></span>
<p><?php the_title(); ?></p>
</li>
</a>
<?php endwhile; ?>
<?php $GLOBALS['wp_query']->max_num_pages = $the_query->max_num_pages;
$args = array (
'prev_text' => '',
'next_text' => '',
'show_all' => false,
'mid_size' => 1,
'type' => 'list'
);
the_posts_pagination($args);?>
Now, you can check it that pagination is working or not.

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

Group by a custom value field wordpress posts

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; ?>

using post ID inside reverse ACF relationship querie

I’m using relationship field on one of my custom post type (releases).
all my realease have a relationship field with “artists” post, so all my releases are associated with one artist.
now i’m trying to perform a reverse query to display all the “releases” associated with the artist selected in my relationship field.
here is my code so far :
<?php
$releases = get_posts(array(
'post_type' => 'releases',
'numberposts'=> -1,
'post__not_in' => array( $post->ID ),
'meta_query' => array(
array(
'key' => 'artist',
'value' => 191,
'compare' => 'LIKE'
)
)
));?>
<?php if( $releases ): ?>
<?php foreach( $releases as $release ): ?>
<div class="col-xs-2 text-center">
<a href="<?php echo get_permalink( $release->ID ); ?>" class="related_releases" title="<?php echo get_the_title($release->ID); ?>">
<img src="<?php echo get_the_post_thumbnail_url($release->ID, 'full'); ?>">
</a>
</div>
<?php endforeach; ?>
<?php endif; ?>
with this code, all the releases from artist ID “191” are displayed on y custom post type.
the problem is that I’m trying to display the releases from the artist associated with the “release” post.
what I’m trying to do is to get the ID of the artist inside my querie, like this (width “get_the_ID()” instead of “191”)
<?php
$releases = get_posts(array(
'post_type' => 'releases',
'numberposts'=> -1,
'post__not_in' => array( $post->ID ),
'meta_query' => array(
array(
'key' => 'artist',
'value' => get_the_ID(),
'compare' => 'LIKE'
)
)
));?>
but I get the ID of the “release” post, not the ID of the artist.
can anyboby help me with this ?
hope you understand my issue, sorry for my bad english
Will this work instead in your array
'value' => get_the_author_meta( $user_id ),
Try below code. i think this will work.
<?php
$artist = get_post_meta (get_the_ID(),'artist');
$releases = get_posts(array(
'post_type' => 'releases',
'numberposts'=> -1,
'post__not_in' => array( $post->ID ),
'meta_query' => array(
array(
'key' => 'artist',
'value' => $artist[0],
'compare' => 'LIKE'
)
)
));?>
<?php if( $releases ): ?>
<?php foreach( $releases as $release ): ?>
<div class="col-xs-2 text-center">
<a href="<?php echo get_permalink( $release->ID ); ?>" class="related_releases" title="<?php echo get_the_title($release->ID); ?>">
<img src="<?php echo get_the_post_thumbnail_url($release->ID, 'full'); ?>">
</a>
</div>
<?php endforeach; ?>
<?php endif; ?>
I've found a solution :
<?php
$posts = get_field('artist'); if( $posts ):
foreach(array_slice($posts, 0, 1) as $post):
setup_postdata($post);
$post_ID = get_the_ID();
endforeach;
wp_reset_postdata();
endif;
$releases = get_posts(array(
'post_type' => 'releases',
'numberposts'=> -1,
'post__not_in' => array( $post->ID ),
'meta_query' => array(
array(
'key' => 'artist',
'value' => $post_ID,
'compare' => 'LIKE'
)
)
));?>
<?php if( $releases ): ?>
<?php foreach( $releases as $release ): ?>
<div class="col-xs-2 text-center">
<a href="<?php echo get_permalink( $release->ID ); ?>" class="related_releases" title="<?php echo get_the_title($release->ID); ?>">
<img src="<?php echo get_the_post_thumbnail_url($release->ID, 'full'); ?>">
</a>
</div>
<?php endforeach; ?>
<?php endif; ?>

Categories