サポート » 使い方全般 » 記事一覧の時のソートができません

  • 解決済 gakusei

    (@gakusei)


    ワードプレス初心者です。
    フォーラムも2度目ですが、よろしくお願いします。

    サイトを作るときに、ボタン一つで記事の並べ替えをしたいと思い、フォーラムを見たところ、
    iyaiyaさんという方が
    (記事一覧をドロップダウンリストでソートしたい)
    というトピックを立てられていたので、そちらに出ているコードを
    貼り付けてみたのですが、iyaiyaさんと同じように、トップページに戻されてしまいました。

    tanaka_kyousukeさんやnobitaさんのご指摘なども参考にしつつ、
    コードの改善やプラグインの全停止などを行ってみましたが、
    トップページに戻されてしまいました。

    最終的に、iyaiyaさんは3.4で動いたというお話でしたので、
    実際に3.5から3.4へなんとかダウングレードを行いましたが、
    やはりトップページに戻されてしまいました。

    ソートで良い方法はあるのでしょうか?

    もし、わかる方がいらっしゃいましたら、ぜひ教えていただきたいです。
    よろしくお願いします。

15件の返信を表示中 - 31 - 45件目 (全48件中)
  • gakusei さん

    このまま、このトピックに質問の返事をしても大丈夫かどうか、ちょっと疑問に思いましたが、
    そのままご返事することにしました。

    あのコードは、is_category、is_tagとあるように、カテゴリ一覧とタグ一覧のみを想定したコードでしたので、gakuseiさんのおっしゃる通り、投稿者一覧のページと検索結果のページのソートにはしておりません。

    なので、書きにそれぞれの場合を記載します。
    (本当はそれぞれにフックさせたほうが楽なのですが、とりあえず)

    検索結果一覧のソートの場合は、検索ワードを取得し、query_posts()と現在のurlに値をセットしなくてはいけません。

    なので、

    • get_query_var()等で検索ワードを取得
    • $uriに検索ワードを足す
    • query_posts()に指定している配列$argsにも検索ワードを足す

    以上で、検索結果のページにソートが可能になるのではと思います。
    具体的なコードは、

    <?php $uri = home_url( '/' ); ?>
        <?php if( is_search() ) : ?>
          <?php $term = get_queried_object(); ?>
          <?php if ( $term ) : ?>
            <?php $uri = get_term_link( $term , $term->taxonomy ); ?>
          <?php endif; ?>
          <?php $search_query = get_query_var('s'); ?>
          <?php if ( $search_query ) : ?>
            <?php $uri .= '?s=' . $search_query; ?><br>
          <?php endif; ?>
        <?php endif; ?>
    
        ドロップダウン形式<br>
    		<?php $DropSortList = array(); ?>
    		<?php $DropSortList[] = array( "sort" => array( "orderby" => "date" , "order" => "DESC" ) , "label" => "日付の新しい順" ); ?>
    		<?php $DropSortList[] = array( "sort" => array( "orderby" => "date" , "order" => "ASC" ) , "label" => "日付の古い順" ); ?>
    		<?php $DropSortList[] = array( "sort" => array( "orderby" => "title" , "order" => "DESC" ) , "label" => "タイトル順(わ~あ・Z~A)" ); ?>
    		<?php $DropSortList[] = array( "sort" => array( "orderby" => "title" , "order" => "ASC" ) , "label" => "タイトル順(A~Z・あ~わ)" ); ?>
    		<?php $DropSortList[] = array( "sort" => array( "meta_key" => "ratings_average" , "order" => "DESC" ) , "label" => "評価平均の高い順" ); ?>
    		<?php $DropSortList[] = array( "sort" => array( "meta_key" => "ratings_average" , "order" => "ASC" ) , "label" => "評価平均の低い順" ); ?>
    		<?php $DropSortList[] = array( "sort" => array( "meta_key" => "ratings_score" , "order" => "DESC" ) , "label" => "評価総合の多い順" ); ?>
    		<?php $DropSortList[] = array( "sort" => array( "meta_key" => "ratings_score" , "order" => "ASC" ) , "label" => "評価総合の少ない順" ); ?>
    		<?php $DropSortList[] = array( "sort" => array( "meta_key" => "ratings_users" , "order" => "DESC" ) , "label" => "評価をつけた人数の多い順" ); ?>
    		<?php $DropSortList[] = array( "sort" => array( "meta_key" => "ratings_users" , "order" => "ASC" ) , "label" => "評価をつけた人数の少ない順" ); ?>
    		<?php $DropSortList[] = array( "sort" => array( "orderby" => "comment_count" , "order" => "DESC" ) , "label" => "コメントの多い順" ); ?>
    		<?php $DropSortList[] = array( "sort" => array( "orderby" => "comment_count" , "order" => "ASC" ) , "label" => "コメントの少ない順" ); ?>
    		<?php $DropSortList[] = array( "sort" => array( "meta_key" => "views" , "order" => "DESC" ) , "label" => "アクセス数の多い順" ); ?>
    		<?php $DropSortList[] = array( "sort" => array( "meta_key" => "views" , "order" => "ASC" ) , "label" => "アクセス数の少ない順" ); ?>
    		<select name="original-sort-dropdown" onchange='document.location.href=this.options[this.selectedIndex].value;'>
    			<?php if( empty( $_GET["order"] ) ) : ?>
    				<option value="">ソート順を選択</option>
    			<?php else: ?>
    				<?php $Sorted = array(); ?>
    				<?php foreach($DropSortList as $key => $SortList) : ?>
    					<?php if( !empty( $_GET["meta_key"] ) && !empty( $SortList["sort"]["meta_key"] ) ) : ?>
    						<?php if( $_GET["order"] == $SortList["sort"]["order"] && $_GET["meta_key"] == $SortList["sort"]["meta_key"] ) : ?>
    							<?php $Sorted = $key; ?>
    							<?php break; ?>
    						<?php endif; ?>
    					<?php elseif( !empty( $_GET["orderby"] ) && !empty( $SortList["sort"]["orderby"] ) ) : ?>
    						<?php if( $_GET["order"] == $SortList["sort"]["order"] && $_GET["orderby"] == $SortList["sort"]["orderby"] ) : ?>
    							<?php $Sorted = $key; ?>
    							<?php break; ?>
    						<?php endif; ?>
    					<?php endif; ?>
    				<?php endforeach; ?>
    			<?php endif; ?>
    			<?php if( !empty( $Sorted ) ) : ?>
    				<option value="<?php echo add_query_arg( $DropSortList[$key]["sort"] , $uri ); ?>" selected="selected"><?php echo $DropSortList[$key]["label"]; ?></option>
    				<?php unset( $DropSortList[$key] ); ?>
    			<?php endif; ?>
    			<?php foreach($DropSortList as $key => $SortList) : ?>
    				<option value="<?php echo add_query_arg( $SortList["sort"] , $uri ); ?>"><?php echo $SortList["label"]; ?></option>
    			<?php endforeach; ?>
    		</select>
            </div>   
    
        <?php
        // 何の指定もないときのデフォルトのソート順
        $args = array( 'nopaging' => 1 , 'orderby' => 'date' , 'order' => 'DESC' );
    
        if( is_category() ) {
          $args["cat"] = get_query_var( "cat" );
        } elseif( is_tag() ) {
          $args["tag"] = get_query_var( "tag" );
    	}
    	$args["s"] = get_query_var( "s" );
    
        if( !empty( $_GET["meta_key"] ) ) {
          $args["meta_key"] = strip_tags( $_GET["meta_key"] );
          $args["orderby"] = 'meta_value_num';
          } elseif( !empty( $_GET["orderby"] ) ) {
          $args["orderby"] = strip_tags( $_GET["orderby"] );
          unset( $args["meta_key"] );
    
        }
    
        if( !empty( $_GET["order"] ) ) {
          $args["order"] = strip_tags( $_GET["order"] );
        }
        ?>
    
        <?php query_posts( $args ); ?>

    投稿者一覧の場合は

    • author のID を取得
    • $uriを投稿者一覧のurlに変更
    • query_posts()に指定している配列$argsにauthorIDを指定

    となると思うので、コードは、

    <?php $uri = home_url( '/' ); ?>
        <?php if( is_author() ) : ?>
          <?php $term = get_queried_object(); ?>
          <?php if ( $term ) : ?>
            <?php $uri = get_author_posts_url( $term->ID ); ?>
          <?php endif; ?>
        <?php endif; ?>
    
        ドロップダウン形式<br>
    		<?php $DropSortList = array(); ?>
    		<?php $DropSortList[] = array( "sort" => array( "orderby" => "date" , "order" => "DESC" ) , "label" => "日付の新しい順" ); ?>
    		<?php $DropSortList[] = array( "sort" => array( "orderby" => "date" , "order" => "ASC" ) , "label" => "日付の古い順" ); ?>
    		<?php $DropSortList[] = array( "sort" => array( "orderby" => "title" , "order" => "DESC" ) , "label" => "タイトル順(わ~あ・Z~A)" ); ?>
    		<?php $DropSortList[] = array( "sort" => array( "orderby" => "title" , "order" => "ASC" ) , "label" => "タイトル順(A~Z・あ~わ)" ); ?>
    		<?php $DropSortList[] = array( "sort" => array( "meta_key" => "ratings_average" , "order" => "DESC" ) , "label" => "評価平均の高い順" ); ?>
    		<?php $DropSortList[] = array( "sort" => array( "meta_key" => "ratings_average" , "order" => "ASC" ) , "label" => "評価平均の低い順" ); ?>
    		<?php $DropSortList[] = array( "sort" => array( "meta_key" => "ratings_score" , "order" => "DESC" ) , "label" => "評価総合の多い順" ); ?>
    		<?php $DropSortList[] = array( "sort" => array( "meta_key" => "ratings_score" , "order" => "ASC" ) , "label" => "評価総合の少ない順" ); ?>
    		<?php $DropSortList[] = array( "sort" => array( "meta_key" => "ratings_users" , "order" => "DESC" ) , "label" => "評価をつけた人数の多い順" ); ?>
    		<?php $DropSortList[] = array( "sort" => array( "meta_key" => "ratings_users" , "order" => "ASC" ) , "label" => "評価をつけた人数の少ない順" ); ?>
    		<?php $DropSortList[] = array( "sort" => array( "orderby" => "comment_count" , "order" => "DESC" ) , "label" => "コメントの多い順" ); ?>
    		<?php $DropSortList[] = array( "sort" => array( "orderby" => "comment_count" , "order" => "ASC" ) , "label" => "コメントの少ない順" ); ?>
    		<?php $DropSortList[] = array( "sort" => array( "meta_key" => "views" , "order" => "DESC" ) , "label" => "アクセス数の多い順" ); ?>
    		<?php $DropSortList[] = array( "sort" => array( "meta_key" => "views" , "order" => "ASC" ) , "label" => "アクセス数の少ない順" ); ?>
    		<select name="original-sort-dropdown" onchange='document.location.href=this.options[this.selectedIndex].value;'>
    			<?php if( empty( $_GET["order"] ) ) : ?>
    				<option value="">ソート順を選択</option>
    			<?php else: ?>
    				<?php $Sorted = array(); ?>
    				<?php foreach($DropSortList as $key => $SortList) : ?>
    					<?php if( !empty( $_GET["meta_key"] ) && !empty( $SortList["sort"]["meta_key"] ) ) : ?>
    						<?php if( $_GET["order"] == $SortList["sort"]["order"] && $_GET["meta_key"] == $SortList["sort"]["meta_key"] ) : ?>
    							<?php $Sorted = $key; ?>
    							<?php break; ?>
    						<?php endif; ?>
    					<?php elseif( !empty( $_GET["orderby"] ) && !empty( $SortList["sort"]["orderby"] ) ) : ?>
    						<?php if( $_GET["order"] == $SortList["sort"]["order"] && $_GET["orderby"] == $SortList["sort"]["orderby"] ) : ?>
    							<?php $Sorted = $key; ?>
    							<?php break; ?>
    						<?php endif; ?>
    					<?php endif; ?>
    				<?php endforeach; ?>
    			<?php endif; ?>
    			<?php if( !empty( $Sorted ) ) : ?>
    				<option value="<?php echo add_query_arg( $DropSortList[$key]["sort"] , $uri ); ?>" selected="selected"><?php echo $DropSortList[$key]["label"]; ?></option>
    				<?php unset( $DropSortList[$key] ); ?>
    			<?php endif; ?>
    			<?php foreach($DropSortList as $key => $SortList) : ?>
    				<option value="<?php echo add_query_arg( $SortList["sort"] , $uri ); ?>"><?php echo $SortList["label"]; ?></option>
    			<?php endforeach; ?>
    		</select>
            </div>   
    
        <?php
        // 何の指定もないときのデフォルトのソート順
        $args = array( 'nopaging' => 1 , 'orderby' => 'date' , 'order' => 'DESC' );
    
        if( is_author() ) {
          $term = get_queried_object();
          if ( $term ) {
    		  $args["author"] = $term->ID;
    	  }
    	}
    
        if( !empty( $_GET["meta_key"] ) ) {
          $args["meta_key"] = strip_tags( $_GET["meta_key"] );
          $args["orderby"] = 'meta_value_num';
          } elseif( !empty( $_GET["orderby"] ) ) {
          $args["orderby"] = strip_tags( $_GET["orderby"] );
          unset( $args["meta_key"] );
    
        }
    
        if( !empty( $_GET["order"] ) ) {
          $args["order"] = strip_tags( $_GET["order"] );
        }
        ?>
    
        <?php query_posts( $args ); ?>

    のようになると思います。

    ちなみに、日付系のアーカイブは、

    <?php $uri = home_url( '/' ); ?>
        <?php if( is_archive() ) : ?>
    		<?php $arc = array( "year" => get_query_var( "year" ) , "month" => get_query_var( "monthnum" ) , "day" => get_query_var( "day" ) ); ?>
          <?php if ( $arc["day"] ) : ?>
            <?php $uri = get_day_link( $arc["year"] , $arc["month"] , $arc["day"] ); ?>
          <?php elseif ( $arc["month"] ) : ?>
            <?php $uri = get_month_link( $arc["year"] , $arc["month"] ); ?>
          <?php elseif ( $arc["year"] ) : ?>
            <?php $uri = get_month_link( $arc["year"] ); ?>
          <?php endif; ?>
        <?php endif; ?>
    
        ドロップダウン形式<br>
    		<?php $DropSortList = array(); ?>
    		<?php $DropSortList[] = array( "sort" => array( "orderby" => "date" , "order" => "DESC" ) , "label" => "日付の新しい順" ); ?>
    		<?php $DropSortList[] = array( "sort" => array( "orderby" => "date" , "order" => "ASC" ) , "label" => "日付の古い順" ); ?>
    		<?php $DropSortList[] = array( "sort" => array( "orderby" => "title" , "order" => "DESC" ) , "label" => "タイトル順(わ~あ・Z~A)" ); ?>
    		<?php $DropSortList[] = array( "sort" => array( "orderby" => "title" , "order" => "ASC" ) , "label" => "タイトル順(A~Z・あ~わ)" ); ?>
    		<?php $DropSortList[] = array( "sort" => array( "meta_key" => "ratings_average" , "order" => "DESC" ) , "label" => "評価平均の高い順" ); ?>
    		<?php $DropSortList[] = array( "sort" => array( "meta_key" => "ratings_average" , "order" => "ASC" ) , "label" => "評価平均の低い順" ); ?>
    		<?php $DropSortList[] = array( "sort" => array( "meta_key" => "ratings_score" , "order" => "DESC" ) , "label" => "評価総合の多い順" ); ?>
    		<?php $DropSortList[] = array( "sort" => array( "meta_key" => "ratings_score" , "order" => "ASC" ) , "label" => "評価総合の少ない順" ); ?>
    		<?php $DropSortList[] = array( "sort" => array( "meta_key" => "ratings_users" , "order" => "DESC" ) , "label" => "評価をつけた人数の多い順" ); ?>
    		<?php $DropSortList[] = array( "sort" => array( "meta_key" => "ratings_users" , "order" => "ASC" ) , "label" => "評価をつけた人数の少ない順" ); ?>
    		<?php $DropSortList[] = array( "sort" => array( "orderby" => "comment_count" , "order" => "DESC" ) , "label" => "コメントの多い順" ); ?>
    		<?php $DropSortList[] = array( "sort" => array( "orderby" => "comment_count" , "order" => "ASC" ) , "label" => "コメントの少ない順" ); ?>
    		<?php $DropSortList[] = array( "sort" => array( "meta_key" => "views" , "order" => "DESC" ) , "label" => "アクセス数の多い順" ); ?>
    		<?php $DropSortList[] = array( "sort" => array( "meta_key" => "views" , "order" => "ASC" ) , "label" => "アクセス数の少ない順" ); ?>
    		<select name="original-sort-dropdown" onchange='document.location.href=this.options[this.selectedIndex].value;'>
    			<?php if( empty( $_GET["order"] ) ) : ?>
    				<option value="">ソート順を選択</option>
    			<?php else: ?>
    				<?php $Sorted = array(); ?>
    				<?php foreach($DropSortList as $key => $SortList) : ?>
    					<?php if( !empty( $_GET["meta_key"] ) && !empty( $SortList["sort"]["meta_key"] ) ) : ?>
    						<?php if( $_GET["order"] == $SortList["sort"]["order"] && $_GET["meta_key"] == $SortList["sort"]["meta_key"] ) : ?>
    							<?php $Sorted = $key; ?>
    							<?php break; ?>
    						<?php endif; ?>
    					<?php elseif( !empty( $_GET["orderby"] ) && !empty( $SortList["sort"]["orderby"] ) ) : ?>
    						<?php if( $_GET["order"] == $SortList["sort"]["order"] && $_GET["orderby"] == $SortList["sort"]["orderby"] ) : ?>
    							<?php $Sorted = $key; ?>
    							<?php break; ?>
    						<?php endif; ?>
    					<?php endif; ?>
    				<?php endforeach; ?>
    			<?php endif; ?>
    			<?php if( !empty( $Sorted ) ) : ?>
    				<option value="<?php echo add_query_arg( $DropSortList[$key]["sort"] , $uri ); ?>" selected="selected"><?php echo $DropSortList[$key]["label"]; ?></option>
    				<?php unset( $DropSortList[$key] ); ?>
    			<?php endif; ?>
    			<?php foreach($DropSortList as $key => $SortList) : ?>
    				<option value="<?php echo add_query_arg( $SortList["sort"] , $uri ); ?>"><?php echo $SortList["label"]; ?></option>
    			<?php endforeach; ?>
    		</select>
            </div>   
    
        <?php
        // 何の指定もないときのデフォルトのソート順
        $args = array( 'nopaging' => 1 , 'orderby' => 'date' , 'order' => 'DESC' );
    
         if ( $arc["day"] ) {
            $args["day"] = $arc["day"];
    	 }
    	 if ( $arc["month"] ) {
            $args["monthnum"] = $arc["month"];
    	 }
    	 if ( $arc["year"] ) {
            $args["year"] = $arc["year"];
    	 }
    
        if( !empty( $_GET["meta_key"] ) ) {
          $args["meta_key"] = strip_tags( $_GET["meta_key"] );
          $args["orderby"] = 'meta_value_num';
          } elseif( !empty( $_GET["orderby"] ) ) {
          $args["orderby"] = strip_tags( $_GET["orderby"] );
          unset( $args["meta_key"] );
    
        }
    
        if( !empty( $_GET["order"] ) ) {
          $args["order"] = strip_tags( $_GET["order"] );
        }
        ?>
    
        <?php query_posts( $args ); ?>

    このようになると思います。

    以上、お試しください。

    トピック投稿者 gakusei

    (@gakusei)

    gqevu6bsizさん

    返答が遅くなってしまい、本当に申し訳ないです。
    すいませんでした。

    ソートについてですが、
    無事希望のものが出来ました。

    解決済みにも関わらず、ここまで教えていただき、
    本当に感謝しています。
    ありがとうございました。

    ぜひ、また他でもご教授いただければ嬉しいです

    gakuteiさん

    無事希望通りの解決ができて良かったですね。

    では、また機会があれば!

    トピック投稿者 gakusei

    (@gakusei)

    gqevu6bsizさん

    たびたび申し訳ありません。

    別のトピックをたてるか悩みましたが、
    トピックを未解決にすることにしました。
    すいません。
    もし、お時間があるようでしたら、ご回答いただけますとありがたいです。

    何度も質問してしまって、非常に申し訳ないです。

    検索フォームで検索するときに、
    <?php wp_dropdown_categories(); ?>を使うことで、カテゴリーを絞って検索できるようにしました。

    ただ、gqevu6bsizさんのコードを使っても、<?php wp_dropdown_categories(); ?>で
    絞って検索した時に、ソートが出来ませんでした。

    検索フォームをカテゴリーで絞った場合でも、
    gqevu6bsizさんのコードでのソートは出来るのでしょうか。

    本当にすいません。
    よろしくお願いします。

    gakusei さん

    いえ、むしろそんなに謝らなくても大丈夫ですよ!

    検索フォームにカテゴリーを絞る際のソートについてですが、
    検索フォームがカテゴリを指定してちゃんと動作している時を前提として書きます。

    解決方法としては、
    検索結果のテンプレート search.php 内の以前のコード部分、
    ドロップダウンリストのURLが挿入される部分の値に、
    検索されたキーワード(search_query)を挿入します。

    カテゴリ指定なし

    <?php $DropSortList[] = array( “sort” => array( “orderby” => “date” , “order” => “DESC” ) , “label” => “日付の新しい順” ); ?>

    カテゴリ指定あり

    <?php $DropSortList[] = array( "sort" => array( "orderby" => "date" , "order" => "DESC" , "s" => get_query_var('s') ) , "label" => "日付の新しい順" ); ?>

    s => get_query_var(s) で検索キーワードを挿入しています。

    以前のドロップダウンリスト全てに適用する場合は、

    <?php $DropSortList[] = array( "sort" => array( "orderby" => "date" , "order" => "DESC" , "s" => get_query_var('s') ) , "label" => "日付の新しい順" ); ?>
    		<?php $DropSortList[] = array( "sort" => array( "orderby" => "date" , "order" => "ASC" , "s" => get_query_var('s') ) , "label" => "日付の古い順" ); ?>
    		<?php $DropSortList[] = array( "sort" => array( "orderby" => "title" , "order" => "DESC" , "s" => get_query_var('s') ) , "label" => "タイトル順(わ~あ・Z~A)" ); ?>
    		<?php $DropSortList[] = array( "sort" => array( "orderby" => "title" , "order" => "ASC" , "s" => get_query_var('s') ) , "label" => "タイトル順(A~Z・あ~わ)" ); ?>
    		<?php $DropSortList[] = array( "sort" => array( "meta_key" => "ratings_average" , "order" => "DESC" , "s" => get_query_var('s') ) , "label" => "評価平均の高い順" ); ?>
    		<?php $DropSortList[] = array( "sort" => array( "meta_key" => "ratings_average" , "order" => "ASC" , "s" => get_query_var('s') ) , "label" => "評価平均の低い順" ); ?>
    		<?php $DropSortList[] = array( "sort" => array( "meta_key" => "ratings_score" , "order" => "DESC" , "s" => get_query_var('s') ) , "label" => "評価総合の多い順" ); ?>
    		<?php $DropSortList[] = array( "sort" => array( "meta_key" => "ratings_score" , "order" => "ASC" , "s" => get_query_var('s') ) , "label" => "評価総合の少ない順" ); ?>
    		<?php $DropSortList[] = array( "sort" => array( "meta_key" => "ratings_users" , "order" => "DESC" , "s" => get_query_var('s') ) , "label" => "評価をつけた人数の多い順" ); ?>
    		<?php $DropSortList[] = array( "sort" => array( "meta_key" => "ratings_users" , "order" => "ASC" , "s" => get_query_var('s') ) , "label" => "評価をつけた人数の少ない順" ); ?>
    		<?php $DropSortList[] = array( "sort" => array( "orderby" => "comment_count" , "order" => "DESC" , "s" => get_query_var('s') ) , "label" => "コメントの多い順" ); ?>
    		<?php $DropSortList[] = array( "sort" => array( "orderby" => "comment_count" , "order" => "ASC" , "s" => get_query_var('s') ) , "label" => "コメントの少ない順" ); ?>
    		<?php $DropSortList[] = array( "sort" => array( "meta_key" => "views" , "order" => "DESC" , "s" => get_query_var('s') ) , "label" => "アクセス数の多い順" ); ?>
    		<?php $DropSortList[] = array( "sort" => array( "meta_key" => "views" , "order" => "ASC" , "s" => get_query_var('s') ) , "label" => "アクセス数の少ない順" ); ?>

    このようになると思います。

    今回のように上手くソートが出来なかった原因としては、
    カテゴリを指定すると、$uriに入れているURLの部分が変わってしまう為、
    ご希望通りのソートが出来なかったと思います。
    (検索結果ページのURLから、カテゴリページのURLへ)

    もし上記のコードで上手く動かないようなら、
    お手数ですがまずはフォームが希望通りの動作をしているか、ご確認ください。

    ※カテゴリ指定+検索キーワード の場合の検索結果のURLの形式に、s と cat の2つが含まれているかどうか。ですね。

    トピック投稿者 gakusei

    (@gakusei)

    gqevu6bsizさん

    本当に申し訳ないです。

    いろいろと考え、試したところ、
    gqevu6bsizさんのコードを用いて、結果的に以下のようなコードにしました。

    (以下のコードは、category.phpに入れました)

    <?php $uri = home_url( '/' ); ?>
        <?php if( is_category() or is_tag() ) : ?>
          <?php $term = get_queried_object(); ?>
          <?php if ( $term ) : ?>
            <?php $uri = get_term_link( $term , $term->taxonomy ); ?><br>
          <?php endif; ?>
        <?php endif; ?>
    
        <div class="sort">
        <select name="original-sort-dropdown" onchange='document.location.href=this.options[this.selectedIndex].value;'>
    
          <?php $Selected = ''; if( !empty( $_GET["orderby"] ) && !empty( $_GET["order"] ) ) : if( $_GET["orderby"] == 'date' && $_GET["order"] == 'DESC' ) : $Selected = 'selected="selected"'; endif; endif; ?>
          <option value="<?php echo add_query_arg( array( 'orderby' => 'date' , 'order' => 'DESC' ), $uri ); ?>" <?php echo $Selected ?>>日付の新しい順</option>
    
          <?php $Selected = ''; if( !empty( $_GET["orderby"] ) && !empty( $_GET["order"] ) ) : if( $_GET["orderby"] == 'date' && $_GET["order"] == 'ASC' ) : $Selected = 'selected="selected"'; endif; endif; ?>
          <option value="<?php echo add_query_arg( array( 'orderby' => 'date' , 'order' => 'ASC' ), $uri ); ?>" <?php echo $Selected ?>>日付の古い順</option>  
    
          <?php $Selected = ''; if( !empty( $_GET["orderby"] ) && !empty( $_GET["order"] ) ) : if( $_GET["orderby"] == 'title' && $_GET["order"] == 'ASC' ) : $Selected = 'selected="selected"'; endif; endif; ?>
          <option value="<?php echo add_query_arg( array( 'orderby' => 'title' , 'order' => 'ASC' ), $uri ); ?>" <?php echo $Selected ?>>タイトル順(あ~わ・A~Z)</option>
    
          <?php $Selected = ''; if( !empty( $_GET["orderby"] ) && !empty( $_GET["order"] ) ) : if( $_GET["orderby"] == 'title' && $_GET["order"] == 'DESC' ) : $Selected = 'selected="selected"'; endif; endif; ?>
          <option value="<?php echo add_query_arg( array( 'orderby' => 'title' , 'order' => 'DESC' ), $uri ); ?>" <?php echo $Selected ?>>タイトル順(わ~あ・Z~A)</option>  
    
          <?php $Selected = ''; if( !empty( $_GET["meta_key"] ) && !empty( $_GET["order"] ) ) : if( $_GET["meta_key"] == 'ratings_average' && $_GET["order"] == 'DESC' ) : $Selected = 'selected="selected"'; endif; endif; ?>
          <option value="<?php echo add_query_arg( array( 'meta_key' => 'ratings_average' , 'order' => 'DESC' ), $uri ); ?>" <?php echo $Selected ?>>評価の高い順(平均)</option>
    
          <?php $Selected = ''; if( !empty( $_GET["meta_key"] ) && !empty( $_GET["order"] ) ) : if( $_GET["meta_key"] == 'ratings_average' && $_GET["order"] == 'ASC' ) : $Selected = 'selected="selected"'; endif; endif; ?>
          <option value="<?php echo add_query_arg( array( 'meta_key' => 'ratings_average' , 'order' => 'ASC' ), $uri ); ?>" <?php echo $Selected ?>>評価の低い順(平均)</option>
    
          <?php $Selected = ''; if( !empty( $_GET["meta_key"] ) && !empty( $_GET["order"] ) ) : if( $_GET["meta_key"] == 'ratings_score' && $_GET["order"] == 'DESC' ) : $Selected = 'selected="selected"'; endif; endif; ?>
          <option value="<?php echo add_query_arg( array( 'meta_key' => 'ratings_score' , 'order' => 'DESC' ), $uri ); ?>" <?php echo $Selected ?>>評価の高い順(総合)</option>
    
          <?php $Selected = ''; if( !empty( $_GET["meta_key"] ) && !empty( $_GET["order"] ) ) : if( $_GET["meta_key"] == 'ratings_score' && $_GET["order"] == 'ASC' ) : $Selected = 'selected="selected"'; endif; endif; ?>
          <option value="<?php echo add_query_arg( array( 'meta_key' => 'ratings_score' , 'order' => 'ASC' ), $uri ); ?>" <?php echo $Selected ?>>評価の低い順(総合)</option>  
    
          <?php $Selected = ''; if( !empty( $_GET["meta_key"] ) && !empty( $_GET["order"] ) ) : if( $_GET["meta_key"] == 'ratings_users' && $_GET["order"] == 'DESC' ) : $Selected = 'selected="selected"'; endif; endif; ?>
          <option value="<?php echo add_query_arg( array( 'meta_key' => 'ratings_users' , 'order' => 'DESC' ), $uri ); ?>" <?php echo $Selected ?>>評価数の多い順</option>
    
          <?php $Selected = ''; if( !empty( $_GET["meta_key"] ) && !empty( $_GET["order"] ) ) : if( $_GET["meta_key"] == 'ratings_users' && $_GET["order"] == 'ASC' ) : $Selected = 'selected="selected"'; endif; endif; ?>
          <option value="<?php echo add_query_arg( array( 'meta_key' => 'ratings_users' , 'order' => 'ASC' ), $uri ); ?>" <?php echo $Selected ?>>評価数の少ない順</option>
    
          <?php $Selected = ''; if( !empty( $_GET["orderby"] ) && !empty( $_GET["order"] ) ) : if( $_GET["orderby"] == 'comment_count' && $_GET["order"] == 'DESC' ) : $Selected = 'selected="selected"'; endif; endif; ?>
          <option value="<?php echo add_query_arg( array( 'orderby' => 'comment_count' , 'order' => 'DESC' ), $uri ); ?>" <?php echo $Selected ?>>コメント数の多い順</option>
    
          <?php $Selected = ''; if( !empty( $_GET["orderby"] ) && !empty( $_GET["order"] ) ) : if( $_GET["orderby"] == 'comment_count' && $_GET["order"] == 'ASC' ) : $Selected = 'selected="selected"'; endif; endif; ?>
          <option value="<?php echo add_query_arg( array( 'orderby' => 'comment_count' , 'order' => 'ASC' ), $uri ); ?>" <?php echo $Selected ?>>コメント数の少ない順</option>
    
          <?php $Selected = ''; if( !empty( $_GET["meta_key"] ) && !empty( $_GET["order"] ) ) : if( $_GET["meta_key"] == 'views' && $_GET["order"] == 'DESC' ) : $Selected = 'selected="selected"'; endif; endif; ?>
          <option value="<?php echo add_query_arg( array( 'meta_key' => 'views' , 'order' => 'DESC' ), $uri ); ?>" <?php echo $Selected ?>>閲覧数の多い順</option>
    
          <?php $Selected = ''; if( !empty( $_GET["meta_key"] ) && !empty( $_GET["order"] ) ) : if( $_GET["meta_key"] == 'views' && $_GET["order"] == 'ASC' ) : $Selected = 'selected="selected"'; endif; endif; ?>
          <option value="<?php echo add_query_arg( array( 'meta_key' => 'views' , 'order' => 'ASC' ), $uri ); ?>" <?php echo $Selected ?>>閲覧数の少ない順</option>
    
        </select>
            </div>   
    
        <?php
        // 何の指定もないときのデフォルトのソート順
        $args = array( 'nopaging' => 1 , 'orderby' => 'date' , 'order' => 'DESC' );
    
        if( is_category() ) {
          $args["cat"] = get_query_var( "cat" );
        } elseif( is_tag() ) {
          $args["tag"] = get_query_var( "tag" );
        }
    
        if( !empty( $_GET["meta_key"] ) ) {
          $args["meta_key"] = strip_tags( $_GET["meta_key"] );
          $args["orderby"] = 'meta_value_num';
          } elseif( !empty( $_GET["orderby"] ) ) {
          $args["orderby"] = strip_tags( $_GET["orderby"] );
          unset( $args["meta_key"] );
    
        }
    
        if( !empty( $_GET["order"] ) ) {
          $args["order"] = strip_tags( $_GET["order"] );
        }
        ?>
    
        <?php query_posts( $args ); ?>

    このコードだと、一通りソートができ、よいのではないかと思っていました。

    ただ、上記のコードだと、筆者ごとのソートと、検索結果のソートをどのように
    作ればいいかがいまいちよくわかりませんでした。
    (カテゴリーを絞った場合も、同様にわかりませんでした。)

    以前から何度も教えていただいているのにも関わらず、
    勝手な対応で、非常に申し訳ないと思っていますが、
    上記のコードの工夫で、筆者や検索の場合などでも、
    ソートが出来るようでしたら、教えていただきたいです。

    また、先ほど気づいたのですが、
    gqevu6bsizさんが教えてくださいましたコードも、
    私が勝手に変えてしまった上記のコードも、
    どちらのコードを用いても、
    「管理画面」の「設定」にある「表示設定」から表示する記事の数を
    制限しても機能せず、全ての記事が表示される、という状況になってしまいます。

    いろいろと調べてみると、
    どうも、query_postsが影響しているみたいです。

    この部分に対しても、gqevu6bsizさんのコードで
    なにか工夫が出来ることがありましたら、教えていただきたいです。

    長くなってしまうと同時に、一方的に話を進めてしまって
    非常に申し訳ないと思っています。
    お手数ですが、もしよろしければ、
    ご回答をよろしくお願いします。

    gakuseiさん

    すみません、文章が結構長いですがご了承ください。。

    gakuseiさんの現在使用しているコードを応用する場合、
    まず「筆者(投稿者)ごとのソート」ですが、

    テンプレートタグ/query posts
    を見ると、 orderbyahthor を指定するようです。

    具体的なドロップダウンのコードは、

    <?php $Selected = ''; if( !empty( $_GET["orderby"] ) && !empty( $_GET["order"] ) ) : if( $_GET["orderby"] == 'author' && $_GET["order"] == 'ASC' ) : $Selected = 'selected="selected"'; endif; endif; ?>
          <option value="<?php echo add_query_arg( array( 'orderby' => 'author' , 'order' => 'ASC' ), $uri ); ?>" <?php echo $Selected ?>>投稿者順(A~Z・あ~わ)</option>
    
          <?php $Selected = ''; if( !empty( $_GET["orderby"] ) && !empty( $_GET["order"] ) ) : if( $_GET["orderby"] == 'author' && $_GET["order"] == 'DESC' ) : $Selected = 'selected="selected"'; endif; endif; ?>
          <option value="<?php echo add_query_arg( array( 'orderby' => 'author' , 'order' => 'DESC' ), $uri ); ?>" <?php echo $Selected ?>>投稿者順(わ~あ・Z~A)</option>

    このようになると思います。

    次に検索結果のソートについてですが、
    検索結果ページ search.php (カテゴリを指定しても) のテンプレートに同じように
    ドロップダウンリスト、query_posts()の記述と、
    http://ja.forums.wordpress.org/topic/13822/page/2?replies=37#post-49705
    で記載した、検索キーワードの値をURLとquery_postsの両方にセットすれば、できると思います。

    具体的なコードは、

    <?php $uri = home_url( '/' ); ?>
        <?php if( is_category() or is_tag() ) : ?>
          <?php $term = get_queried_object(); ?>
          <?php if ( $term ) : ?>
            <?php $uri = get_term_link( $term , $term->taxonomy ); ?>
          <?php endif; ?>
        <?php endif; ?>
    
        <div class="sort">
        <select name="original-sort-dropdown" onchange='document.location.href=this.options[this.selectedIndex].value;'>
    
          <?php $Selected = ''; if( !empty( $_GET["orderby"] ) && !empty( $_GET["order"] ) ) : if( $_GET["orderby"] == 'date' && $_GET["order"] == 'DESC' ) : $Selected = 'selected="selected"'; endif; endif; ?>
          <option value="<?php echo add_query_arg( array( 'orderby' => 'date' , 'order' => 'DESC' , "s" => get_query_var('s') ), $uri ); ?>" <?php echo $Selected ?>>日付の新しい順</option>
    
          <?php $Selected = ''; if( !empty( $_GET["orderby"] ) && !empty( $_GET["order"] ) ) : if( $_GET["orderby"] == 'date' && $_GET["order"] == 'ASC' ) : $Selected = 'selected="selected"'; endif; endif; ?>
          <option value="<?php echo add_query_arg( array( 'orderby' => 'date' , 'order' => 'ASC' , "s" => get_query_var('s') ), $uri ); ?>" <?php echo $Selected ?>>日付の古い順</option>  
    
          <?php $Selected = ''; if( !empty( $_GET["orderby"] ) && !empty( $_GET["order"] ) ) : if( $_GET["orderby"] == 'title' && $_GET["order"] == 'ASC' ) : $Selected = 'selected="selected"'; endif; endif; ?>
          <option value="<?php echo add_query_arg( array( 'orderby' => 'title' , 'order' => 'ASC' , "s" => get_query_var('s') ), $uri ); ?>" <?php echo $Selected ?>>タイトル順(あ~わ・A~Z)</option>
    
          <?php $Selected = ''; if( !empty( $_GET["orderby"] ) && !empty( $_GET["order"] ) ) : if( $_GET["orderby"] == 'title' && $_GET["order"] == 'DESC' ) : $Selected = 'selected="selected"'; endif; endif; ?>
          <option value="<?php echo add_query_arg( array( 'orderby' => 'title' , 'order' => 'DESC' , "s" => get_query_var('s') ), $uri ); ?>" <?php echo $Selected ?>>タイトル順(わ~あ・Z~A)</option>  
    
          <?php $Selected = ''; if( !empty( $_GET["meta_key"] ) && !empty( $_GET["order"] ) ) : if( $_GET["meta_key"] == 'ratings_average' && $_GET["order"] == 'DESC' ) : $Selected = 'selected="selected"'; endif; endif; ?>
          <option value="<?php echo add_query_arg( array( 'meta_key' => 'ratings_average' , 'order' => 'DESC' , "s" => get_query_var('s') ), $uri ); ?>" <?php echo $Selected ?>>評価の高い順(平均)</option>
    
          <?php $Selected = ''; if( !empty( $_GET["meta_key"] ) && !empty( $_GET["order"] ) ) : if( $_GET["meta_key"] == 'ratings_average' && $_GET["order"] == 'ASC' ) : $Selected = 'selected="selected"'; endif; endif; ?>
          <option value="<?php echo add_query_arg( array( 'meta_key' => 'ratings_average' , 'order' => 'ASC' , "s" => get_query_var('s') ), $uri ); ?>" <?php echo $Selected ?>>評価の低い順(平均)</option>
    
          <?php $Selected = ''; if( !empty( $_GET["meta_key"] ) && !empty( $_GET["order"] ) ) : if( $_GET["meta_key"] == 'ratings_score' && $_GET["order"] == 'DESC' ) : $Selected = 'selected="selected"'; endif; endif; ?>
          <option value="<?php echo add_query_arg( array( 'meta_key' => 'ratings_score' , 'order' => 'DESC' , "s" => get_query_var('s') ), $uri ); ?>" <?php echo $Selected ?>>評価の高い順(総合)</option>
    
          <?php $Selected = ''; if( !empty( $_GET["meta_key"] ) && !empty( $_GET["order"] ) ) : if( $_GET["meta_key"] == 'ratings_score' && $_GET["order"] == 'ASC' ) : $Selected = 'selected="selected"'; endif; endif; ?>
          <option value="<?php echo add_query_arg( array( 'meta_key' => 'ratings_score' , 'order' => 'ASC' , "s" => get_query_var('s') ), $uri ); ?>" <?php echo $Selected ?>>評価の低い順(総合)</option>  
    
          <?php $Selected = ''; if( !empty( $_GET["meta_key"] ) && !empty( $_GET["order"] ) ) : if( $_GET["meta_key"] == 'ratings_users' && $_GET["order"] == 'DESC' ) : $Selected = 'selected="selected"'; endif; endif; ?>
          <option value="<?php echo add_query_arg( array( 'meta_key' => 'ratings_users' , 'order' => 'DESC' , "s" => get_query_var('s') ), $uri ); ?>" <?php echo $Selected ?>>評価数の多い順</option>
    
          <?php $Selected = ''; if( !empty( $_GET["meta_key"] ) && !empty( $_GET["order"] ) ) : if( $_GET["meta_key"] == 'ratings_users' && $_GET["order"] == 'ASC' ) : $Selected = 'selected="selected"'; endif; endif; ?>
          <option value="<?php echo add_query_arg( array( 'meta_key' => 'ratings_users' , 'order' => 'ASC' , "s" => get_query_var('s') ), $uri ); ?>" <?php echo $Selected ?>>評価数の少ない順</option>
    
          <?php $Selected = ''; if( !empty( $_GET["orderby"] ) && !empty( $_GET["order"] ) ) : if( $_GET["orderby"] == 'comment_count' && $_GET["order"] == 'DESC' ) : $Selected = 'selected="selected"'; endif; endif; ?>
          <option value="<?php echo add_query_arg( array( 'orderby' => 'comment_count' , 'order' => 'DESC' , "s" => get_query_var('s') ), $uri ); ?>" <?php echo $Selected ?>>コメント数の多い順</option>
    
          <?php $Selected = ''; if( !empty( $_GET["orderby"] ) && !empty( $_GET["order"] ) ) : if( $_GET["orderby"] == 'comment_count' && $_GET["order"] == 'ASC' ) : $Selected = 'selected="selected"'; endif; endif; ?>
          <option value="<?php echo add_query_arg( array( 'orderby' => 'comment_count' , 'order' => 'ASC' , "s" => get_query_var('s') ), $uri ); ?>" <?php echo $Selected ?>>コメント数の少ない順</option>
    
          <?php $Selected = ''; if( !empty( $_GET["meta_key"] ) && !empty( $_GET["order"] ) ) : if( $_GET["meta_key"] == 'views' && $_GET["order"] == 'DESC' ) : $Selected = 'selected="selected"'; endif; endif; ?>
          <option value="<?php echo add_query_arg( array( 'meta_key' => 'views' , 'order' => 'DESC' , "s" => get_query_var('s') ), $uri ); ?>" <?php echo $Selected ?>>閲覧数の多い順</option>
    
          <?php $Selected = ''; if( !empty( $_GET["meta_key"] ) && !empty( $_GET["order"] ) ) : if( $_GET["meta_key"] == 'views' && $_GET["order"] == 'ASC' ) : $Selected = 'selected="selected"'; endif; endif; ?>
          <option value="<?php echo add_query_arg( array( 'meta_key' => 'views' , 'order' => 'ASC' , "s" => get_query_var('s') ), $uri ); ?>" <?php echo $Selected ?>>閲覧数の少ない順</option>
    
          <?php $Selected = ''; if( !empty( $_GET["orderby"] ) && !empty( $_GET["order"] ) ) : if( $_GET["orderby"] == 'author' && $_GET["order"] == 'ASC' ) : $Selected = 'selected="selected"'; endif; endif; ?>
          <option value="<?php echo add_query_arg( array( 'orderby' => 'author' , 'order' => 'ASC' , "s" => get_query_var('s') ), $uri ); ?>" <?php echo $Selected ?>>投稿者順(A~Z・あ~わ)</option>
    
          <?php $Selected = ''; if( !empty( $_GET["orderby"] ) && !empty( $_GET["order"] ) ) : if( $_GET["orderby"] == 'author' && $_GET["order"] == 'DESC' ) : $Selected = 'selected="selected"'; endif; endif; ?>
          <option value="<?php echo add_query_arg( array( 'orderby' => 'author' , 'order' => 'DESC' , "s" => get_query_var('s') ), $uri ); ?>" <?php echo $Selected ?>>投稿者順(わ~あ・Z~A)</option>  
    
        </select>
            </div>   
    
        <?php
        // 何の指定もないときのデフォルトのソート順
        $args = array( 'nopaging' => 1 , 'orderby' => 'date' , 'order' => 'DESC' );
    
        if( is_category() ) {
          $args["cat"] = get_query_var( "cat" );
        } elseif( is_tag() ) {
          $args["tag"] = get_query_var( "tag" );
        }
        $args["s"] = get_query_var( "s" );
    
        if( !empty( $_GET["meta_key"] ) ) {
          $args["meta_key"] = strip_tags( $_GET["meta_key"] );
          $args["orderby"] = 'meta_value_num';
          } elseif( !empty( $_GET["orderby"] ) ) {
          $args["orderby"] = strip_tags( $_GET["orderby"] );
          unset( $args["meta_key"] );
    
        }
    
        if( !empty( $_GET["order"] ) ) {
          $args["order"] = strip_tags( $_GET["order"] );
        }
        ?>
    
        <?php query_posts( $args ); ?>

    このようになると思います。

    最後に、

    どちらのコードを用いても、
    「管理画面」の「設定」にある「表示設定」から表示する記事の数を
    制限しても機能せず、全ての記事が表示される、という状況になってしまいます。

    についてですが、このトピックを作成した当初にgakuseiさんが使用していたコード
    http://ja.forums.wordpress.org/topic/13822?replies=37#post-48511
    にて、posts_per_page の値を -1 としていたので、
    gakuseiさんのご希望が、該当する”全ての記事”を対象にしているのかと思ってました。

    なので、管理画面→表示設定→「1ページに表示する最大投稿数」の値通りの表示をしたい場合は、query_postsへセットしている配列 $args に、この値を挿入します。

    ※現在使用しているコードでは、デフォルトのソート順のところで指定しています。

    // 何の指定もないときのデフォルトのソート順
    $args = array( ‘nopaging’ => 1 , ‘orderby’ => ‘date’ , ‘order’ => ‘DESC’ );

    詳しい説明は、ページ送り引数を参照してください。

    値を挿入する方法としては、

    // 何の指定もないときのデフォルトのソート順
    	$paged = get_query_var( 'paged' );
    	if( empty( $paged ) ) {
    		$paged = 0;
    	}
        $args = array( 'posts_per_page' => get_option( 'posts_per_page' ) , 'orderby' => 'date' , 'order' => 'DESC' , 'paged' => $paged );

    このようになると思います。
    (category.phpや等にも適用する場合は、同じようにデフォルトの設定部分を変更してください。)

    あと別件ですが、ページ送り「←過去の投稿」や「新しい投稿→」が表示されない場合は、
    twentytwelveテーマなら、記事ループ終了直後あたりendwhileに、

    <?php twentytwelve_content_nav( 'nav-below' ); ?>

    を入れてみてください。

    以上になります。

    トピック投稿者 gakusei

    (@gakusei)

    gqevu6bsizさん

    表示する記事の数については、
    gqevu6bsizさんが提示してくださいましたコードの挿入によって、
    出来ました。

    また、別件も無事機能しました。

    ただ、検索結果については、ソートが出来ませんでした。
    gqevu6bsizさんが作成してくださいましたコードを
    そのまま貼り付けてみましたが、カテゴリーを絞り込む、絞り込まないに関わらず
    ソートが出来ませんでした。

    なにか他のコードが邪魔をしてしまう、ということがあるのでしょうか。

    gakuseiさん

    なにか他のコードが邪魔をしてしまう、ということがあるのでしょうか。

    ソートが出来ない。という事だけでは他のコードの影響なのかどうかは判断しにくいので、
    原因を探すために以下の点についてご回答をお願いします。

    • $uriのデバッグ
    • $argsデバッグ
    • キーワードを入力して検索した際のURL
    • 検索結果ページで、何でもいいのでドロップダウンリストからソート順を選択後のURL
    • 現状ではどのような並び準で記事の一覧が表示されていますか?(日付の新しい順ですか?)

    デバッグ値についてですが、
    $uriは、

    <div class=”sort”>

    直前<?php echo $uri; ?>等で。

    $argsは、

    <?php query_posts( $args ); ?>

    直後<pre><?php print_r($args); ?></pre>等で。

    URLについては私の場合だと、

    http://localhost/wordpress/?s=キーワード&cat=1
    (カテゴリ指定あり)
    http://localhost/wordpress/category/未分類/?meta_key=ratings_users&order=ASC&s=キーワード
    (カテゴリ指定あり、評価数の少ない順ソート)

    このようになります。

    それでは、ご回答宜しくお願いします。

    トピック投稿者 gakusei

    (@gakusei)

    gqevu6bsizさん

    以下のような表記でよいのかわかりませんが、
    このようではないかと思い、記載しました

    ・$uriについて(<?php echo $uri; ?>を入れました)
      =http://localhost/wordpress/と表示されました

    ・$argsについて
     →

    <?php print_r($args); ?>

    を入れたところ、
     
      Array
      (
    [nopaging] => 1
    [orderby] => date
    [order] => DESC
    [s] => 投稿
      )

      と表示されました

    ・キーワードを検索したときのURLについて
     →カテゴリーを絞らなかった場合
      =http://localhost/wordpress/?s=キーワード&cat=0
      
      カテゴリを絞った場合
      =http://localhost/wordpress/?s=キーワード&cat=5

    と表示されました

    ・ソート順を選択後のURLについて
     (コメント数の多い順でソートしました)
     →カテゴリーを絞らなかった場合
      =http://localhost/wordpress/?orderby=comment_count&order=DESC&s=キーワード

     →カテゴリーを絞った場合
      =/category/カテゴリー名/?orderby=comment_count&order=DESC&s=キーワード

     と表示されました

    ・現状について
     →現状は、日付の新しい順で記事の一覧が表示されています

    以上が情報となるのですが、大丈夫でしょうか
    よろしくお願いします

    gakuseiさん

    情報ありがとうございます。
    URLは問題なく動作しているようにみえます。

    なので、$argsに入る値かもしれません。

    試しに、

    • キーワード検索のみ
    • キーワード検索+コメント数の多い順ソート+カテゴリ指定

    での、両方の $args に入る値を比較してみて欲しいです。
    ※ orderby に comment_count、order に DESC が指定されているかどうか等。

    もし分からないようでしたら、両方の場合での
    <?php print_r($args); ?>
    で表示される

    Array
      (
    [nopaging] => 1
    [orderby] => date
    [order] => DESC
    [s] => 投稿
      )

    このようなものを記載して欲しいです。

    また、上記とは別の問題かもしれないので、これも試しとしてですが、
    私の search.php のコード全て記載しますので、
    gakusei さんのsearch.phpと変更して、動作確認をしてみてください。

    ※テーマは twentytwelve を想定しています。

    <?php
    /**
     * The template for displaying Search Results pages.
     *
     * @package WordPress
     * @subpackage Twenty_Twelve
     * @since Twenty Twelve 1.0
     */
    
    get_header(); ?>
    
    	<?php $uri = home_url( '/' ); ?>
        <?php if( is_category() or is_tag() ) : ?>
          <?php $term = get_queried_object(); ?>
          <?php if ( $term ) : ?>
            <?php $uri = get_term_link( $term , $term->taxonomy ); ?>
          <?php endif; ?>
        <?php endif; ?>
    
    	<?php echo $uri; ?>
    
        <div class="sort">
        <select name="original-sort-dropdown" onchange='document.location.href=this.options[this.selectedIndex].value;'>
    
          <?php $Selected = ''; if( !empty( $_GET["orderby"] ) && !empty( $_GET["order"] ) ) : if( $_GET["orderby"] == 'date' && $_GET["order"] == 'DESC' ) : $Selected = 'selected="selected"'; endif; endif; ?>
          <option value="<?php echo add_query_arg( array( 'orderby' => 'date' , 'order' => 'DESC' , "s" => get_query_var('s') ), $uri ); ?>" <?php echo $Selected ?>>日付の新しい順</option>
    
          <?php $Selected = ''; if( !empty( $_GET["orderby"] ) && !empty( $_GET["order"] ) ) : if( $_GET["orderby"] == 'date' && $_GET["order"] == 'ASC' ) : $Selected = 'selected="selected"'; endif; endif; ?>
          <option value="<?php echo add_query_arg( array( 'orderby' => 'date' , 'order' => 'ASC' , "s" => get_query_var('s') ), $uri ); ?>" <?php echo $Selected ?>>日付の古い順</option>  
    
          <?php $Selected = ''; if( !empty( $_GET["orderby"] ) && !empty( $_GET["order"] ) ) : if( $_GET["orderby"] == 'title' && $_GET["order"] == 'ASC' ) : $Selected = 'selected="selected"'; endif; endif; ?>
          <option value="<?php echo add_query_arg( array( 'orderby' => 'title' , 'order' => 'ASC' , "s" => get_query_var('s') ), $uri ); ?>" <?php echo $Selected ?>>タイトル順(あ~わ・A~Z)</option>
    
          <?php $Selected = ''; if( !empty( $_GET["orderby"] ) && !empty( $_GET["order"] ) ) : if( $_GET["orderby"] == 'title' && $_GET["order"] == 'DESC' ) : $Selected = 'selected="selected"'; endif; endif; ?>
          <option value="<?php echo add_query_arg( array( 'orderby' => 'title' , 'order' => 'DESC' , "s" => get_query_var('s') ), $uri ); ?>" <?php echo $Selected ?>>タイトル順(わ~あ・Z~A)</option>  
    
          <?php $Selected = ''; if( !empty( $_GET["meta_key"] ) && !empty( $_GET["order"] ) ) : if( $_GET["meta_key"] == 'ratings_average' && $_GET["order"] == 'DESC' ) : $Selected = 'selected="selected"'; endif; endif; ?>
          <option value="<?php echo add_query_arg( array( 'meta_key' => 'ratings_average' , 'order' => 'DESC' , "s" => get_query_var('s') ), $uri ); ?>" <?php echo $Selected ?>>評価の高い順(平均)</option>
    
          <?php $Selected = ''; if( !empty( $_GET["meta_key"] ) && !empty( $_GET["order"] ) ) : if( $_GET["meta_key"] == 'ratings_average' && $_GET["order"] == 'ASC' ) : $Selected = 'selected="selected"'; endif; endif; ?>
          <option value="<?php echo add_query_arg( array( 'meta_key' => 'ratings_average' , 'order' => 'ASC' , "s" => get_query_var('s') ), $uri ); ?>" <?php echo $Selected ?>>評価の低い順(平均)</option>
    
          <?php $Selected = ''; if( !empty( $_GET["meta_key"] ) && !empty( $_GET["order"] ) ) : if( $_GET["meta_key"] == 'ratings_score' && $_GET["order"] == 'DESC' ) : $Selected = 'selected="selected"'; endif; endif; ?>
          <option value="<?php echo add_query_arg( array( 'meta_key' => 'ratings_score' , 'order' => 'DESC' , "s" => get_query_var('s') ), $uri ); ?>" <?php echo $Selected ?>>評価の高い順(総合)</option>
    
          <?php $Selected = ''; if( !empty( $_GET["meta_key"] ) && !empty( $_GET["order"] ) ) : if( $_GET["meta_key"] == 'ratings_score' && $_GET["order"] == 'ASC' ) : $Selected = 'selected="selected"'; endif; endif; ?>
          <option value="<?php echo add_query_arg( array( 'meta_key' => 'ratings_score' , 'order' => 'ASC' , "s" => get_query_var('s') ), $uri ); ?>" <?php echo $Selected ?>>評価の低い順(総合)</option>  
    
          <?php $Selected = ''; if( !empty( $_GET["meta_key"] ) && !empty( $_GET["order"] ) ) : if( $_GET["meta_key"] == 'ratings_users' && $_GET["order"] == 'DESC' ) : $Selected = 'selected="selected"'; endif; endif; ?>
          <option value="<?php echo add_query_arg( array( 'meta_key' => 'ratings_users' , 'order' => 'DESC' , "s" => get_query_var('s') ), $uri ); ?>" <?php echo $Selected ?>>評価数の多い順</option>
    
          <?php $Selected = ''; if( !empty( $_GET["meta_key"] ) && !empty( $_GET["order"] ) ) : if( $_GET["meta_key"] == 'ratings_users' && $_GET["order"] == 'ASC' ) : $Selected = 'selected="selected"'; endif; endif; ?>
          <option value="<?php echo add_query_arg( array( 'meta_key' => 'ratings_users' , 'order' => 'ASC' , "s" => get_query_var('s') ), $uri ); ?>" <?php echo $Selected ?>>評価数の少ない順</option>
    
          <?php $Selected = ''; if( !empty( $_GET["orderby"] ) && !empty( $_GET["order"] ) ) : if( $_GET["orderby"] == 'comment_count' && $_GET["order"] == 'DESC' ) : $Selected = 'selected="selected"'; endif; endif; ?>
          <option value="<?php echo add_query_arg( array( 'orderby' => 'comment_count' , 'order' => 'DESC' , "s" => get_query_var('s') ), $uri ); ?>" <?php echo $Selected ?>>コメント数の多い順</option>
    
          <?php $Selected = ''; if( !empty( $_GET["orderby"] ) && !empty( $_GET["order"] ) ) : if( $_GET["orderby"] == 'comment_count' && $_GET["order"] == 'ASC' ) : $Selected = 'selected="selected"'; endif; endif; ?>
          <option value="<?php echo add_query_arg( array( 'orderby' => 'comment_count' , 'order' => 'ASC' , "s" => get_query_var('s') ), $uri ); ?>" <?php echo $Selected ?>>コメント数の少ない順</option>
    
          <?php $Selected = ''; if( !empty( $_GET["meta_key"] ) && !empty( $_GET["order"] ) ) : if( $_GET["meta_key"] == 'views' && $_GET["order"] == 'DESC' ) : $Selected = 'selected="selected"'; endif; endif; ?>
          <option value="<?php echo add_query_arg( array( 'meta_key' => 'views' , 'order' => 'DESC' , "s" => get_query_var('s') ), $uri ); ?>" <?php echo $Selected ?>>閲覧数の多い順</option>
    
          <?php $Selected = ''; if( !empty( $_GET["meta_key"] ) && !empty( $_GET["order"] ) ) : if( $_GET["meta_key"] == 'views' && $_GET["order"] == 'ASC' ) : $Selected = 'selected="selected"'; endif; endif; ?>
          <option value="<?php echo add_query_arg( array( 'meta_key' => 'views' , 'order' => 'ASC' , "s" => get_query_var('s') ), $uri ); ?>" <?php echo $Selected ?>>閲覧数の少ない順</option>
    
          <?php $Selected = ''; if( !empty( $_GET["orderby"] ) && !empty( $_GET["order"] ) ) : if( $_GET["orderby"] == 'author' && $_GET["order"] == 'ASC' ) : $Selected = 'selected="selected"'; endif; endif; ?>
          <option value="<?php echo add_query_arg( array( 'orderby' => 'author' , 'order' => 'ASC' , "s" => get_query_var('s') ), $uri ); ?>" <?php echo $Selected ?>>投稿者順(A~Z・あ~わ)</option>
    
          <?php $Selected = ''; if( !empty( $_GET["orderby"] ) && !empty( $_GET["order"] ) ) : if( $_GET["orderby"] == 'author' && $_GET["order"] == 'DESC' ) : $Selected = 'selected="selected"'; endif; endif; ?>
          <option value="<?php echo add_query_arg( array( 'orderby' => 'author' , 'order' => 'DESC' , "s" => get_query_var('s') ), $uri ); ?>" <?php echo $Selected ?>>投稿者順(わ~あ・Z~A)</option>  
    
        </select>
            </div>   
    
        <?php
        // 何の指定もないときのデフォルトのソート順
    	$paged = get_query_var( 'paged' );
    	if( empty( $paged ) ) {
    		$paged = 0;
    	}
        $args = array( 'posts_per_page' => get_option('posts_per_page') , 'orderby' => 'date' , 'order' => 'DESC' , 'paged' => $paged );
    
        if( is_category() ) {
          $args["cat"] = get_query_var( "cat" );
        } elseif( is_tag() ) {
          $args["tag"] = get_query_var( "tag" );
        }
    	$args["s"] = get_query_var( "s" );
    
        if( !empty( $_GET["meta_key"] ) ) {
          $args["meta_key"] = strip_tags( $_GET["meta_key"] );
          $args["orderby"] = 'meta_value_num';
          } elseif( !empty( $_GET["orderby"] ) ) {
          $args["orderby"] = strip_tags( $_GET["orderby"] );
          unset( $args["meta_key"] );
    
        }
    
        if( !empty( $_GET["order"] ) ) {
          $args["order"] = strip_tags( $_GET["order"] );
        }
        ?>
    
        <?php query_posts( $args ); ?>
    	<pre><?php print_r($args); ?></pre>
    
    	<section id="primary" class="site-content">
    		<div id="content" role="main">
    
    		<?php if ( have_posts() ) : ?>
    
    			<header class="page-header">
    				<h1 class="page-title"><?php printf( __( 'Search Results for: %s', 'twentytwelve' ), '<span>' . get_search_query() . '</span>' ); ?></h1>
    			</header>
    
    			<?php twentytwelve_content_nav( 'nav-above' ); ?>
    
    			<?php /* Start the Loop */ ?>
    			<?php while ( have_posts() ) : the_post(); ?>
    				<?php get_template_part( 'content', get_post_format() ); ?>
    			<?php endwhile; ?>
    
    			<?php twentytwelve_content_nav( 'nav-below' ); ?>
    
    		<?php else : ?>
    
    			<article id="post-0" class="post no-results not-found">
    				<header class="entry-header">
    					<h1 class="entry-title"><?php _e( 'Nothing Found', 'twentytwelve' ); ?></h1>
    				</header>
    
    				<div class="entry-content">
    					<p><?php _e( 'Sorry, but nothing matched your search criteria. Please try again with some different keywords.', 'twentytwelve' ); ?></p>
    					<?php get_search_form(); ?>
    				</div><!-- .entry-content -->
    			</article><!-- #post-0 -->
    
    		<?php endif; ?>
    
    		</div><!-- #content -->
    	</section><!-- #primary -->
    
    <?php get_sidebar(); ?>
    <?php get_footer(); ?>

    ※nopagingの部分は変更済みです。

    少し面倒に感じるかもしれませんが、よろしくお願いします。

    トピック投稿者 gakusei

    (@gakusei)

    gqevu6bsizさん

    情報を記載しておきます

    <?php print_r($args); ?>について

    ・キーワード検索のみの場合
     →Array
      (
    [nopaging] => 1
    [orderby] => date
    [order] => DESC
    [s] => 投稿
      )

    ・キーワード検索+コメント数の多い順ソート+カテゴリ指定の場合
     →Array
      (
    [nopaging] => 1
    [orderby] => comment_count
    [order] => DESC
    [cat] => 5
    [s] => キーワード名
      )

    また、gqevu6bsizさんの
    search.phpと入れ替えた結果、以下のようなことが分かりました
    →・「Search Results for: 投稿」より下が表示されませんでした
     ・フッターも表示されませんでした
     ・記事の一覧が表示されませんでした
     ・ヘッダーから「

    <?php print_r($args); ?>

    」までは
      普通に表示されました

    以下のような部分が変更点かと思います。

    よろしくお願いします

    gakuseiさん

    お手数お掛けしました。情報ありがとうございます。
    $args への値をみる限り、コードは合っているように思えますね。

    他の原因・解決方法を考えると、あとは

    • 使用しているプラグインを全て停止して動作確認
    • 使用しているテーマ内テンプレートの問題 (search.php内で二重にquery_postsをしていないか等)
      デフォルトのテーマに一度戻して、そこでもういちどsearch.phpの動作確認

    が考えられますので、一度試してみてください。

    デフォルトのテンプレート(twentytwelve)に戻して、希望通りの動作をするようなら、使用中・カスタマイズ中のテンプレート内の問題、デフォルトのテンプレートでも動かないようならコードの問題、と問題を切り分ける事ができると思います。

    →・「Search Results for: 投稿」より下が表示されませんでした
     ・フッターも表示されませんでした
     ・記事の一覧が表示されませんでした

    この部分については gakusei さんのテンプレートがどのようなものかが分からないのでなんともいえません。

    トピック投稿者 gakusei

    (@gakusei)

    gqevu6bsizさん

    検索結果のソートが動かない理由について、
    原因が分かりました。

    なぜ、このようなコードを挿入したのか覚えていないのですが、
    検索して記事が見つからなかったときに
    文章を表示するために、以下のようなコードを挿入していました。

    <?php query_posts('posts_per_page=-1&order=DESC&' . $query_string); ?>
    <?php if(have_posts()): while(have_posts()):
    the_post(); ?>
            <?php endwhile; else: ?>
      <p>一致する記事は見つかりませんでした</p>
      <?php endif; ?>

    どうも、このコードとぶつかっていたみたいです。

    ただ、このコードが使えないとなると、
    今回のようなソートのためのコードを用いた時に、
    記事が無かった時の場合を表示する方法というものは
    あるのでしょうか。

    追加的な質問で申し訳ありませんが、
    よろしかったら、ご回答いただけますとありがたいです。

    gakuseiさん

    そうだったんですね。
    原因らしきものがはっきりして良かったです!

    ただ、このコードが使えないとなると、
    今回のようなソートのためのコードを用いた時に、
    記事が無かった時の場合を表示する方法というものは
    あるのでしょうか。

    あります。
    基本的にはこんな感じです。

    <?php if ( have_posts() ) : ?>
    
      ココに書いた内容は、記事が見つかった場合の内容
      記事の分だけ while 等
    
    <?php else : ?>
    
      ココに書いた内容は、記事が見つからなかった場合の内容
    
    <?php endif; ?>

    The Loop

    gakuseiさんご自身のコードをじっくり見て、該当する部分を見つけてください。

    記事が無かった時の場合を表示する方法

    該当する箇所が分かれば簡単にできます。

    じっくり見ても分からないようなら、テンプレートを出来る限り丸々記載したほうが、
    回答もいただきやすくなると思います。

    それと、

    <?php query_posts(‘posts_per_page=-1&order=DESC&’ . $query_string); ?>
    <?php if(have_posts()): while(have_posts()):
    the_post(); ?>
    <?php endwhile; else: ?>
    <p>一致する記事は見つかりませんでした</p>
    <?php endif; ?>

    もしこのコードのままを記載しているなら、削除したほうがいいと思います。
    ※query_postsを二重に行っている為と、記事のループ部分に何も処理がない為。

15件の返信を表示中 - 31 - 45件目 (全48件中)
  • トピック「記事一覧の時のソートができません」には新たに返信することはできません。