0

I have a custom post type (from a theme installed by previous developer), and here is the code of loop which retrieve the post and display them.

<?php
// Counter
$post_count = 1;

// Query
$gp_query_args = array(
    'post_type'         => 'event',

    //EXCLUDE Events try by me
    'post__not_in' => array('1646'),
    // exclude event try code block ends

    'meta_key'          => 'gp_event_date',
    'meta_value'        => date('Y/m/d'),
    'meta_compare'      => '>=',
    'orderby'           => 'meta_value',
    'order'             => 'ASC',
    'posts_per_page'    => -1

);
$gp_query = NULL;
$gp_query = new WP_Query($gp_query_args);

I want to exclude certain events (post of custom post type, from loop), and as you can see i tried using 'post__not_in' => array('1646'), where 1646 is the post id i see when i edit the custom post . So i guess this is universal post id, not custom post type specific id. But No Matter, the post is not being excluded :-(

Am i doing wrong??

Edit1:

I found a [repository on github](links below edit3 ) that is exactly similar to my theme structure. I don't know the theme name, so i'm not sure whether that's the same theme on the site i'm working over.

Edit2

So as per the suggestions below

I did, renamed template-event.php (my template file for showing list of events ) to archive-event.php

And placed the function snippet from here in function.php of my theme.

But i cannot exclude the post with specified ID yet.

Edit3

As asked here is the link of the github repository that is exactly same as my theme.

Github Links:

  • Add your CPT code as an EDIT above. – Brad Dalton Feb 7 '15 at 14:47
  • Hi added the github links. By CPT code, you mean the code registering the post type right?? (posttype-event.php) – echoashu Feb 7 '15 at 14:57
  • The problem is with your code. Add this parameter 'has_archive' => true, – Brad Dalton Feb 7 '15 at 15:09
  • If you EVER republish anyone else's work, always include a link as per SE policy blog.stackoverflow.com/2009/06/attribution-required – Brad Dalton Feb 7 '15 at 15:24
  • I cannot reproduce your issue. I had a look at your template-event.php, and the alarming thing is that it uses query_posts which you should never ever use. You should change that. Also, echo the SQL request (echo $gp_query->request) and check if the SQL is correctly build, and make double sure your ID is correct – Pieter Goosen Feb 7 '15 at 16:23
0

Something like this should work from your functions file as long as you use the correct conditional tag for your CPT archive:

function exclude_single_post_cpt($query) {
  if (is_post_type_archive('event') && $query->is_main_query() && !is_admin() ) {
    $query->set('post__not_in', array(1646));
  }
}

add_action('pre_get_posts', 'exclude_single_post_cpt');
  • well yes. my cpt is 'event'. still this code dosn't work :-( I added the function snippt at the end of my function.php thanks – echoashu Feb 7 '15 at 14:17
  • Exclude from CPT archive loop or home page ( posts page )? – Brad Dalton Feb 7 '15 at 14:19
  • exclude from the page that shows list of posts of the type = event. The template file name is - template-event.php The structure and code is exactly same as the given github link. regards – echoashu Feb 7 '15 at 14:21
  • i just read the codex, i guess my case isn't for archive. Thats a simple template just like the homepage template which queries post of the cpt type, an display a list of them. Ur thoughts?? – echoashu Feb 7 '15 at 14:25
  • Yes, it should be archive-event.php, so the code works but the conditional doesn't because your file name is wrong. Change the file name and the code will work or add another file using the correct name according to the WordPress Template Hierarchy. – Brad Dalton Feb 7 '15 at 14:26
-1

You should rename your file archive-event.php instead of template-event.php as per the WordPress Theme Hierarchy. The template-event.php (template-customposttypename.php) file used for display the content of single events as the single.php working to display the content of post content.

You can use your above code by using any page template also. I hope it will be work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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