web-dev-qa-db-fra.com

Filtrer les publications avant le nombre de catégories d'affichage

J'ai la clé méta "Due" pour les publications .... si une publication a expiré (au-delà de la date du jour ou n'est pas la même), elle ne devrait pas être affichée dans le compte (j'ai le code pour afficher le compte qui s'affiche en fondu Par exemple, à l'heure actuelle, on dit qu'il en a 89, mais il ne devrait en être que 26 car le reste a expiré. Alors, comment puis-je filtrer les publications avant que le compte ne soit affiché?

Voici mon code:

<?php
$categories = get_categories('hide_empty=0&exclude=13,1,1460&orderby=count&order=DESC&number=6');
if (!empty($categories)) {

    $i = 0;

    foreach ($categories as $cat) {
        $class = ( $i % 3 ) ? 'span4' : 'span4';

        $thumbnail_id = get_option('seamless_term_thumb_' . $cat->term_id);
        $image        = wp_get_attachment_url($thumbnail_id);

        get_the_category()

?>

        <div class="<?php echo $class . ' ' . 'category-' . $cat->term_id;?>">
            <div class="thumb one">
                <?php
                    echo '<a href="' . get_field('category_link', $post->ID) . '">' . '<div class="two">' . $cat->count . '</div>' . '</a>';
                ?>
                <a href="<?php echo get_field('category_link', $post->ID); ?>">
                    <img src="<?php echo $image; ?>" alt="<?php echo get_cat_name($cat->term_id); ?>" class="item-image">
                </a>
            </div>  <!-- end .thumb -->
        </div>

<?php $i++; } } ?>

Voici le code nouveau et mis à jour qui fonctionne, mais a créé BEAUCOUP de requêtes:

/**
 * Function to list all category with thumbnail custom link, etc..
 *
 * How to use this function:
 * Add in template: <?php my_category_list(); ?>
 *
 */
function my_category_list(){

/* LIST OF CATS DATA */
$cats_data = array();

/**
 * Get Categories
 * @link http://codex.wordpress.org/Function_Reference/get_categories
 */
$cat_args = array(
    'hide_empty'    => 0,
    'exclude'       => '13,1,1460'
);
$categories = get_categories( $cat_args );

/* If category found, load list of category */
if ( !empty( $categories ) ) {
    $i = 0;

    /* Foreach category: display the data */
    foreach ( $categories as $cat) {

        /* ======= HTML CLASS ========= */
        /* dynamic class (?) need fix */
        $class = ( $i % 3 ) ? 'span4' : 'span4';
        $classes = $class . ' ' . 'category-' . $cat->term_id;

        /* ======= POST COUNT ========= */
        /* Get all posts in category + in due date
         * this only to get the post count.
         * @link http://themehybrid.com/support/topic/issue-with-filtering-due-meta-key
         */
        $query_args = array(
            'post_type'       => 'post',
            'category_name'   => $cat->slug,
            'meta_query' => array(
                array(
                    'key'        => 'Due',
                    'value'      => date( 'Ymd' ),
                    'type'       => 'DATE',
                    'compare'    => '>=', // greater than or equal to
                )
            )
        );
        $my_query = new WP_Query( $query_args );
        $post_count = $my_query->found_posts;

        /* ====== CATEGORY THUMBNAIL ======== */
        $thumbnail_id = get_option('seamless_term_thumb_' . $cat->term_id);
        $image = wp_get_attachment_url($thumbnail_id);

        /* ====== LINK TO SEARCH: no need fields ======= */
        $link_to = 'http://www.scholarships360.org/discover/?search_query=&orderby=blank&tax_category=' . $cat->slug .'&wpas=1';

        /* MERGE DATA IN ARRAY */
        $cats_data[] = array(
            'classes'      => $classes,
            'post_count'   => $post_count,
            'image'        => $image,
            'name'         => $cat->name,
            'link'         => $link_to,
        );

        $i++;
    } // end foreach

    /**
     * NOW THE FUN PART
     * =================
     */

    /* Sort Cat Data by Post Count */
    usort($cats_data, 'my_sort_cat_data');

    /* Cut only 6 item to display */
    $cats_data = array_slice( $cats_data, 0, 6 );

    /* Display it */
    foreach ($cats_data as $cat_data ){ ?>

        <div class="<?php echo $cat_data['classes'];?>">
            <div class="thumb one">
                <a href="<?php echo $cat_data['link'] ?>">
                    <div class="two"><?php echo  $cat_data['post_count'] . ' Scholarships' ?></div>
                </a>
                <a href="<?php echo $cat_data['link'] ?>">
                    <img src="<?php echo $cat_data['image']; ?>" alt="<?php echo esc_attr( $cat_data['name'] ); ?>" class="item-image">
                </a>
            </div>  <!-- end .thumb -->
        </div>
    <?php 
    }
}
/* No category found */
else {
    echo '<p>No category found...</p>';
}
}

 /**
 * Sort Cat Data Helper Function
 * @link http://stackoverflow.com/questions/2699086/sort-multidimensional-array-by-value-2
 */
function my_sort_cat_data( $a, $b ){
 return $b['post_count'] - $a['post_count'];
}
2
Jagst3r15

Comme l'a souligné @ Jagst3r15, vous allez devoir parcourir les publications de chaque catégorie, peu importe le filtrage des publications contenant votre clé méta.

Pour une réponse de haut niveau: je suggérerais de regarder la logique INNER JOIN qui se produit dans _pad_term_counts() pour obtenir un exemple montrant comment importer le tableau posts. Ensuite, filtrez sur le crochet get_terms pour parcourir ces résultats et exclure ce dont vous n’avez pas besoin.

C'est une bonne question avec une réponse pas très simple, malheureusement.

1
DrewAPicture