M
M
mix123452018-08-25 16:57:01
WordPress
mix12345, 2018-08-25 16:57:01

How to filter by authors and posts by wordpress authors?

there is such a code

if (isset($_POST['susr']))
  $susr = $_POST['susr'];
else
  $susr = 0;
if (isset($_POST['scat']))
  $scat = $_POST['scat'];
else
  $scat = 0;
?>

<div <?php tie_content_column_attr(); ?>>

<form action="" method="post">
<label>Автор</label>
<select id="susr" name="susr">
  <option value="0">Выберите автора</option>
<?php
  $users = get_users();
  foreach($users as $user)
  {
    echo "<option ".($user->ID==$susr?"selected ":"")."value=\"".$user->ID."\">".$user->display_name."</option>";
  }

?>
</select>
<label>Категория</label>
<select id="scat" name="scat">
  <option value="0">Выберите категорию</option>
<?php
  $categories = get_categories();
  foreach($categories as $category)
  {
    echo "<option ".($category->term_id==$scat?"selected ":"")."value=\"".$category->term_id."\">".$category->name."</option>";
  }

?>
</select>
</form>
<script type='text/javascript'>
/* <![CDATA[ */
(function() {
  var dropdown1 = document.getElementById( "susr" );
  function onCatChange1() {
    dropdown1.parentNode.submit();
  }
  dropdown1.onchange = onCatChange1;

  var dropdown2 = document.getElementById( "scat" );
  function onCatChange2() {
    dropdown2.parentNode.submit();
  }
  dropdown2.onchange = onCatChange2;
})();
/* ]]> */
</script>

<?php
$args = array( 'post_type' => 'post' );
if ($susr != 0)
  $args['author'] = $susr;
if ($scat != 0)
  $args['category__and'] = $scat;

$query = new WP_Query( $args );

if ( $query->have_posts() ) :

      // Get the layout template part
      TIELABS_HELPER::get_template_part( 'templates/archives2', '', array(
        'layout'         => tie_get_option( 'blog_display', 'excerpt' ),
        'excerpt_length' => tie_get_option( 'blog_excerpt_length' ),
        'my_query'		 => $query
      ));

      // Page navigation
      TIELABS_PAGINATION::show( array( 'type' => tie_get_option( 'blog_pagination' ) ) );
else:
      TIELABS_HELPER::get_template_part( 'templates/not-found' );

endif;

?>
</div><!-- .main-content /-->
<?php

the code displays a drop-down list of authors, a drop-down list of headings, when you select an author and a heading, a list of posts of this heading is displayed, how to rewrite this code so that it is so
we select the author from the drop-down list, then the heading and press the apply filter button, then we display a list of posts of this heading ?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Grimashevich, 2018-08-26
@jams

Read about Wp_Query(). There is an option to sort by author, etc.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question