1

カテゴリページの上部にドロップダウンメニューを表示して、投稿を日付でフィルタリングできるようにしたいと考えています。

私はおそらくカスタムフィールドを使用する必要がありますが、それは問題ではありません。

GETスタイル変数を使用してカスタム投稿クエリを作成できることは知っていますが、きれいなURLが有効になっていると、GET変数を使用して特定の投稿(例www.domain.com/category/?orderby=title&order=ASCなど)をフィルタリングできないようです。

私はプラグインを探してみましたが、必要なものが何も飛び出していないようです。また、私の状況に対する適切な解決策がない、同様の主題についての多くの話に気づきました。

4

2 に答える 2

2

一般的なクエリは次のようになります。

<?php $posts = query_posts( $query_string . '&orderby=date&order=asc' ); ?>
    <?php if( $posts ) : ?>
    //whatever
            <?php foreach( $posts as $post ) : setup_postdata( $post ); ?>
        //whatever
            <p><?php the_content(); ?></p>
                <?php endforeach; ?>

    <?php endif; ?>

ドロップダウンの場合、次のようなことができます:

$args = $args=array(
      'cat' => $cat_id,
      'post_type' => 'post',
      'post_status' => 'publish',
      'posts_per_page' => -1,
      'orderby' => 'DATE', 
       'order' => 'ASC' // or DESC
    );

<form action="<? bloginfo('url'); ?>" method="get">
 <select name="page_id" id="page_id">
 <?php
 global $post;
 $args = array( 'numberposts' => -1);
 $posts = get_posts($args);
 foreach( $posts as $post ) : setup_postdata($post); ?>
                <option value="<? echo $post->ID; ?>"><?php the_title(); ?></option>
 <?php endforeach; ?>
 </select>
 <input type="submit" name="submit" value="view" />
 </form>

そして別のオプション:

<?php
    $cat_id = get_cat_ID('uncategorized'); //your-category
    $args=array(
      'cat' => $cat_id,
      'post_type' => 'post',
      'post_status' => 'publish',
      'posts_per_page' => -1,
      'orderby' => 'DATE', 
       'order' => 'ASC' // or DESC
    );
    $my_query = null;
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
?>
    <form name="jump">
        <select name="menu">
            <?php
              while ($my_query->have_posts()) : $my_query->the_post(); ?>
                <option value="<?php the_permalink() ?>"><?php the_title(); ?></option>
                <?php

              endwhile;
            }
            ?>
        </select>
        <input type="button" onClick="location=document.jump.menu.options[document.jump.menu.selectedIndex].value;" value="Go">
    </form>

<?php
    wp_reset_query();
?>
于 2012-07-04T12:19:32.567 に答える
0

日付で検索投稿を探していました。スタックオーバーフローで適切な解決策を見つけることができませんでした。次に、wp_query オブジェクトを出力して、このことを整理しました。それは私のために働いた。私のシナリオでは、タイトルまたは日付で投稿を検索しています。これがフックのコードです。

function SearchFilter($query) {
if ($query->is_search) {
    $query->set('post_type', 'post');
    // check if query is a date
    $search_query   = $query->query['s'];
    $date_format        = DateTime::createFromFormat('d/M/Y', $search_query);
    if ($date_format)
    {
        $dte            = date('j',$date_format->getTimestamp());
        $month          = date('n',$date_format->getTimestamp());
        $year           = date('Y',$date_format->getTimestamp());
    }
    if (isset($dte) && isset($month) && isset($year)) {
        unset($query->query['s']);
        unset($query->query_vars['s']);
        $query->query['date_query'] = array(
                    array(
                        'year'  => $year,
                        'month' => $month,
                        'day'   => $dte,
                    )
                );
        $query->set('date_query', array(
                        array(
                            'year'  => $year,
                            'month' => $month,
                            'day'   => $dte,
                        )
                    )
                );

    }
}
return $query;
}

add_filter('pre_get_posts','SearchFilter');

お気づきのとおり、このフィルターは、渡されたパラメーターが文字列か日付かを自動的にチェックします。使用している

$query->set('post_type', 'post')

投稿のみの結果を取得するには、ページもフェッチします。各投稿の下に日付があり、その日付に href を追加できるとします。その日付のすべての投稿を href の最後に取得するには、 ?s=something のような検索パラメーターを追加します

http://my.blog?s=1/Jun/2015

などのテンプレートについては、次のようなデフォルトのテンプレート関数を使用するだけで、独自のカスタムを作成する必要はありませんhave_posts()

于 2015-06-15T09:33:25.280 に答える