Rewriting default url for custom post in dashboard

Is there a way of rewriting the default url for a custom post menu item in the admin area. I would like to append a variable

e.g.
from this: edit.php?post_type=custom_post
to this: edit.php?post_type=custom_post&variable=4

  • Wonderdust
    • Site Builder, Child of Zeus

    Hi Michael, I'm using the following code:

    add_action('init', 'partner_news_register');
    
    function partner_news_register() {
    
    	$labels = array(
    		'name' => _x('My Partner News', 'post type general name'),
    		'singular_name' => _x('Partner News', 'post type singular name'),
    		'add_new' => _x('Add New', 'partner_news'),
    		'add_new_item' => __('Add New Partner News Item'),
    		'edit_item' => __('Edit Partner News Item'),
    		'new_item' => __('New Partner News Item'),
    		'view_item' => __('View Partner News Item'),
    		'search_items' => __('Search Partner News Item'),
    		'not_found' =>  __('Nothing found'),
    		'not_found_in_trash' => __('Nothing found in Trash'),
    		'parent_item_colon' => ''
    	);
    
    	$args = array(
    		'labels' => $labels,
    		'public' => true,
    		'publicly_queryable' => true,
    		'show_ui' => true,
    		'query_var' => true,
    		'rewrite' => array( 'slug' => 'partner-news' ),
    		'hierarchical' => false,
    		'menu_position' => null,
    		'supports' => array('title','editor','thumbnail'),
    		'has_archive' => true,
      		'capability_type' => 'posts',
        	'capabilities' => array(
                'read_post' => 'read_partner_news',
                'publish_posts' => 'publish_partner_news',
                'edit_posts' => 'edit_partner_news',
                'edit_others_posts' => 'edit_others_partner_news',
                'delete_posts' => 'delete_partner_news',
                'delete_others_posts' => 'delete_others_partner_news',
                'read_private_posts' => 'read_private_partner_news',
                'edit_post' => 'edit_partner_news',
                'delete_post' => 'delete_partner_news',
    
            ),
    	  ); 
    
    	register_post_type( 'partner_news' , $args );
    }
  • Ash
    • WordPress Hacker

    Hello @Wonderdust

    Please try the following code:

    add_action( 'admin_head', 'set_custom_var_in_url' );
    function set_custom_var_in_url() {
    	if( isset($_REQUEST['post_type']) && $_REQUEST['post_type'] == 'custom_post' && !isset($_REQUEST['variable']) ){
    		?>
    		<script type="text/javascript">
    		jQuery(function($){
    			var currentUrl = window.location.href;
    			window.location.href = currentUrl + '&&variable=4';
    		});
    		</script>
    		<?php
    	}
    }

    You can add those codes in your functions.php in the theme, if you think your theme won’t be changed. Otherwise mu-plugins is the best solution. To use mu-plugins, go to /wp-content/ and find the folder with name 'mu-plugins'. If there is no folder in that name, then create a folder, name it 'mu-plugins', create a file inside that, give any name you like and paste the code in there. You don't need to activate that plugin. Mu-plugins means must use plugins, so it will be activated automatically always. If you use mu-plugins then add a php start tag at the beginning of the code.

    Hope it helps :slight_smile: Please feel free to ask more question if you have.

    Cheers
    Ash

  • Wonderdust
    • Site Builder, Child of Zeus

    Hi Ash

    I have tried this method but when I change the appended url variables (I have changed to '&author=6&post_status=mine') The url gets stuck in a loop of redirecting and adding the variables to the end of the url each time.

  • Ash
    • WordPress Hacker

    Hello @Wonderdust

    Thank you for your patience, I had taken some days off.

    (I have changed to '&author=6&post_status=mine')

    So, are you adding two variables?

    Would you please be more specific? What is your current URL (before adding variables) and what URL you want it to look like? Please post me full URLs.

    Cheers
    Ash

  • Ash
    • WordPress Hacker

    Hello @Wonderdust

    Please try the following code:

    add_action( 'admin_head', 'set_custom_var_in_url' );
    function set_custom_var_in_url() {
    	if( isset($_REQUEST['post_type']) && $_REQUEST['post_type'] == 'partner_news' && !isset($_REQUEST['post_status']) ){
    		?>
    		<script type="text/javascript">
    		jQuery(function($){
    			var currentUrl = window.location.href;
    			window.location.href = currentUrl + '&author=6&post_status=mine';
    		});
    		</script>
    		<?php
    	}
    }

    Hope it helps :slight_smile: Please feel free to ask more question if you have.

    Cheers
    Ash

  • Wonderdust
    • Site Builder, Child of Zeus

    Thank you Ash

    The redirect works but only after a delay. The page generated by the url without the appended variables renders before the redirect occurs. I don't want the user to see this page at all. Is there any way of resolving this?

  • Wonderdust
    • Site Builder, Child of Zeus

    Thank you Ash

    The redirect works but only after a delay. The page generated by the url without the appended variables renders before the redirect occurs. I don't want the user to see this page at all. Is there any way of resolving this?

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.