Skip Navigation

[Gelöst] Modify a pagination script

This support ticket is created vor 6 Jahre, 7 Monate. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 10 Antworten, has 2 Stimmen.

Last updated by Rune Brynestad vor 6 Jahre, 7 Monate.

Assisted by: Minesh.

Author
Artikel
#570914
pagination.jpg

I have created a blog portal where I'm using Toolset to create custom posts for blogs and blog posts. A typical blog post looks like this:

hidden link

Near the bottom of the page, I have created a pagination using a script that I found in your Community Forum

add_shortcode('check_url_param', 'func_check_url_param');
function func_check_url_param($atts, $content = '') {
if(isset($_GET['wpv_paged']) and $_GET['wpv_paged']>=2){
return 0;
}else{
return 1;
}
}

function pagination_func( $atts ){
return do_shortcode('[et_pb_section global_module="2169"][/et_pb_section]');
}
add_shortcode( 'pagination', 'pagination_func' );

By using this script, I'm able to paginate all blog posts. But is it possible to modify this script so it only paginate posts from the current author?

Thanks in advice.

Regards
Rune

#570941

Minesh
Supporter

Languages: Englisch (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Well - it seems you have added the pagination on single post and you would like to display only current loggedin user posts - correct? If yes - using view's you can filter the posts by current loggedin user and it seems that you are not using views here.

You should check with your theme author how you can display current logged in user posts for with single page with pagination.

#570987

Hi Minesh

Thanks for looking into this.

It's not the current logged in users post I want to paginate. It's posts where post author is the same as the displayed blog post.

I'm not able to see how I can use a view on a single blog post to create a pagination, so I ended up with this script that I found in your Community Forum. I have inserted the script in my child theme's function page, and used the Visual editor to display the shortcode in the single post layout. My question is if it is it possible to modify this script so it only paginate posts from the current author.

Thanks again.

Regards
Rune

add_shortcode('check_url_param', 'func_check_url_param');
function func_check_url_param($atts, $content = '') {
  if(isset($_GET['wpv_paged']) and $_GET['wpv_paged']>=2){
      return 0;
  }else{
      return 1;
  }
}

function pagination_func( $atts ){
	return do_shortcode('[et_pb_section global_module="2169"][/et_pb_section]');
}
add_shortcode( 'pagination', 'pagination_func' );
#570991

Minesh
Supporter

Languages: Englisch (English )

Timezone: Asia/Kolkata (GMT+05:30)

Well - its custom code and I do not see any way to filter your posts by the post author as no Toolset shortcode involved here. Maybe you can ask to divi support.

However - you can add the view filter by the post author.
=> https://toolset.com/documentation/user-guides/filtering-views-query-by-author/

And we have post next and previous pagination shortcode:
=> https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-post-previous-link
=> https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-post-next-link

#571049
pagination_2.jpg

Ok, I have created a view "Pagination" and used the Post author filter "Select posts with the author the same as the page where this View is shown", and was able to create the pagination links. But it does not work as it should. If you look at the pagination on this post:

hidden link

The next post "Moteuka" belongs to another author than the current post, and should not have been displayed in this pagination.

Regards
Rune

#571247

Minesh
Supporter

Languages: Englisch (English )

Timezone: Asia/Kolkata (GMT+05:30)

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I would additionally need your permission to de- and re-activate Plugins and the Theme, and to change configurations on the site. This is also a reason the backup is really important. If you agree to this, please use the form fields I have enabled below to provide temporary access details (wp-admin and FTP).

I have set the next reply to private which means only you and I have access to it.

#571394

Minesh
Supporter

Languages: Englisch (English )

Timezone: Asia/Kolkata (GMT+05:30)

Well - as you seen the following shortcode it does not have any attribute to filter the query by author and the pagination is set by current post you are displaying - so it displays the correct post "MOTEUKA" but author filter will not apply there as there is no argument available:
=> https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-post-previous-link
=> https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-post-next-link

Now, to display the filtered results I've created a test view and it just displays the correct posts belongs to that user:
=> hidden link

View's pagination will be attached to post type to which it attached so it will not override or have the effect on the master pagination you are displaying when you first load. It will not replace the layout content - it will only display the content available inside view's loop output.

You can check with the following Doc to know how view's pagination works:
=> https://toolset.com/documentation/user-guides/views-pagination/

#571734
pagination_2.jpg

Hi Minesh

As far as I can see, the only modification you did was changing the filter "Post author is the author of the page where this View is shown" to "Select posts which author's id is set by the View shortcode attribute "author" eg. [wpv-view name="Pagination" author="1"]"

When I try it, the post "MOTEUKA", written by another author, still shows in the pagination for this post:
hidden link

I'm sorry, but I can't see why it's not working. My loop output looks like this now:

[wpv-layout-start]
	[wpv-items-found]
	<!-- wpv-loop-start -->
	<wpv-loop>
		
	</wpv-loop>
	<!-- wpv-loop-end -->
	[/wpv-items-found]
[wpv-filter-start hide="false"]
[wpv-filter-controls]
<span class="nav-previous">[wpv-post-previous-link format="" %%LINK%%" link=" %%TITLE%%"]</span>
<span class="nav-next">[wpv-post-next-link format="%%LINK%% "" link=" %%TITLE%%"]</span>
[/wpv-filter-controls]
[wpv-filter-end]
	[wpv-no-items-found]
		<strong>[wpml-string context="wpv-views"]No items found[/wpml-string]</strong>
	[/wpv-no-items-found]
[wpv-layout-end]

Could you please look at it again and try to point me in the right direction?

Thanks in advice.

Regards
Rune

#572102

Minesh
Supporter

Languages: Englisch (English )

Timezone: Asia/Kolkata (GMT+05:30)

Well - as I tried to explain with my reply here:
=> https://toolset.com/forums/topic/modify-a-pagination-script/#post-571394

It's not logical or possible to combine current pagination with view's pagination. View's pagination is different and it attached to the specific view.

=> https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-post-previous-link
=> https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-post-next-link

As you seen the above shortcode it does not have any attribute to filter the query by author and the pagination is set by current post you are displaying - so it displays the correct post "MOTEUKA" because author filter will not apply there as there is no argument available.

To modify your current single page pagination to filter with the current post author it needs custom code and that is beyond the scope of our support policy. However - I'm sharing few links here which might help you to build your single-blogginnlegg.php and you can customize the single post query accordingly.
=> https://wordpress.stackexchange.com/questions/172422/paginate-get-related-post-by-author-function
=> https://wordpress.stackexchange.com/questions/143070/include-latest-author-posts-with-pagination-in-single-template
=> https://wordpress.stackexchange.com/questions/28376/how-to-use-next-post-link-and-previous-post-link-on-single-posts-in-search-resul

This is the correct example using view's how you can set pagination. I've created a test view and it just displays the correct posts belongs to that user:
=> hidden link

#572107

Minesh
Supporter

Languages: Englisch (English )

Timezone: Asia/Kolkata (GMT+05:30)

Well - I've good news for you. Finally, I found the solution for you using custom code that is easy to implement it and I've done that for you.

I've added following code to your current theme's functions.php file:

add_filter( "get_next_post_where", function($where, $in_same_term, $excluded_terms, $taxonomy, $post){
	global $post_type;
	
	if($post_type=='blogginnlegg' and is_singular('blogginnlegg')){
			$where .= " AND p.post_author='".$post->post_author."'";
	}
	
	return $where;
}, 10, 5);

add_filter( "get_previous_post_where", function($where, $in_same_term, $excluded_terms, $taxonomy, $post){
	if($post_type=='blogginnlegg' and is_singular('blogginnlegg')){
		$where .= " AND p.post_author='".$post->post_author."'";
	}
  return $where;
}, 10, 5);

I used filters 'get_next_post_where' and 'get_previous_post_where' to fix your issue and that leads to the solution you were looking for.

#572731

This works perfect as far as I can see. Thanks a lot for helping me on this.

Regards
Rune

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.