Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

this is my first public plugin called Sort posts in taxonomies. With the plugin I intend to list regular posts and custom post_types in the Category(Taxonomy) edit page in wp-admin. Further give the posibility to sort posts using numeric values or ajax.

This could be the layout of the content:

Genre - Taxonomy

Comedy - Category

  1. Escape Plan - Post

  2. Liar, Liar - Post

  3. Last Vegas - Post

Drama - Category

  1. Gravity - Post

  2. Escape Plan - Post

  3. Prisoners - Post

Notice that the movie Escape Plan is listed different in different taxonomies.

When I edit the Comedy category I want to list all the posts the category Comedy in that edit page.

What I have figured out so far is that I have to use the action {$taxonomy}_edit_form as the hook. But I don't know how to get the Taxonomy outside the hook without hardcoding the value. Any ideas?

This is my sort plugin (achd-sort-posts.php):

<?php
/**
 * Plugin Name: Sort posts in taxonomies
 * Version: 1.0
 * Author: Anthony H

global $wp_query;



// $taxonomy = get_query_var( $wp_query->query_vars['taxonomy'] );
// $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );

// $taxonomy = $wp_query->taxonomy ;
// This is the hardcoded taxonomy
    $taxonomy = 'genre';


add_action( "{$taxonomy}_edit_form", 'sort_term_post');


function sort_term_post ($wp_query) {


    $screen = get_current_screen();

    $args = array (
        'post_type' => "{$screen->post_type}",
        'tax_query' => array(
            array(
                'taxonomy'  => "{$screen->taxonomy}",
                'terms'     => "{$wp_query->term_id}"
            )
        )
    );

    $query = new WP_Query( $args );

    // The Loop
    if ( $query->have_posts() ) {
            echo '<ul>';
        while ( $query->have_posts() ) {
            $query->the_post();
            echo '<li>' . get_the_title() . '</li>';
        }
            echo '</ul>';
    } else {
        echo "No posts found";
    }
    /* Restore original Post Data */
    wp_reset_postdata();
}


?>

And here is the custom post_type and taxonomy that i have made (achd-movie.php):

<?php
/**
 * Plugin Name: Movie post type
 * Version: 1.0
 * Author: Anthony H
 */



add_action( 'init', 'create_post_type' );
function create_post_type() {
    register_post_type( 'achd_movie',
        array(
            'labels' => array(
                'name' => __( 'Movies' ),
                'singular_name' => __( 'Movies' )
            ),
        'public' => true,
        'has_archive' => true,
        'taxonomies' => array( 'genre' ),
        'rewrite' => array('slug' => 'movie'),
        )
    );

}

add_action( 'init', 'create_my_taxonomies', 0 );

function create_my_taxonomies() {
    register_taxonomy(
        'genre',
        'achd_movie',
        array(
            'labels' => array(
                'name' => 'Genres',
                'add_new_item' => 'Add new genre',
                'new_item_name' => "New genre"
            ),
            'show_ui' => true,
            'show_tagcloud' => false,
            'hierarchical' => true
        )
    );
}

add_filter( 'template_include', 'include_template_function', 1 );

function include_template_function( $template_path ) {
    if ( get_post_type() == 'achd_movie' ) {
        if ( is_single() ) {
            // checks if the file exists in the theme first,
            // otherwise serve the file from the plugin
            if ( $theme_file = locate_template( array ( 'single-achd_movie.php' ) ) ) {
                $template_path = $theme_file;
            } else {
                $template_path = plugin_dir_path( __FILE__ ) . '/single-achd_movie.php';
            }
        }
        elseif ( is_archive() ) {
            if ( $theme_file = locate_template( array ( 'archive-achd_movie.php' ) ) ) {
                $template_path = $theme_file;
            } else { $template_path = plugin_dir_path( __FILE__ ) . '/archive-achd_movie.php';

            }
        }
    }
    return $template_path;
        flush_rewrite_rules();
}


?>

My problem

I can't get the taxonomy to be sent to the action hook. Without hardcoding it. How can I retrieve it? Any ideas?

My end goal

To create a plugin that will list posts in a category edit page and have the ability to sort the posts for that category. Maybe with a small textarea and later with ajax. The sorting is category specific so if the post exists in two categories it can have different sorting in each category.

The thought is to add a column perhaps called "term_post_order" to the "wp_term_relationships" table in wordpress database or use the already existing "term_order" but it seems that column already is used for some other functions.

Is there any existing function to get data from wp_term_relationshoips ?

share|improve this question
    
Have you registered 'productcategory' as a taxonomy? You introduce as if you're working on the 'category' taxonomy, then give an example with the 'genre' category, then your code is using the 'productcategory' taxonomy. –  Bendoh Feb 24 at 17:35
    
I am working on the Genre taxonomy but I want the plugin to be dynamic and work for both. I updated the question with more text and code now. –  Anthony H Feb 27 at 12:30

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.