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.

I did not find any useful tips from google search and I wrote the same question in wordpress forum but there is no reply

I am writing the complete step here

First step I am redirect page to custom post archive page

add_action( 'template_redirect', 'plu_redirects' ); //I am saving in the session the post_id which is the event $_SESSION['event_page'] = $_GET[ 'event' ];

wp_safe_redirect( get_post_type_archive_link('plug') );

In the archive page I am trying to get the post by id or if session not exists then get the latest post.

    if ( !isset($_SESSION['event_page'])) :
        $args = array(
            'post_type'=> 'plug',
            'posts_per_page' => 1,
            'order'    => 'ASC',
            'tax_query' => array(
                array(
                    'taxonomy' => 'event_type',
                    'field' => 'slug',
                    'terms' => 'schedule'
                )
            )
        );
        query_posts($args);
    else :
        $args = array(
            'post_type'=> 'plug',
            'posts_per_page' => 1,
                                'p' => $_SESSION['event_page'],
            'order'    => 'ASC',
            'tax_query' => array(
                array(
                    'taxonomy' => 'event_type',
                    'field' => 'slug',
                    'terms' => 'schedule'
                )
            )
        );
        query_posts($args);         endif;

But this is always getting the last record when session is set ...Why? I need to is there a better way it code can be written instead of using the session and also I don't require the loop as I am getting only one record. I am having the problem in the archive page and the code which I have added is working . but not professional..

share|improve this question
    
You'll probably get a lot more help if you show what research you have done so far, what you have tried, what that got you instead of what you were aiming for. Right now your question is pretty vauge and looks similar to "Please do all my work for me for free." –  helgatheviking Mar 30 '13 at 17:30
    
I did not know about the query_vars.. may be it works instead of using the session.. thanks –  B L Praveen Jul 21 '13 at 12:20

1 Answer 1

up vote 0 down vote accepted

Instead of using $_SESSION variables we'll register a query variable.

function wpa_91718_query_var( $vars ) {
    $vars[] = 'event_page';
    return $vars;
}
add_filter( 'query_vars', 'wpa_91718_query_var' );

Then we can modify the query before it is run using pre_get_posts.

If I understood you and your code correctly, then if the query var is present in the URL, you want to search for the post with that ID. Otherwise, you just want to show the most recent CPT with the 'schedule' term in the 'event_type' taxonomy.

I think this can be done via:

function wpa_91718_pre_get_posts( $query ) {
    // only modify the main query and only on the front-end
    if ( !is_admin() && $query->is_main_query() ) {

    if ( is_post_type_archive( 'plug' ) ) {
      // Display only 1 post for the cpt archive
      set_query_var( 'posts_per_page', 1 );

      // if query var exists search for that particular post ID number
      if ( $event_page = get_query_var( 'event_page' ) ){
        set_query_var( 'p', (int) $event_page );
      // otherwise get the most recent post of event_type taxonomy
      } else {
        set_query_var( 'event_type', 'schedule' );
      }
    }
  }
}
add_action( 'pre_get_posts','wpa_91718_pre_get_posts' );

Totally untested, but I think it should put you on the right path.

share|improve this answer
    
Thanks..But this was really old question..No body has replied..Yesterday somebody stunned me by voting down...So I commented to my questions. It can be done using a query vars..but as I was new to worpdress I did not know about query vars before.. –  B L Praveen Jul 22 '13 at 8:52
    
That downvote must've been why it was on the front page when I answered it. If the question is from this year, it must not even that old yet. If you get a chance to test this out, let me know if it works. –  helgatheviking Jul 22 '13 at 13:55

Your Answer

 
discard

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.