modify loop to exclude posts which belong to a certain category AND no other categories
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
add a comment |
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
add a comment |
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
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
wordpress
edited Nov 16 '18 at 4:31
WillD
asked Nov 14 '18 at 21:09
WillDWillD
464314
464314
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
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' );
add a comment |
I would assume that including all category y, will include all posts contained in x and also in y.
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
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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' );
add a comment |
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' );
add a comment |
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' );
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' );
edited Nov 15 '18 at 17:01
answered Nov 15 '18 at 4:59
WillDWillD
464314
464314
add a comment |
add a comment |
I would assume that including all category y, will include all posts contained in x and also in y.
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
add a comment |
I would assume that including all category y, will include all posts contained in x and also in y.
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
add a comment |
I would assume that including all category y, will include all posts contained in x and also in y.
I would assume that including all category y, will include all posts contained in x and also in y.
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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