0

Im trying to set up a taxonomy to hide a post_format on a particular page.

I have created the below which stops that post_format from displaying on the page but it also hides it in the 'Posts' page on the CMS. Is there a way to get around this?

    function exclude_campaigns( $query ) {
        if( $query->is_main_query() && $query->is_home() ) {
            $tax_query = array( array(
                'taxonomy' => 'post_format',
                'field' => 'slug',
                'terms' => array( 'post-format-link' ),
                'operator' => 'NOT IN',
            ) );
            $query->set( 'tax_query', $tax_query );
        }
    }
    add_action( 'pre_get_posts', 'exclude_campaigns' );

Updated function:

// Exclude post-format from one particular page
function exclude_campaigns( $query ) {
    // is_page() allows for page ID, page title or page slug
    if( $query->is_main_query() && $query->is_page(239) ) {
        $tax_query = array( array(
            'taxonomy' => 'post_format',
            'field' => 'slug',
            'terms' => array( 'post-format-link' ),
            'operator' => 'NOT IN',
        ) );
        $query->set( 'tax_query', $tax_query );
    }
}
add_action( 'pre_get_posts', 'exclude_campaigns' );
  • 1
    You're applying this action on the main WP query and on the home page. The home page would includes posts if you've set your latest posts to be displayed on the front page via your "Settings > Reading" options. Are you wanting the post-format to not appear on ANY pages, or just one particular page? – Courtney Ivey Oct 20 '14 at 23:45
  • Thanks for the reply, yeah Im trying to hide this post-format on just one particular page. – adam Oct 20 '14 at 23:51
  • "hides it in the 'Posts' page on the CMS" What do you mean by this. – Pieter Goosen Oct 21 '14 at 4:26
  • In the posts page it doesnt display any post with this post-format – adam Oct 21 '14 at 9:45
  • Backend or front end – Pieter Goosen Oct 21 '14 at 18:59
1

If you're trying to hide a post-format on one particular page, then the function below should work.

<?php
// Exclude post-format from one particular page
function exclude_campaigns( $query ) {
    // is_page() allows for page ID, page title or page slug
    if( $query->is_main_query() && $query->is_page( INSERT_PAGE_ID_HERE ) ) {
        $tax_query = array(
            'taxonomy' => 'post_format',
            'field' => 'slug',
            'terms' => array( 'post-format-link' ),
            'operator' => 'NOT IN',
        );
        $query->set( 'tax_query', $tax_query );
}
}
add_action( 'pre_get_posts', 'exclude_campaigns' );
  • When I put the page id in Im getting this error: Trying to get property of non-object in C:\wamp\www\wordpress-btb\wp-includes\query.php on line 4337 Call Stack – adam Oct 21 '14 at 0:09
  • @adam The you are accessing it wrong. Update your question with your new code - keep in mind that you have a PHP syntax error and that you might want to read about objects in PHP in general. – kaiser Oct 21 '14 at 0:24
  • Oh right ok thanks for the reply but how I am accessing it wrong? – adam Oct 21 '14 at 0:36
1

pre_get_posts alters all queries, back-end and front-end, new instances of WP_Query and the default main query. You should use pre_get_posts in conjunction with the conditional tags.

The most important conditional tags here are:

  • is_main_query() -> This is used to target the main query only, excludes all custom instances of WP_Query

  • is_admin() -> Very important, with this conditional you can target only back-end queries. For this purpose, you are going to need the negative !is_admin() as you need to target the frond-end, not the back-end (admin side)

The rest of the conditionals can be used to target specific pages. You need to check what you need and apply it as needed.

if( $query->is_main_query() && $query->is_home() ) {

should become

if( !is_admin() && $query->is_main_query() && $query->is_home() ) {

which will only apply your changes to the front-end main query on the home page

I do believe that is_page() is inappropriate here. You will need to check what kind of page this is and then use the correct conditional tag accordingly. For instance, if this is the home page, use is_home(), or if it is a category page, use is_category().

  • Thanks a lot Pieter for the detailed reply, but using "if( !is_admin() && $query->is_main_query() && $query->is_home() ){" does not hide the post-format on the page. I even tried removing " && $query->is_home()" and that doesnt work either! I'm completely stumped – adam Oct 21 '14 at 20:25
  • On which page is this exactly. Is this an archive type page like a taxonomy or category page or maybe a home page or static front page. Are you sure that the query you are targeting is actually the main query – Pieter Goosen Oct 22 '14 at 4:27

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.