Wordpress Pagination Links - how do I modify this?

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
simonmlewis
DevNet Master
Posts: 4434
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Wordpress Pagination Links - how do I modify this?

Post by simonmlewis »

I've found this code within Wordpress. I want to be able to change the query so it only finds comments where the comments_content field is not empty.

This isn't a normal PHP query to me, so not sure how to do it. Whether I need to add a function and puts something in there, so that $comments_content is only empty when it is used. I don't know. Any help would be great.

This query is in wp-includes/link-template.php.

Code: Select all

function paginate_comments_links( $args = array() ) {
	global $wp_rewrite;

	if ( ! is_singular() )
		return;

	$page = get_query_var('cpage');
	if ( !$page )
		$page = 1;
	$max_page = get_comment_pages_count();
	$defaults = array(
		'base' => add_query_arg( 'cpage', '%#%' ),
		'format' => '',
		'total' => $max_page,
		'current' => $page,
		'echo' => true,
		'add_fragment' => '#comments'
	);
	if ( $wp_rewrite->using_permalinks() )
		$defaults['base'] = user_<span style='color:blue' title='I&#39;m naughty, are you naughty?'>smurf</span>(<span style='color:blue' title='I&#39;m naughty, are you naughty?'>smurf</span>(get_permalink()) . $wp_rewrite->comments_pagination_base . '-%#%', 'commentpaged');

	$args = wp_parse_args( $args, $defaults );
	$page_links = paginate_links( $args );

	if ( $args['echo'] )
		echo $page_links;
	else
		return $page_links;
}
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6425
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Wordpress Pagination Links - how do I modify this?

Post by Celauran »

A few things. First, don't modify the WordPress core. Next time you update WP, all your changes will be lost. Write a plugin or make changes at the theme level. If you're using a third-party theme, create a sub-theme and make the changes there so you can update your theme later.

To the crux of the matter. That function you posted doesn't retrieve comments, it simply creates the pagination links for comments already retrieved. Comments themselves are retrieved by wp_list_comments. I haven't worked with WordPress in a couple of years, so I could be wrong, but I'm not seeing any obvious way to exclude empty comments. I'm kind of surprised that isn't default behaviour. Does it display empty comments even when you aren't logged in?

In any event, wp_list_comments does accept an array of comment objects as its second parameter, so you could retrieve the defaults, use array_filter or similar to filter out the empty comments, and then pass the result back to wp_list_comments. Barring that, you may need to create your own methods to retrieve and paginate over your comments. Take a look at this tutorial for ideas.
simonmlewis
DevNet Master
Posts: 4434
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Wordpress Pagination Links - how do I modify this?

Post by simonmlewis »

I don't know about these queries, but yes I can see what you mean, as there is no actual query on this file.
On the page you sent, "Other Simple Comment Restrictions", this is I think where it is trying to guide to do it.
[text]Using these parameters we could list a custom comment type which contains the term "kazoo".[/text]

Code: Select all

$args = array(
    'type' => 'review',
    'search' => 'kazoo'
);
$comments_query = new WP_Comment_Query;
$comments = $comments_query->query( $args );
So how could you use this, but not to say where reviews is on "kazoo", but where comment_content is not empty? And how to do get that into the query?

That's what I am trying to learn. Oddly, it is only for this one website, because of the import we did. But it's important, as we get so many reviews, we are at the moment, only showing 30 (disabling the pagination via CSS for now).
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6425
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Wordpress Pagination Links - how do I modify this?

Post by Celauran »

If these are simply due to an import, would it not be easier to simply mark them as unapproved, or to delete them altogether?
simonmlewis
DevNet Master
Posts: 4434
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Wordpress Pagination Links - how do I modify this?

Post by simonmlewis »

No. I wish we could. We imported over 5,000 star reviews from a non-WP site. To do that, and keep the reviews schema, they were imported into Reviews but as they were just ratings, they had no 'comments'.

At the top of the page we have all the star reviews as an average. And in the Reviews tab, we show only reviews where it has something in comment_content, but the pagination still shows ALL the page numbers for every review for that product/post, no matter what is in that field.... hence trying to fix pagination.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
simonmlewis
DevNet Master
Posts: 4434
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Wordpress Pagination Links - how do I modify this?

Post by simonmlewis »

https://codex.wordpress.org/Function_Re ... ents_links
https://developer.wordpress.org/referen ... nts_links/

Found these two pages. The query itself must be somewhere, in order to extract the info. I'm told that these things are edited via Hooks and Actions, but that this query doesn't have those.

I suppose it's because usually no one needs to edit it. But we need to.

It would even be in the basic Wordpress setup for reviews on Posts. Just need to figure out how to write a little query where comment_content is not empty, and put that into the query. But with WP queries I am stumped.
If it was my custom coded website, it would be easy.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6425
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Wordpress Pagination Links - how do I modify this?

Post by Celauran »

simonmlewis wrote:https://codex.wordpress.org/Function_Re ... ents_links
https://developer.wordpress.org/referen ... nts_links/

Found these two pages. The query itself must be somewhere, in order to extract the info. I'm told that these things are edited via Hooks and Actions, but that this query doesn't have those.

I suppose it's because usually no one needs to edit it. But we need to.

It would even be in the basic Wordpress setup for reviews on Posts. Just need to figure out how to write a little query where comment_content is not empty, and put that into the query. But with WP queries I am stumped.
If it was my custom coded website, it would be easy.
Have you tried what I suggested above?
In any event, wp_list_comments does accept an array of comment objects as its second parameter, so you could retrieve the defaults, use array_filter or similar to filter out the empty comments, and then pass the result back to wp_list_comments. Barring that, you may need to create your own methods to retrieve and paginate over your comments. Take a look at this tutorial for ideas.
simonmlewis
DevNet Master
Posts: 4434
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Wordpress Pagination Links - how do I modify this?

Post by simonmlewis »

so you could retrieve the defaults

Code: Select all

<?php $args = array(
	'walker'            => null,
	'max_depth'         => '',
	'style'             => 'ul',
	'callback'          => null,
	'end-callback'      => null,
	'type'              => 'all',
	'reply_text'        => 'Reply',
	'page'              => '',
	'per_page'          => '',
	'avatar_size'       => 32,
	'reverse_top_level' => null,
	'reverse_children'  => '',
	'format'            => 'html5', // or 'xhtml' if no 'HTML5' theme support
	'short_ping'        => false,   // @since 3.6
        'echo'              => true     // boolean, default is true
); ?>
use array_filter or similar to filter out the empty comments
How?
then pass the result back to wp_list_comments
Again how. sorry I am totally blind with this, but I do want to learn. Iv'e seen people doing functions and passing stuff back but never understood it.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6425
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Wordpress Pagination Links - how do I modify this?

Post by Celauran »

Again, not having worked with WP in a couple of years, and having to leave for work myself in a few minutes, here's where I'd start:

wp_list_comments allows you to hook into a filter: wp_list_comments_args. If I recall WP correctly, you'll need to add_filter to tell WP what to run when that filter is applied. Take a look at this tutorial to understand how filters work. This is where you'll likely want to use array_filter to remove all the comments with empty contents.

For an example of how array_filter works, consider this:

Code: Select all

$array = [
    ['id' => 'A', 'condition' => true],
    ['id' => 'B', 'condition' => false],
    ['id' => 'C', 'condition' => false],
    ['id' => 'D', 'condition' => true],
];

$filtered = array_filter($array, function($item) {
    return $item['condition'] === true;
});

print_r($filtered);
which prints the following:
[text]Array
(
[0] => Array
(
[id] => A
[condition] => 1
)

[3] => Array
(
[id] => D
[condition] => 1
)

)[/text]
Make sense?
Again how. sorry I am totally blind with this, but I do want to learn. Iv'e seen people doing functions and passing stuff back but never understood it.
https://laracasts.com/series/php-for-beginners
Take the time to go through this free video series. Yes, it's basic and there will be things you're already well aware of, but I expect it will also help fill in the gaps on the things you're less familiar with. From there, I'm happy to answer any questions you might have.
simonmlewis
DevNet Master
Posts: 4434
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Wordpress Pagination Links - how do I modify this?

Post by simonmlewis »

Ok I can see how arrays work, and the different types. Where you can take an array, create a function with an added part to it, then merge them together.
What I don't yet see if how I can say in the array of things it is looking for in the default arg, where comment_content is empty.

In the default arg, a portion of it is this:

Code: Select all

'page'              => '',
Is the => a way of saying "equals"?
Such as "page = 'fred'"?

So could you do something a bit like this:

Code: Select all

<?php wp_list_comments( array( 'comment_content' != '' ) ); ?>
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6425
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Wordpress Pagination Links - how do I modify this?

Post by Celauran »

simonmlewis wrote:In the default arg, a portion of it is this:

Code: Select all

'page'              => '',
Is the => a way of saying "equals"?
Such as "page = 'fred'"?
Yes, that's exactly right. These are equivalent:

Code: Select all

$shirt = [
    'size' => 'M',
    'colour' => 'Red',
];

Code: Select all

$shirt = [];
$shirt['size'] = 'M';
$shirt['colour'] = 'Red';
simonmlewis wrote:What I don't yet see if how I can say in the array of things it is looking for in the default arg, where comment_content is empty.
You can't. Keep going through the function and you'll see it calls apply_filters. It provides the name of the filter it will call, which allows you to hook into it via add_filter. You pass that the name of the filter as the first argument, and the name of the function you want run when that filter is called as the second argument. This second argument will be the function you define, and this is where you can filter out comments with no content.
simonmlewis
DevNet Master
Posts: 4434
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Wordpress Pagination Links - how do I modify this?

Post by simonmlewis »

Thanks for confirmation.
I still don't see where you state where something is *NOT* something.
Where color is NOT red.
Where fieldname is NOT empty.
Where comment_content != ''.

Even though you cannot do that, surely you have to somehow tell it in the arg, where a field is not empty. That's the start of this puzzle for me. I See lots where field => 'red'. but not where field != 'red'.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
simonmlewis
DevNet Master
Posts: 4434
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: Wordpress Pagination Links - how do I modify this?

Post by simonmlewis »

Taking a step backward a little.
First I need to get into the function for list comments.

Code: Select all

wp_list_comments( $args, $comments );
This is the array in it's Arguments.

Code: Select all

$args = array(
	'walker'            => null,
	'max_depth'         => '',
	'style'             => 'ul',
	'callback'          => null,
	'end-callback'      => null,
	'type'              => 'all',
	'reply_text'        => 'Reply',
	'page'              => '',
	'per_page'          => '',
	'avatar_size'       => 32,
	'reverse_top_level' => null,
	'reverse_children'  => '',
	'format'            => 'html5', // or 'xhtml' if no 'HTML5' theme support
	'short_ping'        => false,   // @since 3.6
        'echo'              => true     // boolean, default is true
So I need to add_array to this.

Code: Select all

if(has_filter('wp_list_comments')) {
		$args = apply_filters('wp_list_comments', $args);
	}
Then this?

Code: Select all

function wp_list_comments_remove_blanks($args) {
	// the $args parameter is an array of arguments for  wp_list_comments() function
 
$additional_arg = array(
		'comment_content'  != '',
	);
 
	// combine the two arrays
	$arg = array_merge($additional_arg, $arg);
	return $arg;
}
add_filter('wp_list_comments', 'wp_list_comments_remove_blanks');
Is this getting toward it ?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6425
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Wordpress Pagination Links - how do I modify this?

Post by Celauran »

Go back and look at my array_filter example. That's where you'd do it, inside the filter function you defined.
User avatar
Celauran
Moderator
Posts: 6425
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Wordpress Pagination Links - how do I modify this?

Post by Celauran »

I'm pretty sure you want to hook into wp_list_comments_args. On my phone on the subway, so hard for me to dig into the code, but that's where I'd start. I'll look further tonight if you haven't resolved it.
Post Reply