modify loop to exclude posts which belong to a certain category AND no other categories












0















In Wordpress:



I have a need to exclude a certain category of posts from the main WP loop. But only if that post has no other categories.



For example, if the category in question was called X...



Test 1: A post which belongs to only X category would be excluded.



Test 2: A post which belongs to X and Y categories would be included.



Test 3: Posts which belong to Z or Foo or anything else would also be included.



I have the following code so far:



function exclude_category( $query ) {
if ( $query->is_home() && $query->is_main_query() ) {
$query->set( 'cat', '-271' );
}
}

add_action( 'pre_get_posts', 'exclude_category' );


But this works for test 1 and 3 (above) but not test 2. How can I modify the query to achieve this?










share|improve this question





























    0















    In Wordpress:



    I have a need to exclude a certain category of posts from the main WP loop. But only if that post has no other categories.



    For example, if the category in question was called X...



    Test 1: A post which belongs to only X category would be excluded.



    Test 2: A post which belongs to X and Y categories would be included.



    Test 3: Posts which belong to Z or Foo or anything else would also be included.



    I have the following code so far:



    function exclude_category( $query ) {
    if ( $query->is_home() && $query->is_main_query() ) {
    $query->set( 'cat', '-271' );
    }
    }

    add_action( 'pre_get_posts', 'exclude_category' );


    But this works for test 1 and 3 (above) but not test 2. How can I modify the query to achieve this?










    share|improve this question



























      0












      0








      0








      In Wordpress:



      I have a need to exclude a certain category of posts from the main WP loop. But only if that post has no other categories.



      For example, if the category in question was called X...



      Test 1: A post which belongs to only X category would be excluded.



      Test 2: A post which belongs to X and Y categories would be included.



      Test 3: Posts which belong to Z or Foo or anything else would also be included.



      I have the following code so far:



      function exclude_category( $query ) {
      if ( $query->is_home() && $query->is_main_query() ) {
      $query->set( 'cat', '-271' );
      }
      }

      add_action( 'pre_get_posts', 'exclude_category' );


      But this works for test 1 and 3 (above) but not test 2. How can I modify the query to achieve this?










      share|improve this question
















      In Wordpress:



      I have a need to exclude a certain category of posts from the main WP loop. But only if that post has no other categories.



      For example, if the category in question was called X...



      Test 1: A post which belongs to only X category would be excluded.



      Test 2: A post which belongs to X and Y categories would be included.



      Test 3: Posts which belong to Z or Foo or anything else would also be included.



      I have the following code so far:



      function exclude_category( $query ) {
      if ( $query->is_home() && $query->is_main_query() ) {
      $query->set( 'cat', '-271' );
      }
      }

      add_action( 'pre_get_posts', 'exclude_category' );


      But this works for test 1 and 3 (above) but not test 2. How can I modify the query to achieve this?







      wordpress






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 16 '18 at 4:31







      WillD

















      asked Nov 14 '18 at 21:09









      WillDWillD

      464314




      464314
























          2 Answers
          2






          active

          oldest

          votes


















          0














          I thought up a way to do this, though I am not sure it is the best way.



          Basically I am asking WP for a list of all categories then I am concatenating a list of those categories minus the one which I want to exclude. I then feed that into the 'cat' parameter in the WP Query. It seems to work as I would hope, but it also seems a little round-about. I would appreciate any feedback.



          In this case 271 is the id for my x category.



          function exclude_category( $query ) {

          $terms = get_terms( array(
          'taxonomy' => 'category',
          'hide_empty' => false,
          ) );

          $catquery = "";
          foreach($terms as $term) {
          if($term->term_id != 271) {
          $catquery .= $term->term_id . ",";
          }
          }

          $catquery = substr($catquery, 0, -1);

          if ( $query->is_home() && $query->is_main_query() ) {
          $query->set( 'cat', $catquery );
          }
          }

          add_action( 'pre_get_posts', 'exclude_category' );


          EDIT



          I ended up changing it to a taxonomy query so I could set include_children to false.



          function exclude_category( $query ) {

          $terms = get_terms( array(
          'taxonomy' => 'category',
          'hide_empty' => false,
          ) );

          $catarray = ;
          foreach($terms as $term) {
          if($term->term_id != 271) {
          array_push($catarray, $term->term_id);
          }
          }

          if ( $query->is_main_query() && $query->is_home()) {
          $query->set( 'tax_query', array( array(
          'taxonomy' => 'category',
          'field' => 'term_id',
          'terms' => $catarray,
          'include_children' => false
          )));
          }
          }

          add_action( 'pre_get_posts', 'exclude_category' );





          share|improve this answer

































            -1














            I would assume that including all category y, will include all posts contained in x and also in y.






            share|improve this answer
























            • you gave a case scenario, i gave the solution to eliminate all posts in category x, because you don't understand, doesn't mean its incorrect, guess you were expecting me to write your code for you

              – user4497766
              Nov 16 '18 at 3:37











            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53308758%2fmodify-loop-to-exclude-posts-which-belong-to-a-certain-category-and-no-other-cat%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            I thought up a way to do this, though I am not sure it is the best way.



            Basically I am asking WP for a list of all categories then I am concatenating a list of those categories minus the one which I want to exclude. I then feed that into the 'cat' parameter in the WP Query. It seems to work as I would hope, but it also seems a little round-about. I would appreciate any feedback.



            In this case 271 is the id for my x category.



            function exclude_category( $query ) {

            $terms = get_terms( array(
            'taxonomy' => 'category',
            'hide_empty' => false,
            ) );

            $catquery = "";
            foreach($terms as $term) {
            if($term->term_id != 271) {
            $catquery .= $term->term_id . ",";
            }
            }

            $catquery = substr($catquery, 0, -1);

            if ( $query->is_home() && $query->is_main_query() ) {
            $query->set( 'cat', $catquery );
            }
            }

            add_action( 'pre_get_posts', 'exclude_category' );


            EDIT



            I ended up changing it to a taxonomy query so I could set include_children to false.



            function exclude_category( $query ) {

            $terms = get_terms( array(
            'taxonomy' => 'category',
            'hide_empty' => false,
            ) );

            $catarray = ;
            foreach($terms as $term) {
            if($term->term_id != 271) {
            array_push($catarray, $term->term_id);
            }
            }

            if ( $query->is_main_query() && $query->is_home()) {
            $query->set( 'tax_query', array( array(
            'taxonomy' => 'category',
            'field' => 'term_id',
            'terms' => $catarray,
            'include_children' => false
            )));
            }
            }

            add_action( 'pre_get_posts', 'exclude_category' );





            share|improve this answer






























              0














              I thought up a way to do this, though I am not sure it is the best way.



              Basically I am asking WP for a list of all categories then I am concatenating a list of those categories minus the one which I want to exclude. I then feed that into the 'cat' parameter in the WP Query. It seems to work as I would hope, but it also seems a little round-about. I would appreciate any feedback.



              In this case 271 is the id for my x category.



              function exclude_category( $query ) {

              $terms = get_terms( array(
              'taxonomy' => 'category',
              'hide_empty' => false,
              ) );

              $catquery = "";
              foreach($terms as $term) {
              if($term->term_id != 271) {
              $catquery .= $term->term_id . ",";
              }
              }

              $catquery = substr($catquery, 0, -1);

              if ( $query->is_home() && $query->is_main_query() ) {
              $query->set( 'cat', $catquery );
              }
              }

              add_action( 'pre_get_posts', 'exclude_category' );


              EDIT



              I ended up changing it to a taxonomy query so I could set include_children to false.



              function exclude_category( $query ) {

              $terms = get_terms( array(
              'taxonomy' => 'category',
              'hide_empty' => false,
              ) );

              $catarray = ;
              foreach($terms as $term) {
              if($term->term_id != 271) {
              array_push($catarray, $term->term_id);
              }
              }

              if ( $query->is_main_query() && $query->is_home()) {
              $query->set( 'tax_query', array( array(
              'taxonomy' => 'category',
              'field' => 'term_id',
              'terms' => $catarray,
              'include_children' => false
              )));
              }
              }

              add_action( 'pre_get_posts', 'exclude_category' );





              share|improve this answer




























                0












                0








                0







                I thought up a way to do this, though I am not sure it is the best way.



                Basically I am asking WP for a list of all categories then I am concatenating a list of those categories minus the one which I want to exclude. I then feed that into the 'cat' parameter in the WP Query. It seems to work as I would hope, but it also seems a little round-about. I would appreciate any feedback.



                In this case 271 is the id for my x category.



                function exclude_category( $query ) {

                $terms = get_terms( array(
                'taxonomy' => 'category',
                'hide_empty' => false,
                ) );

                $catquery = "";
                foreach($terms as $term) {
                if($term->term_id != 271) {
                $catquery .= $term->term_id . ",";
                }
                }

                $catquery = substr($catquery, 0, -1);

                if ( $query->is_home() && $query->is_main_query() ) {
                $query->set( 'cat', $catquery );
                }
                }

                add_action( 'pre_get_posts', 'exclude_category' );


                EDIT



                I ended up changing it to a taxonomy query so I could set include_children to false.



                function exclude_category( $query ) {

                $terms = get_terms( array(
                'taxonomy' => 'category',
                'hide_empty' => false,
                ) );

                $catarray = ;
                foreach($terms as $term) {
                if($term->term_id != 271) {
                array_push($catarray, $term->term_id);
                }
                }

                if ( $query->is_main_query() && $query->is_home()) {
                $query->set( 'tax_query', array( array(
                'taxonomy' => 'category',
                'field' => 'term_id',
                'terms' => $catarray,
                'include_children' => false
                )));
                }
                }

                add_action( 'pre_get_posts', 'exclude_category' );





                share|improve this answer















                I thought up a way to do this, though I am not sure it is the best way.



                Basically I am asking WP for a list of all categories then I am concatenating a list of those categories minus the one which I want to exclude. I then feed that into the 'cat' parameter in the WP Query. It seems to work as I would hope, but it also seems a little round-about. I would appreciate any feedback.



                In this case 271 is the id for my x category.



                function exclude_category( $query ) {

                $terms = get_terms( array(
                'taxonomy' => 'category',
                'hide_empty' => false,
                ) );

                $catquery = "";
                foreach($terms as $term) {
                if($term->term_id != 271) {
                $catquery .= $term->term_id . ",";
                }
                }

                $catquery = substr($catquery, 0, -1);

                if ( $query->is_home() && $query->is_main_query() ) {
                $query->set( 'cat', $catquery );
                }
                }

                add_action( 'pre_get_posts', 'exclude_category' );


                EDIT



                I ended up changing it to a taxonomy query so I could set include_children to false.



                function exclude_category( $query ) {

                $terms = get_terms( array(
                'taxonomy' => 'category',
                'hide_empty' => false,
                ) );

                $catarray = ;
                foreach($terms as $term) {
                if($term->term_id != 271) {
                array_push($catarray, $term->term_id);
                }
                }

                if ( $query->is_main_query() && $query->is_home()) {
                $query->set( 'tax_query', array( array(
                'taxonomy' => 'category',
                'field' => 'term_id',
                'terms' => $catarray,
                'include_children' => false
                )));
                }
                }

                add_action( 'pre_get_posts', 'exclude_category' );






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 15 '18 at 17:01

























                answered Nov 15 '18 at 4:59









                WillDWillD

                464314




                464314

























                    -1














                    I would assume that including all category y, will include all posts contained in x and also in y.






                    share|improve this answer
























                    • you gave a case scenario, i gave the solution to eliminate all posts in category x, because you don't understand, doesn't mean its incorrect, guess you were expecting me to write your code for you

                      – user4497766
                      Nov 16 '18 at 3:37
















                    -1














                    I would assume that including all category y, will include all posts contained in x and also in y.






                    share|improve this answer
























                    • you gave a case scenario, i gave the solution to eliminate all posts in category x, because you don't understand, doesn't mean its incorrect, guess you were expecting me to write your code for you

                      – user4497766
                      Nov 16 '18 at 3:37














                    -1












                    -1








                    -1







                    I would assume that including all category y, will include all posts contained in x and also in y.






                    share|improve this answer













                    I would assume that including all category y, will include all posts contained in x and also in y.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 14 '18 at 23:31









                    user4497766user4497766

                    12




                    12













                    • you gave a case scenario, i gave the solution to eliminate all posts in category x, because you don't understand, doesn't mean its incorrect, guess you were expecting me to write your code for you

                      – user4497766
                      Nov 16 '18 at 3:37



















                    • you gave a case scenario, i gave the solution to eliminate all posts in category x, because you don't understand, doesn't mean its incorrect, guess you were expecting me to write your code for you

                      – user4497766
                      Nov 16 '18 at 3:37

















                    you gave a case scenario, i gave the solution to eliminate all posts in category x, because you don't understand, doesn't mean its incorrect, guess you were expecting me to write your code for you

                    – user4497766
                    Nov 16 '18 at 3:37





                    you gave a case scenario, i gave the solution to eliminate all posts in category x, because you don't understand, doesn't mean its incorrect, guess you were expecting me to write your code for you

                    – user4497766
                    Nov 16 '18 at 3:37


















                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53308758%2fmodify-loop-to-exclude-posts-which-belong-to-a-certain-category-and-no-other-cat%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    這個網誌中的熱門文章

                    Post-Redirect-Get with Spring WebFlux and Thymeleaf

                    Xamarin.form Move up view when keyboard appear

                    JBPM : POST request for execute process go wrong