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

本秂侑毒 提交于 2019-12-25 18:47:07

问题


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

回答1:


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



回答2:


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.




回答3:


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




回答4:


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',
      ),
    ),
);



回答5:


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?



来源:https://stackoverflow.com/questions/39276798/show-most-recent-post-if-yes-chosen-as-radio-button-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!