• Resolved taloweb

    (@taloweb)


    How can I have a section with the post of a particular custom post type?

    I show all post type in home with this:

    add_filter( 'pre_get_posts', 'my_get_posts' );
    function my_get_posts( $query ) {
     if (!is_admin())
     {
      $query->set( 'post_type', array( 'post', 'Video', 'attachment', 'page' ));
     }
     return $query;
    }

    but after this I can’t filter “Video”

    THANKS

Viewing 11 replies - 1 through 11 (of 11 total)
  • I am not sure exactly what your question is, but as a guess, I think you could use a global variable to hold the post types and set it just before the query. Your filter would look like this:

    add_filter( 'pre_get_posts', 'my_get_posts' );
    function my_get_posts( $query ) {
    global $post_types;
     if (!is_admin() && is_array($post_types))
     {
      $query->set( 'post_type', $post_types);
     }
     return $query;
    }

    Then, before the query in the home page, set $post_types to the full set of values:

    $post_types = array( 'post', 'Video', 'attachment', 'page' );

    And, where you just want ‘Video’, use:

    `$post_types = array(‘Video’);

    Thread Starter taloweb

    (@taloweb)

    this is not a bad idea !

    My problem is that I don’t want a dedicated page to list all videos, but I want to use /index.php?post_type=Video syntax.

    In this way if I use ‘pre_get_posts’ I can’t show only videos, if I remove this everthing works fine, but videos aren’t shown in home

    Sorry, but I still don’t understand exactly what you want to do. Do you want to use index.php for different types of post if called from different places?

    Can you please explain more?

    Hey,

    I’m sorry to jump in here but if you have the answer to my question I would be incredibly grateful.

    How do you add custom post types to searches?

    Thanks!

    -M

    Thread Starter taloweb

    (@taloweb)

    vtxyzzy, I have two needs: the first is to show in home all posts and custom posts, and the second need is to have a “place” where to show ONLY custom post type….

    dapro, I think you need only to edit your function.php and add these lines: (where ‘Video’ is your custom post type name)

    add_filter( 'pre_get_posts', 'my_get_posts' );
    function my_get_posts( $query ) {
     if (!is_admin())
     {
      if (is_search()){
      $query->set( 'post_type', array( 'post', 'Video', 'attachment', 'page' ));
    }
     }
     return $query;
    }

    OK, what is the ‘place’? You have said that you don’t want a dedicated page, so do you want to use index.php in two different ways?

    @ taloweb, Thanks I added the code above to my functions.php but it didn’t work. ??

    I thought that editing the search.php file would be were the code would go.

    Here is what I have.

    <div id="basicsearch">
            <form method="get" id="searchform" action="<?php bloginfo('url'); ?>/">
    			<fieldset>
                    <input type="text" value="<?php the_search_query(); ?>" name="s" id="s" />
                    <input type="image" src="<?php bloginfo('template_directory')?>/images/mag.jpg" id="go" alt="Search" title="Search" />
    			</fieldset>
            </form>
    </div>

    For instance I have a particular that I want added to the normal search box.

    @ chinmoy29 It doesn’t say how to add a custom post type to search but I appreciate the link.

    @ taloweb, here is what I am thinking. If you are able to pass a parameter to index.php, test for the parameter and use the global variable I described earlier. I don’t think you can use post_type because WP already uses that – use my_type instead: index.php/?my_type=Video. Then in index.php, use code like this:

    if (isset($_GET('my_type')) && $_GET('my_type') == 'Video') {
       $post_types = array('Video');
    } else {
       $post_types = array('post', 'Video', 'attachment', 'page');
    }
    Thread Starter taloweb

    (@taloweb)

    @vtxyzzy this is not a bad idea, I’ll use your code!

    If it works, please use the dropdown at top right to mark this topic ‘Resolved’ so that others researching a similar problem can see that there is a solution.

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘how to get a list of custom post type?’ is closed to new replies.