Wordpress custom Posttyp soll auch im Monats- und Jahresarchiv erscheinen

Maffy

Erfahrenes Mitglied
ich habe mir ein kleines Plugin erstellt um E-Books anzuzeigen.
Soweit funktioniert alles recht gut.

Das erste Problem ist, dass die Books im Adminbereich nicht nach aktuellem Datum sortiert angezeigt wird.
Ein weiteres Problem ist auch noch, dass die Books nicht im Monats Archiv angezeigt werden.

http://www.wandernimsaaletal.de/wanderfuehrer/

PHP:
<?php

/*
Plugin Name: My E-Books
Description: Flash Magazine als neuen Benutzer Post Typ anzeigen
Author: Matthias
Version: 0.0.2
Author URI: http://www.wandernimsaaletal.de/wanderfuehrer/
*/

/*  Copyright 2013  Matthias Reichert (email : matthias-reichert@mr-73.de)

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

add_action( 'init', 'create_post_type' );

function create_post_type() {
   
$labels = array(
'name' => 'E-Books Einträge',
'singular_name' => 'E-Books',
'menu_name' => 'E-Books',
'parent_item_colon' => '',
'all_items' => 'Alle Einträge',
'view_item' => 'Eintrag ansehen',
'add_new_item' => 'Neuer Eintrag',
'add_new' => 'Hinzufügen',
'edit_item' => 'Eintrag bearbeiten',
'update_item' => 'Update Eintrag',
'search_items' => 'E-Books suchen',
'not_found' => 'Kein E-Book gefunden',
'not_found_in_trash' => '',
);

$rewrite = array(
'slug' => 'book',
'with_front' => true,
'pages' => true,
'feeds' => true,
);

$args = array(
    'labels' => $labels,
     'public' => true, // Öffentlich zugänglich
    'publicly_queryable' => true,
    'show_ui' => true, // Im Administrationsbereich anzeigen
    'show_in_menu' => true, // Im Menü anzeigen
    'query_var' => true, // Für eigene Querys zugänglich machen
    'rewrite' => true,
    'capability_type' => 'post',
    'has_archive' => true,
    'hierarchical' => true,
    'menu_position' => null,
    'supports' => array('title','editor','thumbnail','excerpt','comments','customfields'),
    'taxonomies' => array( 'category', 'post_tag' ), // add default post categories and tags
    'public' => true,
    'rewrite' => array('slug' => 'custom')       
);

    register_post_type( 'book', $args );
    register_taxonomy_for_object_type( 'category', 'book' );   
   
}


// hook into the init action and call create_book_taxonomies when it fires
add_action( 'init', 'create_book_taxonomies', 0 );

// create two taxonomies, genres and writers for the post type "book"
function create_book_taxonomies() {

    // Add new taxonomy, make it hierarchical (like categories)
    $labels = array(
        'name'              => _x( 'Regionen', 'taxonomy general name' ),
        'singular_name'     => _x( 'Region', 'taxonomy singular name' ),
        'search_items'      => __( 'Search Region' ),
        'all_items'         => __( 'All Regionen' ),
        'parent_item'       => __( 'Parent Region' ),
        'parent_item_colon' => __( 'Parent Region:' ),
        'edit_item'         => __( 'Edit Region' ),
        'update_item'       => __( 'Update Region' ),
        'add_new_item'      => __( 'Add New Region' ),
        'new_item_name'     => __( 'New Region Name' ),
        'menu_name'         => __( 'Region' ),
    );

    $args = array(
        'hierarchical'      => true,
        'labels'            => $labels,
        'show_ui'           => true,
        'show_admin_column' => true,
        'query_var'         => true,
        'rewrite'           => array( 'slug' => 'region' ),
       
    );

    register_taxonomy( 'region', array( 'book' ), $args );

}

/*
# Artikel Suchen und anzeigen
*/

add_filter( 'pre_get_posts', 'my_get_posts' );

function my_get_posts( $query ) {

    if ( is_home() && $query->is_main_query() )
        $query->set( 'post_type', array( 'post', 'page', 'book', 'region' ) );

    return $query;
}


function add_custom_types_to_tax( $query ) {
if( is_category() || is_tag() && empty( $query->query_vars['suppress_filters'] ) ) {

// Get all your post types
$post_types = array( 'post', 'book' );

$query->set( 'post_type', $post_types );
return $query;
}
}
add_filter( 'pre_get_posts', 'add_custom_types_to_tax' );

/*
* Add Flash Magazine Metaboxes to book post-type
*
* http://codex.wordpress.org/Function_Reference/add_meta_box
* http://blogs.sitepoint.com/guide-to-wordpress-custom-write-panels/
*/

/**
* Adds a box to the main column on the Book edit screens.
*/
function book_add_custom_box() {

    $screens = array( 'book' );

    foreach ( $screens as $screen ) {

        add_meta_box(
            'book_sectionid',
            __( 'Flash Magazine Daten:', 'book_textdomain' ),
            'book_inner_custom_box',
            $screen
        );
    }
}
add_action( 'add_meta_boxes', 'book_add_custom_box' );

/**
* Prints the box content.
*
* @param WP_Post $post The object for the current post/page.
*/
function book_inner_custom_box( $post ) {

  // Add an nonce field so we can check for it later.
  wp_nonce_field( 'book_inner_custom_box', 'book_inner_custom_box_nonce' );

  /*
   * Use get_post_meta() to retrieve an existing value
   * from the database and use the value for the form.
   */
     
  $value1 = get_post_meta( $post->ID, '_my_meta_value_key', true );
  echo '<label><b>Flash Magazin Link:</b><label>';
  echo '<br><input type="text" id="flashmagazin" name="flashmagazin" value="' . esc_attr( $value1 ) . '" size="75" /><br>';
 
  $value2 = get_post_meta( $post->ID, 'pdfurl', true );
  echo '<br><label><b>PDF Dokument Link:</b><label><br>';
  echo '<input type="text" id="pdfurl" name="pdfurl" value="' . esc_attr( $value2 ) . '" size="75" /><br><br>';
  echo '<label><b>I-Frame Link:</b><label><br>';
  $value3 = get_post_meta( $post->ID, 'artikelurl', true ); 
  echo '<input type="text" id="artikelurl" name="artikelurl" value="' . esc_attr( $value3 ) . '" size="75" /><br><br>';   
       
}

/**
* When the post is saved, saves our custom data.
*
* @param int $post_id The ID of the post being saved.
*/
function book_save_postdata( $post_id ) {

  /*
   * We need to verify this came from the our screen and with proper authorization,
   * because save_post can be triggered at other times.
   */

  // Check if our nonce is set.
  if ( ! isset( $_POST['book_inner_custom_box_nonce'] ) )
    return $post_id;

  $nonce = $_POST['book_inner_custom_box_nonce'];

  // Verify that the nonce is valid.
  if ( ! wp_verify_nonce( $nonce, 'book_inner_custom_box' ) )
      return $post_id;

  // If this is an autosave, our form has not been submitted, so we don't want to do anything.
  if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
      return $post_id;

  // Check the user's permissions.
  if ( 'book' == $_POST['post_type'] ) {

    if ( ! current_user_can( 'edit_page', $post_id ) )
        return $post_id;
 
  } else {

    if ( ! current_user_can( 'edit_post', $post_id ) )
        return $post_id;
  }

  /* OK, its safe for us to save the data now. */

  // Sanitize user input.
  $mydata1 = sanitize_text_field( $_POST['flashmagazin'] );
  $mydata2 = sanitize_text_field( $_POST['pdfurl'] );
  $mydata3 = sanitize_text_field( $_POST['artikelurl'] );
  $mydata4 = sanitize_text_field( $_POST['_store'] );

  // Update the meta field in the database.
  update_post_meta( $post_id, '_my_meta_value_key', $mydata1 );

  // Update the meta field in the database.
  update_post_meta( $post_id, 'pdfurl', $mydata2 );

  // Update the meta field in the database.
  update_post_meta( $post_id, 'artikelurl', $mydata3 ); 
   
}
add_action( 'save_post', 'book_save_postdata' );

?>
 
Zurück