0

It is a bit complicated to formulate (or maybe just I'am so tired now), but I trying will be clear:

I want creating archive pages for liked posts by users. This will be similar to author pages, like:

 author pages: domain.tld/author/%USER%/(page/x/)
 liked pages: domain.tld/likedby/%USER%/(page/x/)

I can get these liked posts by the current user with my sql query, like this (I use the wp_ulike plugin):

 $sql =  "
 SELECT * FROM wp_ulike WHERE user_id = '" . $user_id . "'
 AND status = 'like'
 ORDER BY wp_ulike.date_time DESC
 LIMIT " . $offset . ", " . $post_per_page . "; "; 

 $user_logs = $wpdb->get_results( $sql, OBJECT );

But, how can I create a page template, like category.php, tag.php or author.php, to my metadata query, so how can I make a template (like likedby.php), what generating dynamic pages with my all user's nick, like this: domain.tld/likedby/%USER%/(page/x/)?

My first idea, that I create a custom taxonomy (likedby) and I add my users names to this with a function, like $tags... So in this case, this is it, what create the dynamic pages, and I create a template for this custom taxonomy, what really use my sql query... but this not so elegant, because the taxonomy will be empty, and just need for that creating the pages... So I hope exist a targeted solution for this...

(Maybe my question not so clear, in turn I dont find anything on the net about this problem, but maybe I just use bad keywords, If you know, what is that, what I need searching, please write a comment, any idea is helpful!)

0

A fairly simple way to do this is to create a new page with the slug likedby, then add some rewrite rules to handle the extra parameters.

First, the query var that holds the requested username:

function wpd_likedby_query_var( $query_vars ) {
    $query_vars[] = 'likeduser';
    return $query_vars;
}
add_filter( 'query_vars', 'wpd_likedby_query_var' );

Then a couple of rewrite rules to handle the requests, directing them to our likedby page and setting the extra parameters:

function wpd_likedby_rules(){
    add_rewrite_rule(
        'likedby/([^/]+)/page/?([0-9]{1,})/?$',
        'index.php?pagename=likedby&likeduser=$matches[1]&paged=$matches[2]',
        'top'
    );
    add_rewrite_rule(
        'likedby/([^/]+)/?$',
        'index.php?pagename=likedby&likeduser=$matches[1]',
        'top'
    );
}
add_action( 'init', 'wpd_likedby_rules' );

You can then create a page-likedby.php template and put your custom queries in there. Use get_query_var( 'likeduser' ) and get_query_var( 'paged' ) to get the query parameters.

I also recommend looking at wpdb::prepare() for generating queries that accept unknown input.

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.