WordPress.org

Ready to get started?Download WordPress

Forums

WordPress - Using index.php for post and custom post type (2 posts)

  1. Khealdor
    Member
    Posted 1 year ago #

    I need to use my index.php file for my standard wordpress news, but also for custom post type.

    In my index.php file I use a standard loop with no arguments or parameters.

    I am trying to make it work with the action "pre_get_posts". I have been trying to change the query post type, but still doesn't work.

    $query->set('post_type', 'any')

    So I need a way for /dossiers/ to use index.php which mean I need to change the query of the loop when /dossiers/ is detected in the url.

    Thanks alot!

    Edit:

    Here is the function I call with "pre_get_post":

    function custom_post_types( $query ) {
    	    if( $query->is_main_query() && $query->query['name'] == 'dossiers' ) {
    		    $query->set('post_type', 'any');
    
    		    var_dump($query);
    	    }
        }

    And here is the var_dump of $query:

    object(WP_Query)#100 (42) {
          ["query"]=>
          array(2) {
          ["page"]=>
          string(0) ""
          ["name"]=>
          string(8) "dossiers"
        }
        ["query_vars"]=>
        array(46) {
          ["page"]=>
          string(0) ""
          ["name"]=>
          string(8) "dossiers"
          ["error"]=>
          string(0) ""
          ["m"]=>
          int(0)
          ["p"]=>
          int(0)
          ["post_parent"]=>
          string(0) ""
          ["subpost"]=>
          string(0) ""
          ["subpost_id"]=>
          string(0) ""
          ["attachment"]=>
          string(0) ""
          ["attachment_id"]=>
          int(0)
          ["static"]=>
          string(0) ""
          ["pagename"]=>
          string(0) ""
          ["page_id"]=>
          int(0)
          ["second"]=>
          string(0) ""
          ["minute"]=>
          string(0) ""
          ["hour"]=>
          string(0) ""
          ["day"]=>
          int(0)
          ["monthnum"]=>
          int(0)
          ["year"]=>
          int(0)
          ["w"]=>
          int(0)
          ["category_name"]=>
          string(0) ""
          ["tag"]=>
          string(0) ""
          ["cat"]=>
          string(0) ""
          ["tag_id"]=>
          string(0) ""
          ["author_name"]=>
          string(0) ""
          ["feed"]=>
          string(0) ""
          ["tb"]=>
          string(0) ""
          ["paged"]=>
          int(0)
          ["comments_popup"]=>
          string(0) ""
          ["meta_key"]=>
          string(0) ""
          ["meta_value"]=>
          string(0) ""
          ["preview"]=>
          string(0) ""
          ["s"]=>
          string(0) ""
          ["sentence"]=>
          string(0) ""
          ["fields"]=>
          string(0) ""
          ["category__in"]=>
            array(0) {
          }
          ["category__not_in"]=>
            array(0) {
          }
          ["category__and"]=>
            array(0) {
          }
          ["post__in"]=>
            array(0) {
          }
          ["post__not_in"]=>
            array(0) {
          }
          ["tag__in"]=>
            array(0) {
          }
          ["tag__not_in"]=>
            array(0) {
          }
          ["tag__and"]=>
            array(0) {
          }
          ["tag_slug__in"]=>
            array(0) {
          }
          ["tag_slug__and"]=>
            array(0) {
          }
          ["post_type"]=>
            string(3) "any"
        }
          ["tax_query"]=>
          NULL
          ["meta_query"]=>
          bool(false)
          ["post_count"]=>
          int(0)
          ["current_post"]=>
          int(-1)
          ["in_the_loop"]=>
          bool(false)
          ["comment_count"]=>
          int(0)
          ["current_comment"]=>
          int(-1)
          ["found_posts"]=>
          int(0)
          ["max_num_pages"]=>
          int(0)
          ["max_num_comment_pages"]=>
          int(0)
          ["is_single"]=>
          bool(true)
          ["is_preview"]=>
          bool(false)
          ["is_page"]=>
          bool(false)
          ["is_archive"]=>
          bool(false)
          ["is_date"]=>
          bool(false)
          ["is_year"]=>
          bool(false)
          ["is_month"]=>
          bool(false)
          ["is_day"]=>
          bool(false)
          ["is_time"]=>
          bool(false)
          ["is_author"]=>
          bool(false)
          ["is_category"]=>
          bool(false)
          ["is_tag"]=>
          bool(false)
          ["is_tax"]=>
          bool(false)
          ["is_search"]=>
          bool(false)
          ["is_feed"]=>
          bool(false)
          ["is_comment_feed"]=>
          bool(false)
          ["is_trackback"]=>
          bool(false)
          ["is_home"]=>
          bool(false)
          ["is_404"]=>
          bool(false)
          ["is_comments_popup"]=>
          bool(false)
          ["is_paged"]=>
          bool(false)
          ["is_admin"]=>
          bool(false)
          ["is_attachment"]=>
          bool(false)
          ["is_singular"]=>
          bool(true)
          ["is_robots"]=>
          bool(false)
          ["is_posts_page"]=>
          bool(false)
          ["is_post_type_archive"]=>
          bool(false)
          ["query_vars_hash"]=>
          string(32) "3cd80adcb57d626df6535f6dafb98057"
          ["query_vars_changed"]=>
          bool(false)
          ["thumbnails_cached"]=>
          bool(false)
        }

    The value for "name" is set to "dossiers", which is supposed to be the slug for a post name, if I set it to "", I get redirected to single.php with my last custom post type.

  2. 360
    Member
    Posted 1 year ago #

    the template hirarchy will help here:

    archive-{post_type}.php - If the post type were product, WordPress would look for archive-product.php.

    http://codex.wordpress.org/Template_Hierarchy

Topic Closed

This topic has been closed to new replies.

About this Topic