Is there a way to create an alternate permalink for a specific content type in Wordpress?
For example, I want an alias url:
https://example.com/123
to point to an existing permalink such as:
https://example.com/something/this-is-a-post
I don't mind writing code if I have to.
After a little playing around, I came up with a method. First I set up an action on the init hook. Then I use the following code to confirm the id from the URL is valid and perform a redirection to the permalink.
function my_init() {
global $wpdb;
// Using an alias id to redirect to permalink
// split URL path into parts and look at the first part (index 0)
$path_parts = explode("/", substr(wp_parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH), 1));
// make sure we're dealing with an number
if (is_numeric($path_parts[0]))
{
// query the database using the numeric value to see if it corresponds to a post ID of the mp type and its status is publish
$results = $wpdb->get_results("SELECT post_type, post_status FROM wp_posts WHERE ID = $path_parts[0]");
if ($results[0]->post_type == 'mp' && ($results[0]->post_status == 'publish' || is_admin()))
{
// redirect to permalink url
wp_redirect(get_post_permalink($path_parts[0]));
exit;
}
}
}
Related
There's a call to wp_count_posts() on every page in wp-admin. I would think it should only happen on the product pages. Is there a way to disable the call on all pages but products? Our site has over 100,000 products, and this call slows down wp-admin pages.
The following is the caller log from Query Monitor
wp_count_posts()
wp-includes/post.php:2859
WC_Install::is_new_install()
wp-content/plugins/woocommerce/includes/class-wc-install.php:399
WC_Admin_Notices::init()
wp-content/plugins/woocommerce/includes/admin/class-wc-admin-notices.php:58
WC_Admin->includes()
wp-content/plugins/woocommerce/includes/admin/class-wc-admin.php:62
do_action('init')
wp-includes/plugin.php:470
Could you try the following. Add following function in your theme functions.php . All credits goes to - wordpress remove post status count from cms
I have edited $post_type where we want all post types and $exclude_post_types changed to product over page post type.
add_filter('bulk_post_updated_messages', 'suppress_counts', 10, 2);
// We need to let the function "pass through" the intended filter content, so accept the variable $bulk_messages
function suppress_counts($bulk_messages) {
// If the GET "post_type" is not set, then it's the "posts" type
$post_type = (isset($_GET['post_type'])) ? $_GET['post_type'] : '';
// List any post types you would like to KEEP counts for in this array
$exclude_post_types = array('product');
// Global in the variable so we can modify it
global $locked_post_status;
// If the post type is not in the "Exclude" list, then set the $locked variable
if ( ! in_array($post_type, $exclude_post_types)) {
$locked_post_status = TRUE;
}
// Don't forget to return this so the filtered content still displays!
return $bulk_messages;
}
I want to remove the post status count from WordPress edit.php.
My WordPress CMS have more than 500,000 posts. The publish count is loaded every time you open the page. The following query is fired every time.
This makes my Wordpress CMS loading very slow.
SELECT post_status, COUNT( * ) AS num_posts FROM wp_posts WHERE post_type = 'post' GROUP BY post_status
By tracing the code, I've worked up the only solution that I can see.
The filter bulk_post_updated_messages is ONLY called on the edit.php screen.
The counts are not calculated (in class-wp-posts-list-table.php, get_views method) if the global variable $locked_post_status is not empty.
By gluing these two pieces of information together, I've got a solution that you can use by dropping it into your theme's functions.php file:
// Hook this filter, only called on the `edit.php` screen.
// It's not the "correct" filter, but it's the only one we can leverage
// so we're hijacking it a bit.
add_filter('bulk_post_updated_messages', 'suppress_counts', 10, 2);
// We need to let the function "pass through" the intended filter content, so accept the variable $bulk_messages
function suppress_counts($bulk_messages) {
// If the GET "post_type" is not set, then it's the "posts" type
$post_type = (isset($_GET['post_type'])) ? $_GET['post_type'] : 'post';
// List any post types you would like to KEEP counts for in this array
$exclude_post_types = array('page');
// Global in the variable so we can modify it
global $locked_post_status;
// If the post type is not in the "Exclude" list, then set the $locked variable
if ( ! in_array($post_type, $exclude_post_types)) {
$locked_post_status = TRUE;
}
// Don't forget to return this so the filtered content still displays!
return $bulk_messages;
}
i came up with this solution.
//Disable Article Counter - query runs for about 1-2 seconds
add_filter('admin_init', function () {
foreach (get_post_types() as $type) {
$cache_key = _count_posts_cache_key($type, "readable");
$counts = array_fill_keys(get_post_stati(), 1);
wp_cache_set($cache_key, (object)$counts, 'counts');
}
}, -1);
add_action('admin_head', function () {
$css = '<style>';
$css .= '.subsubsub a .count { display: none; }';
$css .= '</style>';
echo $css;
});
the post counter uses the wp-cache, the idea behind this approach is, to prefill the cache with the "correct" object containing 1's (0 would skip the status from being clickable) - at the earliest moment.
it results in all stati being displayed - with 1's and the query is not run et-all
in addition it returns a css snippet to hide the (1)
I'm using a setting from my plugin to generate the rewrite rule:
$bioPage = $wpdb->get_var("SELECT settingValue FROM $table_name WHERE settingType='bioPage'");
$post = get_post($bioPage);
$slug = $post->post_name;
add_rewrite_tag('%player%','([^&]+)');
add_rewrite_rule('^'.$slug.'/([^/]*)$','/'.$slug.'/?player=$matches[1]','top');
I am getting the correct slug.
I have a url that looks like this:
mysite.com/info/?player=joe.smith.01
I want it to look like this:
mysiste.com/info/joe.smith.01
I'm using the add_rewrite_tag so WP will grab the variable for the url. When I visit a page url structured like what I'm trying to achieve I get a page not found.
I would not recommend modifying the htaccess file if you don't really have to. WP's Rewrite APi should do just fine.
That said, I'm pretty sure you have to go via 'index.php' in the rewrite argument of add_rewrite_rule().
I don't have any way of testing this right now, but something along the lines of the following might work:
$bioPage = $wpdb->get_var("SELECT settingValue FROM $table_name WHERE settingType='bioPage'");
$post = get_post($bioPage);
$slug = $post->post_name;
$page_id = $post->ID; //Get the post ID to include in the page_id query var
add_rewrite_tag('%player%','([^&]+)');
add_rewrite_rule('^'.$slug.'/([^/]*)$','index.php?page_id='.$page_id'.&player=$matches[1]','top');
I want to create an drupal page Template depending on the url alias.
Her my current situation:
I create a page named test, the url alias is test, too.
The page template, based on this docu - http://drupal.org/node/1089656 is: page--test.tpl.php.
I cleaned the drupal them cache, but there is still the default page template shown for this page.
What could be the error?
page--test.tpl.php doesn't work because Drupal is using the real path of page--node--#.tpl.php. To get Drupal to recognize aliased paths, you have to add the aliased path as part of the theme suggestions like so:
function MYMODULE_preprocess_page(&$vars, $hook) {
// only do this for page-type nodes and only if Path module exists
if (module_exists('path') && isset($vars['node']) && $vars['node']->type == 'page') {
// look up the alias from the url_alias table
$source = 'node/' .$vars['node']->nid;
$alias = db_query("SELECT alias FROM {url_alias} WHERE source = '$source'")->fetchField();
if ($alias != '') {
// build a suggestion for every possibility
$parts = explode('/', $alias);
$suggestion = '';
foreach ($parts as $part) {
if ($suggestion == '') {
// first suggestion gets prefaced with 'page--'
$suggestion .= "page--$part";
} else {
// subsequent suggestions get appended
$suggestion .= "__$part";
}
// add the suggestion to the array
$vars['theme_hook_suggestions'][] = $suggestion;
}
}
}
}
Source: http://groups.drupal.org/node/130944#comment-425189
I'm trying to create a permalink pattern for a Custom Type, that includes one of its taxonomies. The taxonomy name is known from the start (so I'm not trying to add or mix all of its taxonomies, just a specific one), but the value will by dynamic, of course.
Normally, the Custom Type permalink is built using the rewrite arg with the slug param, but I don't see how I could add a dynamic variable in there.
http://codex.wordpress.org/Function_Reference/register_post_type
I'm guessing a custom solution is required, but I'm not sure what the best unintrusive approach would be.
Is there a known practice for this or has anyone built something similar recently? I'm using WP 3.2.1 btw.
After more searching I managed to create fairly elegant solution using the custom_post_link filter.
Let's say you have a project Custom Type with a client Taxonomy. Add this hook:
function custom_post_link($post_link, $id = 0)
{
$post = get_post($id);
if(!is_object($post) || $post->post_type != 'project')
{
return $post_link;
}
$client = 'misc';
if($terms = wp_get_object_terms($post->ID, 'client'))
{
$client = $terms[0]->slug;
//Replace the query var surrounded by % with the slug of
//the first taxonomy it belongs to.
return str_replace('%client%', $client, $post_link);
}
//If all else fails, just return the $post_link.
return $post_link;
}
add_filter('post_type_link', 'custom_post_link', 1, 3);
Then, when registering the Custom Type, set the rewrite arg like this:
'rewrite' => array('slug' => '%client%')
I guess I should have dug deeper before asking, but at least we have a complete solution now.