Wordpress - Grab Item by Category in Post Type Que

2019-08-30 04:02发布

问题:

I have setup a custom post type in Wordpress. It is a section where the client can upload documents in PDF format. The documents are divided into two categories - 'Downloadable Forms' and 'Menu' ; the Custom Field name for the category is 'document_category'

I am trying to run a query and only display the 'Downloadable Forms' on a page. Here is the code I normally use --- I'm hoping someone can help me add what I need to make it work?

<?php       
$args = array('post_type' => 'prep_forms', 'posts_per_page' => -1);
// The Query
$the_query = new WP_Query( $args );
// The Loop
$i = 0;
while ( $the_query->have_posts() ) : $the_query->the_post();
?>
<li><a href="<?php echo get_field('document_pdf_file'); ?>"><?php the_title(); ?></a></li>
<?php
$i++;
endwhile;
// Reset Post Data
wp_reset_postdata();
?>

Thanks.

回答1:

<?php       
$args = array('post_type' => 'prep_forms', 'posts_per_page' => -1);
// The Query
$the_query = new WP_Query( $args );
// The Loop
$i = 0;
while ( $the_query->have_posts() ) : 
    $the_query->the_post();
    // Get all Advanced custom fields
    $my_custom_fields = get_fields(get_the_ID());
    // Does document_category field exists and is it equal with 'Downloadable Forms'?
    if(isset($my_custom_fields['document_category']) && $my_custom_fields['document_category'] == 'Downloadable Forms'):
?>
<li><a href="<?php echo get_field('document_pdf_file'); ?>"><?php the_title(); ?></a></li>
<?php
    endif;
$i++;
endwhile;
// Reset Post Data
wp_reset_postdata();
?>

I've used the get_fields($post_id = false) function of the Advanced Custom Fields plugin that returns an array of all the posts' custom fields and then filtered it to match your needs.

Hope i've helped



回答2:

Well, you can use the is_category(); inside loop and only display those posts, like so (using your same code):

<?php       
$args = array('post_type' => 'prep_forms', 'posts_per_page' => -1);
// The Query
$the_query = new WP_Query( $args );
// The Loop
$i = 0;
while ( $the_query->have_posts() ) : $the_query->the_post();

if(is_category('Sownloadable Forms')){  // here the condition

?>
<li><a href="<?php echo get_field('document_pdf_file'); ?>"><?php the_title(); ?></a></li>
<?php
$i++;
} // here ends the IF statment
endwhile;
// Reset Post Data
wp_reset_postdata();
?>

Better yet: http://codex.wordpress.org/Function_Reference/is_category

Hope that helps.



回答3:

Why dont you make it simpler and set a query based on custom field data?

<?php 
$count = 1;
$args = array(
  'post_type' => 'any',
  'posts_per_page' => 4,
  'meta_key' => 'display', 
  'meta_value' => 'true'
);

$query = new WP_Query();$query->query($args); 
while ($query->have_posts()) : $query->the_post();                      
$do_not_duplicate[] = $post->ID;
?>
    <!-- query content -->
    <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php printf( esc_attr__( 'Permalink to %s', 'advanced' ), the_title_attribute( 'echo=0' ) ); ?>" ></h2>
    <?php the_excerpt() ;?>
    <!-- query content -->
<?php $count++; endwhile; wp_reset_query(); ?>

That way any post with custom field key display and custom field value true will be shown on your query.



标签: wordpress