change permalink of custom post type - wordpress

I create new post type and set 'rewrite' => false then for set new permalink I use:
// add to our plugin init function
global $wp_rewrite;
$dariche_structure = '/d/%post_id%/%dariche%';
$wp_rewrite->add_rewrite_tag("%dariche%", '([^/]+)', "dariche=");
$wp_rewrite->add_permastruct('dariche', $dariche_structure, false);
// Add filter to plugin init function
add_filter('post_type_link', 'dariche_permalink', 10, 3);
// Adapted from get_permalink function in wp-includes/link-template.php
function dariche_permalink($permalink, $post_id, $leavename) {
$post = get_post($post_id);
$rewritecode = array(
'%year%',
'%monthnum%',
'%day%',
'%hour%',
'%minute%',
'%second%',
$leavename? '' : '%postname%',
'%post_id%',
'%category%',
'%author%',
$leavename? '' : '%pagename%',
);
if ( '' != $permalink && !in_array($post->post_status, array('draft', 'pending', 'auto-draft')) ) {
$unixtime = strtotime($post->post_date);
$category = '';
if ( strpos($permalink, '%category%') !== false ) {
$cats = get_the_category($post->ID);
if ( $cats ) {
usort($cats, '_usort_terms_by_ID'); // order by ID
$category = $cats[0]->slug;
if ( $parent = $cats[0]->parent )
$category = get_category_parents($parent, false, '/', true) . $category;
}
// show default category in permalinks, without
// having to assign it explicitly
if ( empty($category) ) {
$default_category = get_category( get_option( 'default_category' ) );
$category = is_wp_error( $default_category ) ? '' : $default_category->slug;
}
}
$author = '';
if ( strpos($permalink, '%author%') !== false ) {
$authordata = get_userdata($post->post_author);
$author = $authordata->user_nicename;
}
$date = explode(" ",date('Y m d H i s', $unixtime));
$rewritereplace =
array(
$date[0],
$date[1],
$date[2],
$date[3],
$date[4],
$date[5],
$post->post_name,
$post->ID,
$category,
$author,
$post->post_name,
);
$permalink = str_replace($rewritecode, $rewritereplace, $permalink);
} else { // if they're not using the fancy permalink option
}
return $permalink;
}
My new post type name is dariche.
My default permalink for all post in setting is: article/%post_id%/%postname%/.
Now, my permalink of my new post type is d/%post_id%/%postname%/. But it doesn't work and just show 404 Error! What is problem in my code?

it seems you just want to change the base of the new post type permalink. you can achieve this by setting up rewrite. When you registering the post type.
'rewrite' => array(
'slug' => 'd',
'with_front' => false,
)

Related

get_post_types() is returning null

I am trying to retrieve the post types and put them into an array with their slug being the index and their label being the value. When I print_r( get_post_types() ); it returns the proper array with data, but when I try to use it like below, it returns null.
function get_posttype_list() {
$pt_list = [];
$post_types = get_post_types( array( 'public' => true ) );
foreach( $post_types as $pt ) {
$pt_list[ $pt->name ] = $pt->labels->singular_name;
}
}
Check the document here. You need to return your $pt_list array.
function get_posttype_list() {
$pt_list = array();
$post_types = get_post_types( array( 'public' => true ) );
foreach ( $post_types as $pt ) {
$pt_list[ $pt->name ] = $pt->labels->singular_name;
}
return $pt_list;
}

Wordpress add_filter use variable

I'm using Awesome Support and I want to add a filter to manually assign the agent to answer the tickets.
This is the funcition:
function wpas_find_agent( $ticket_id = false ) {
if ( defined( 'WPAS_DISABLE_AUTO_ASSIGN' ) && true === WPAS_DISABLE_AUTO_ASSIGN ) {
return apply_filters( 'wpas_find_available_agent', wpas_get_option( 'assignee_default' ), $ticket_id );
}
$users = shuffle_assoc( wpas_get_users( apply_filters( 'wpas_find_agent_get_users_args', array( 'cap' => 'edit_ticket' ) ) ) );
$agent = array();
foreach ( $users->members as $user ) {
$wpas_agent = new WPAS_Member_Agent( $user );
/**
* Make sure the user really is an agent and that he can currently be assigned
*/
if ( true !== $wpas_agent->is_agent() || false === $wpas_agent->can_be_assigned() ) {
continue;
}
$count = $wpas_agent->open_tickets(); // Total number of open tickets for this agent
if ( empty( $agent ) ) {
$agent = array(
'tickets' => $count,
'user_id' => $user->ID,
);
} else {
if ( $count < $agent['tickets'] ) {
$agent = array(
'tickets' => $count,
'user_id' => $user->ID,
);
}
}
}
if ( is_array( $agent ) && isset( $agent['user_id'] ) ) {
$agent_id = $agent['user_id'];
} else {
$default_id = wpas_get_option( 'assignee_default', 1 );
if ( empty( $default_id ) ) {
$default_id = 1;
}
$agent_id = $default_id;
}
return apply_filters( 'wpas_find_available_agent', (int) $agent_id, $ticket_id );
}
And this is the filter I want to add:
add_filter('wpas_find_available_agent', 'asignar_agente', 10, 2);
function asignar_agente($agent_id){
$term_list = wp_get_post_terms( $ticket_id, 'department', array( 'fields' => 'ids' ) );
if($term_list[0] == 34){
$agent_id = 2;
}else{
$agent_id = 3;
}
return $agent_id;
}
How can I pass the $ticket_id variable to the filter to use it?
I need this variable because I need to know the term (department taxonomy) of the ticket is being submitted.
Thank you.
Since you pass $ticket_id when you call apply_filters('wpas_find_available_agent'....); you will be able to get the $ticket_id if you change your filter function to.
function asignar_agente($agent_id, $ticket_id){ <--- just add this parameter here
...
}
this is possible since you pass the parameter in apply_filters and you say add_filter('wpas_find_available_agent', 'asignar_agente', 10, 2); with emphasis on the last parameter with the value 2, that means your filter function will be able to receive 2 parameters.

I want get_posts to return only posts of logged in user

Here's my code:
I'd like to only return posts of logged in user.
I am populating a dropdown form in WordPress gravity forms.
$current_user = _wp_get_current_user();
function populate_posts( $form) {
foreach ( $form['fields'] as &$field ) {
if ( $field->type != 'select' || strpos( $field->cssClass, 'populate-posts' ) === false ) {
continue;
}
$posts = get_posts( 'post_type=credit cards&numberposts=-1&post_status=publish&author=$current_user' );
$choices = array();
foreach ( $posts as $post ) {
$choices[] = array( 'text' => $post->post_title, 'value' => $post->post_title );
}
$field->placeholder = 'Select Credit Card';
$field->choices = $choices;
}
As i understand question of your's is to get "Post of Current User".Then here is a below code of it.
1) You need to pass author ID(Current User ID) as argument.
$current_user = wp_get_current_user();
$args = array(
'author' => $current_user->ID,
'orderby' => 'post_date',
'order' => 'ASC',
);
2) Pass above argument is post as below :
$current_user_posts = get_posts( $args );
I Hope this helps you.
You can also use WP_Query()
$user = wp_get_current_user();
$query_args = array(
'post_type' => 'post',
'author' => $user->ID,
);
$query = new WP_Query($query_args);
if ($query->have_posts()) {
while ($query->have_posts()) {
$query->the_post();
$post = $query->post;
echo $post->post_title;
}
wp_reset_postdata();
}
else{
echo 'No posts found.';
}

Permalink structure for my custom_post_type

I have a “gallery” as custom_post_type and “albums” as taxonomry_name
How can i achieve this structure :
mydomain.com/gallery/albums/{taxonomy_term}/{post}
I've tried something like the example below but it didn't work or perhaps i haven't used it properly
add_filter('rewrite_rules_array', 'mmp_rewrite_rules');
function mmp_rewrite_rules($rules) {
$newRules = array();
$newRules['basename/(.+)/(.+)/(.+)/?$'] = 'index.php?gallery=$matches[3]'; // my custom structure will always have the post name as the 4th uri segment
$newRules['basename/(.+)/?$'] = 'index.php?albums=$matches[1]';
return array_merge($newRules, $rules);
}
function filter_post_type_link($link, $post)
{
if ($post->post_type != 'gallery')
return $link;
if ($cats = get_the_terms($post->ID, 'albums'))
{
$link = str_replace('%albums%', get_taxonomy_parents(array_pop($cats)->term_id, 'albums', false, '/', true), $link); // see custom function defined below
}
return $link;
}
add_filter('post_type_link', 'filter_post_type_link', 10, 2);
function get_taxonomy_parents($id, $taxonomy, $link = false, $separator = '/', $nicename = false, $visited = array()) {
$chain = '';
$parent = &get_term($id, $taxonomy);
if (is_wp_error($parent)) {
return $parent;
}
if ($nicename)
$name = $parent -> slug;
else
$name = $parent -> name;
if ($parent -> parent && ($parent -> parent != $parent -> term_id) && !in_array($parent -> parent, $visited)) {
$visited[] = $parent -> parent;
$chain .= get_taxonomy_parents($parent -> parent, $taxonomy, $link, $separator, $nicename, $visited);
}
if ($link) {
// nothing, can't get this working :(
} else
$chain .= $name . $separator;
return $chain;
}
I solved my problem by using the code below:
// Add our custom permastructures for custom taxonomy and post
add_action( 'wp_loaded', 'add_album_permastructure' );
function add_album_permastructure() {
global $wp_rewrite;
add_permastruct( 'albums', 'gallery/%albums%', false );
add_permastruct( 'gallery', 'gallery/%albums%/%gallery%', false );
}
// Make sure that all links on the site, include the related texonomy terms
add_filter( 'post_type_link', 'gallery_permalinks', 10, 2 );
function gallery_permalinks( $permalink, $post ) {
if ( $post->post_type !== 'gallery' )
return $permalink;
$terms = get_the_terms( $post->ID, 'albums' );
if ( ! $terms )
return str_replace( '%albums%/', '', $permalink );
$post_terms = array();
foreach ( $terms as $term )
$post_terms[] = $term->slug;
return str_replace( '%albums%', implode( ',', $post_terms ) , $permalink );
}
// Make sure that all term links include their parents in the permalinks
add_filter( 'term_link', 'add_term_parents_to_permalinks', 10, 2 );
function add_term_parents_to_permalinks( $permalink, $term ) {
$term_parents = get_term_parents( $term );
foreach ( $term_parents as $term_parent )
$permlink = str_replace( $term->slug, $term_parent->slug . ',' . $term->slug, $permalink );
return $permlink;
}
// Helper function to get all parents of a term
function get_term_parents( $term, &$parents = array() ) {
$parent = get_term( $term->parent, $term->taxonomy );
if ( is_wp_error( $parent ) )
return $parents;
$parents[] = $parent;
if ( $parent->parent )
get_term_parents( $parent, $parents );
return $parents;
}
Added this to fucntions.php and then refreshed my permalink structure from the Settings -> Permalink

Custom Post Type and Taxonomy Permalink Rewrite in WordPress 3.0.1

I have a 'story' Custom Post Type and 'artist', writer' Taxonomies.
I need to set rewrite rules in the functions.php for the permalinks to look like this:
Artist (Taxonomy/Category):
http://www.example.com/isaac-deutscher
(/%artist%)
Writer (Taxonomy/Category):
http://www.example.com/jean-paul-sartre
(/%writer%)
Story (Custom Post Type):
http://www.example.com/issac-deutscher/jean-paul-sartre/the-cat-is-under-the-table
(/%artist%/%writer%/%story%)
I have tried some code I found in blogs without success, and can't figure out how to solve this.
I'm working in Wordpress 3.0.1
This does the trick for Story and Artist, but not for Writer:
add_action('init', 'custom_init');
add_filter('post_type_link', 'story_permalink', 10, 3);
function custom_init(){
$story = array(
'query_var' => true,
'rewrite' => false,
);
$artist = array(
'query_var' => true,
'rewrite' => true
);
$writer = array(
'query_var' => true,
'rewrite' => true
);
register_post_type('story', $story);
register_taxonomy('artist', 'story', $artist);
register_taxonomy('writer', 'story', $writer);
global $wp_rewrite;
$story_structure = '/%artist%/%writer%/%story%';
$wp_rewrite->add_rewrite_tag("%story%", '([^/]+)', "story=");
$wp_rewrite->add_permastruct('story', $story_structure, false);
}
function story_permalink($permalink, $post_id, $leavename){
$post = get_post($post_id);
$rewritecode = array(
'%artist%',
'%writer%',
$leavename? '' : '%postname%',
$leavename? '' : '%pagename%',
);
if('' != $permalink && !in_array($post->post_status, array('draft', 'pending', 'auto-draft'))){
if (strpos($permalink, '%artist%') !== FALSE){
$terms = wp_get_object_terms($post->ID, 'artist');
if (!is_wp_error($terms) && !empty($terms) && is_object($terms[0])) $artist = $terms[0]->slug;
else $artist = 'unassigned-artist';
}
if (strpos($permalink, '%writer%') !== FALSE){
$terms = wp_get_object_terms($post->ID, 'writer');
if (!is_wp_error($terms) && !empty($terms) && is_object($terms[0])) $writer = $terms[0]->slug;
else $writer = 'unassigned-writer';
}
$rewritereplace = array(
$artist,
$writer,
$post->post_name,
$post->post_name,
);
$permalink = str_replace($rewritecode, $rewritereplace, $permalink);
}
else{
}
return $permalink;
}
Hope it helps.

Resources