Remove Pagination in Appearance -> Menus -> Categories - php

I asked this question on WP Stackexchange already but did not get any answers. So let me try here. Wordpress Stackexchange
Is there a way to remove the Pagination in the Wordpress Menu Editor?
I have something like 200 categories in my blog and customising menus seems to be tricky when you have to click through it to find the desired category.
I know there's a plugin that "removes" the pagination for PAGES but I could not find anything to remove the pagination for the CATEGORIES.
I tried to find something in the Wordpress Admin PHP files or even nav-menus.php but did not get lucky.
Link to Problem

Ok after reading through the source code I found that the number of categories returned in the edit menu section is hardcoded to 50 on line 613 of \wp-admin\includes\nav-menu.php
// Paginate browsing for large numbers of objects.
$per_page = 50;
$pagenum = isset( $_REQUEST[$taxonomy_name . '-tab'] ) && isset( $_REQUEST['paged'] ) ? absint( $_REQUEST['paged'] ) : 1;
$offset = 0 < $pagenum ? $per_page * ( $pagenum - 1 ) : 0;
In order to override the default of 50 per page you can set the number to '' to instruct the query to return all categories. Add the following code to your functions.php file.
add_filter( 'get_terms_args', 'show_all_categories_admin_nav_menu', 10, 2);
function show_all_categories_admin_nav_menu( $args, $taxonomies ) {
if( reset($taxonomies) === 'category' ) {
$args['number'] = '';
}
return $args;
}
If you set the number to blank it still shows the pagination even though it's showing all the categories.
There's also a filter called terms_clauses that exists in which you can remove the SQL LIMIT clause from the query but this didn't seem to have any affect on the query.
add_filter('terms_clauses', 'modify_terms_clauses', 10, 3);
function modify_terms_clauses( $clauses, $taxonomies, $args ) {
if( reset($taxonomies) === 'category' ) {
$clauses['limits'] = '';
}
return $clauses;
}

Related

Wordpress custom plugin - Select post status on default

I am creating my own plugin in Wordpress. With register_post_type I can view my own posts. I also created a post status with register_post_status. When I go to the summary page I can see all posts and filter on the status.
The "problem": When I go to the summary page, I can see all posts and the filters. The "All" status is always selected on default, but I want to select my custom status on default. Is this possible? Or change the URL in the menu to post_status=&post_type= ? I am talking about the admin side.
Hope someone can help me, because I can't figure it out.
I have fixed it with this code - it changes the url in the menu:
add_action( 'admin_menu', 'wpse_admin_menu', 100 );
function wpse_admin_menu()
{
global $menu, $submenu;
$parent = 'parent';
if( !isset($submenu[$parent]) )
return;
foreach( $submenu[$parent] as $k => $d ){
if( $d[0] == 'name' )
{
$submenu[$parent][$k][2] = 'edit.php?post_status=status&post_type=type';
break;
}
}
}

Change number of posts to display in category page

Currently by default Wordpress display 8 posts in category.php page, i need to increase or decrease limit.
How to get it?
Thank you.
Use the pre_get_posts filter to modify the number of posts displayed on categories.
Example:
function wpse_modify_category_posts_per_page( $query ) {
// Check we're on the frontend and modifying the main query.
if ( ! is_admin() && $query->is_main_query() ) {
// Change to 8 posts per page when viewing a category.
if ( $query->is_category() ) {
$query->set( 'posts_per_page', 8 );
}
}
}
add_action( 'pre_get_posts', 'wpse_modify_category_posts_per_page' );
Here we begin by checking we're not in the admin and we're affecting the main query. Then we test whether we're viewing a category page.
If all of those tests are passed we use the set method to change the number of posts per page.
Further reading: http://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts
In your category.php file, before the while loop, add this
// Get the last 10 posts in the special_cat category.
<?php query_posts( 'posts_per_page=10' ); ?>
Replace 10 with your desired number.

Change number of posts on consecutive category pages (Wordpress)

I am trying change the number of posts that are displayed on a category pages to change on consecutive pages (page 2, 3, etc). So page one displays 7 posts, but pages 2, 3 and 4, etc of that category display only 6 posts per page (i.e. when you click 'next page' to list the older posts).
I am aware that it is relatively straightforward to change the number of posts for different categories / archive pages - but this is different, as I would like the paginated pages to have different numbers of posts.
Any ideas?
This is from an answer I recently done on WPSE. I have made some changes to suite your needs. You can go and check out that post here
STEP 1
If you have changed the main query for a custom query, change it back to the default loop
<?php
if ( have_posts() ) :
// Start the Loop.
while ( have_posts() ) : the_post();
///<---YOUR LOOP--->
endwhile;
//<---YOUR PAGINATION--->
else :
//NO POSTS FOUND OR SOMETHING
endif;
?>
STEP 2
Use pre_get_posts to alter the main query to change the posts_per_page parameter on the category pages
STEP 3
Now, get the posts_per_page option set from the back end (which I assume is 6) and also set your offset which we are going to use. That will be 1 as you will need 7 posts on page one and 6 on the rest
$ppg = get_option('posts_per_page');
$offset = 1;
STEP 4
On page one, you'll need to add the offset to posts_per_page will add up to 7 to get your seven posts on page one.
$query->set('posts_per_page', $offset + $ppp);
STEP 5
You must apply your offset to all subsequent pages, otherwise you will get a repetition of the last post of the page on the next page
$offset = $offset + ( ($query->query_vars['paged']-1) * $ppp );
$query->set('posts_per_page',$ppp);
$query->set('offset',$offset);
STEP 6
Lastly, you need to subtract your offset from found_posts otherwise your pagination on the last page will be wrong and give you a 404 error as the last post will be missing due to the incorrect post count
function category_offset_pagination( $found_posts, $query ) {
$offset = 1;
if( !is_admin() && $query->is_category() && $query->is_main_query() ) {
$found_posts = $found_posts - $offset;
}
return $found_posts;
}
add_filter( 'found_posts', 'category_offset_pagination', 10, 2 );
ALL TOGETHER
This is how your complete query will look like that should go into functions.php
function ppp_and_offset_category_page( $query ) {
if ($query->is_category() && $query->is_main_query() && !is_admin()) {
$ppp = get_option('posts_per_page');
$offset = 1;
if (!$query->is_paged()) {
$query->set('posts_per_page',$offset + $ppp);
} else {
$offset = $offset + ( ($query->query_vars['paged']-1) * $ppp );
$query->set('posts_per_page',$ppp);
$query->set('offset',$offset);
}
}
}
add_action('pre_get_posts','ppp_and_offset_category_page');
function category_offset_pagination( $found_posts, $query ) {
$offset = 1;
if( !is_admin() && $query->is_category() && $query->is_main_query() ) {
$found_posts = $found_posts - $offset;
}
return $found_posts;
}
add_filter( 'found_posts', 'category_offset_pagination', 10, 2 );

How to change woocommerce product image upload path

I have a wordpress multisite with one main and four child website. i have done plugin for share woocommerce product to childsite. now child site can add or update mainsite product from child site. but when displaying in front end the media path is wrong. i want use all child site product image path to same path. how it possible ?
In woocommerce plugin and in file class-wc-admin-post-types.php
woocomerce override the WordPress upload filter through the filter 'upload_dir'
add_filter( 'upload_dir', array( $this, 'upload_dir' ) );
public function upload_dir( $pathdata ) {
// Change upload dir for downloadable files
if ( isset( $_POST['type'] ) && 'downloadable_product' == $_POST['type'] ) {
if ( empty( $pathdata['subdir'] ) ) {
$pathdata['path'] = $pathdata['path'] . '/woocommerce_uploads';
$pathdata['url'] = $pathdata['url']. '/woocommerce_uploads';
$pathdata['subdir'] = '/woocommerce_uploads';
} else {
$new_subdir = '/woocommerce_uploads' . $pathdata['subdir'];
$pathdata['path'] = str_replace( $pathdata['subdir'], $new_subdir, $pathdata['path'] );
$pathdata['url'] = str_replace( $pathdata['subdir'], $new_subdir, $pathdata['url'] );
$pathdata['subdir'] = str_replace( $pathdata['subdir'], $new_subdir, $pathdata['subdir'] );
}
}
return $pathdata;
}
so if you want to override it, you can create filter to this 'upload_dir' with higher priorites.
I would call this a try and error thing, I had done something similar but it takes some time customizing as your desired.
So here it's what I believe could solve your main problem:
add_action('pre_get_posts', '_my_pre_get_posts', 10, 1);
function _my_pre_get_posts( $wp ) {
global $typenow, $blog_id;
if ( 'product' == $typenow && $blog_id != 1) {
switch_to_blog(1);
}
}
if the $blog_id is not the main site (reads ID = 1) you should switch to blog ID 1, all this is happening before all post are pulled from the database.
product is the current post_type being queried, if you want this to work with orders as well you probably will check for 'shop_order'.
To know which post_type is being queried you should check the url, Ex: /edit.php?post_type=shop_order.
This probably would bring some issues such as the title of the blog in the adminbar is the one from the main site, and it's missing that at some point it needs to restore to the main site back, but at least this should do the trick.
check this Solution and Let me know how end up

How to exclude a post by id from the category.php query in wordpress

I have a template where there is a main latest featured post (tagged as featured), and then 8 more below it.
On the homepage, i was able to query for the latest featured post, and then pass its id to a function in the pre_get_posts wordpress filter. it works great there
function mh_exclude_featured_query( $query ) {
if(is_home()){
if ( $query->is_home() ) {
$feat = get_featured_post();
$query->query_vars['post__not_in'] = array($feat->ID);
}
}
}
add_action( 'pre_get_posts', 'mh_exclude_featured_query' );
but i'm also trying to do the same thing in the category.php, where i would show the latest post tagged as featured from that category. and then the remaining posts below with the featured post excluded.
Unfortnately, when i try the same method as above by using the pre_get_posts filter, i get stuck in an infinite loop and run out of memory.
if($query->is_category() && $query->is_main_query()){
$cur_cat_id = get_cat_id( single_cat_title("",false) );
$feat = get_featured_post($cur_cat_id);
$query->query_vars['post__not_in'] = array($feat->ID);
}
not sure what i'm doing differently that leads to a memory exhaustion. the category.php and index.php are near identical in their structure.
use the pre_get_posts filter:
<?php
function excludePostId($query) {
$postIds = array(
24, 10
);
if (is_category() && is_main_query()) {
set_query_var('post__not_in', $postIds);
}
}
add_action('pre_get_posts', 'excludePostId');
is_category() will accept a category slug or ID. You can limit which categories the post will be excluded from.
Add This Code
<?php if(!is_category('category_id')){
echo 'Your Code Here';
}?>

Categories