1

Hi I'm trying to display post by author on author.php but I can't get the author ID. domain.com/author/user_23/ ( display post from that user )

JQuery

    jQuery('#menu_posts').click(function(){
    $.ajax({
        type        : 'GET',
        url         : '/wp-admin/admin-ajax.php',
        dataType    : 'html',
        data        : { action: 'user_posts' },
        success : function(data){
                  $('#user-tab').html(data);
            },
            error: function(data){  
                alert("Error!");
                return false;
            }
        });
    });

Function

add_action('wp_ajax_user_posts', 'user_posts');
add_action('wp_ajax_nopriv_user_posts', 'user_posts');

function user_posts() {
    $args = array(
        'author'                 => $curauth->ID,
        'orderby'                => 'asc',
        'post_type'              => 'post',
        'posts_per_page'         => 10
    );
    $get_posts_query = new WP_Query( $args );
        while ( $get_posts_query->have_posts() ) : $get_posts_query->the_post() ?>

            (( html here ))

    <?php
    endwhile;
    die();
}

author.php

<?php $curauth = (get_query_var('author_name')) ? get_user_by('slug', get_query_var('author_name')) : get_userdata(get_query_var('author')); ?>
1

Author.php means you are looking at an archive of the author already, so why would you need to display posts by that author?

Regardless, you can get the author ID from get_queried_object, which returns the whole user object.

<?php

$curauth = get_queried_object();
if($curauth && isset($curauth->ID)){
    //...trigger ajax to pass ID via the data object
}

Reference:

Edit

You will need to amend your author.php file. I would recommend storing the author ID as a data component so your script can easily pick it up.

For example:

<?php
/* 
* This is inside your author.php file, based on this:
* https://github.com/WordPress/WordPress/blob/master/wp-content/themes/twentyfourteen/author.php 
* Yours may differ. 

*/

get_header(); ?>

    <section id="primary" class="content-area">
        <div id="content" class="site-content" role="main">

            <?php if ( have_posts() ) : 
                  $curauth = get_queried_object();
            ?>

            <header class="archive-header" <?php echo sprintf('data-author_id="%s"'), ($curauth && isset($curauth->ID) ? $curauth->ID : 0 ) ?>>
                <h1 class="archive-title">

                //...the rest of the file below...

Then, you will need to amend your javascript to look for the author ID, include it in the Ajax call and change the method to POST:

jQuery('#menu_posts').click(function(){
    var $curauth = $('[data-author_id]').data('author_id');
    $.ajax({
        type        : 'POST',
        url         : '/wp-admin/admin-ajax.php',
        dataType    : 'html',
        data        : { action: 'user_posts', author_id: $curauth },
        success : function(data){
                  $('#user-tab').html(data);
            },
            error: function(data){  
                alert("Error!");
                return false;
            }
        });
    });

You are now POSTing data to the ajax action. You will need to grab the author ID from there:

add_action('wp_ajax_user_posts', 'user_posts');
add_action('wp_ajax_nopriv_user_posts', 'user_posts');

function user_posts() {

    // You should add security here, like check_ajax_referer

    $curauth = (isset($_POST['author_id']) ? $_POST['author_id'] : false;

    $args = array(
        'author'                 => $curauth,
        'orderby'                => 'asc',
        'post_type'              => 'post',
        'posts_per_page'         => 10
    );
    $get_posts_query = new WP_Query( $args );
        while ( $get_posts_query->have_posts() ) : $get_posts_query->the_post() ?>

            (( html here ))

    <?php
    endwhile;
    wp_die();
}

The above should get you started, but there is no security or escaping variables, so I would definitely recommend the following links for further reading:

  • Hi thank you, can you help me to achieve this please ? – mems Nov 30 '19 at 13:23
  • 1
    @mems See my update in the answer above. – Tom Nov 30 '19 at 13:41
  • Thank you very much :) – mems Nov 30 '19 at 14:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.