I have my blog posts set up so that it shows a blog roll of other posts at the bottom. I am trying to hide the current post that the user is on from the other posts as it would not make sense to appear there.
The problem I am having is that it is hiding the first blog post from the blog landing page also. I am using the following code in my functions.php file:
add_action( 'pre_get_posts', function ( $query ) {
// not an admin page and not the main query
if ( ! is_admin() && ! $query->is_main_query() ) {
$query->set( 'post__not_in', array( get_the_ID() ) );
}
} );
Related
I am trying to hide a few pages from WordPress Search results. For that, I have written a code.
function jp_search_filter( $query ) {
if ( ! $query->is_admin && $query->is_search && $query->is_main_query() ) {
$query->set( 'post__not_in', array( 10,11,20,105 ) );
}
}
add_action( 'pre_get_posts', 'jp_search_filter' );
However, this code doesn't seem to be working. The pages that I wanted to hide from WordPress search results are still showing. What I have done in the above code is I passed the page IDs as an array.
Can anyone help me with the code? Thank you so much.
somebody tried to fix pagination problem on my site by adding a function to your functions.php file[![screenshot - console][1]][1]
unfortunately, after updating wordpress or acf, this function does not work and when you try to go to the next page in the "atom" category, it displays 404 - Sorry, this page does not exist.
The pagination problem concerns only one category (subcategory). In the functions.php file I found a function like this:
function fix_atom_category_paged_query( $q ) {
if ( ! is_admin() && is_category( 'atom' ) && $_GET['debug'] == 1 ) {
$q->set( 'post_type', 'post' );
$q->set( 'posts_per_page', 9 );
// wp_die( var_dump( $q ) );
return $q;
}
}
add_action( 'pre_get_posts', 'fix_atom_category_paged_query', 2, 1 ); ```
[1]: https://i.stack.imgur.com/rKqzT.png
I suspect that the problem with the category called "atom" is due to the canonical link building and the wordpress core build itself. Names such as rss, feed, rss2, rdf, atom may conflict.
Ok, I tested it on another site. If category is called "atom", then wordpress pagination does not work for the archive of this category. I think this is WordPress problem.
I'm busy with this for two days now. I have the following setup in Wordpress:
Permalinks:
/%category%/%postname%/
Files (among others):
category-blog
category-podcast
category-ebooks
Created categories:
blog (standard)
podcast
ebooks
The links site.com/blog, site.com/podcast and site.com/ebooks work, but when I go to site.com/blog/page2/ the page can't be found. Same problem with the other category pages.
I can see the posts in the category, but can't access page 2 etc.
When I create a new post type it works, but not for the standard Wordpress posts. Also, I don't want to create a new post type but want to use categories.
What can I do?
You can try below code.
<?php
function paginated_category( $query ) {
if ( ! is_admin() && $query->is_main_query() ) {
if ( $query->is_category() ) {
$query->set( 'posts_per_page', 2 );
}
}
}
add_action( 'pre_get_posts', 'paginated_category' );
?>
I found this solution (after days of search) which seems to work:
Fix 404 errors
I am trying to create a series of pages that display the posts within a single category. To do this I use the following PHP code:
<?php
$args = array( 'category' => '$CATEGORY', 'numberposts' => 10000000000);
$myposts = get_posts( $args );
foreach($myposts as $post) :
setup_postdata($post);
?>
My problem is that the $CATEGORY does not seem to contain the category as a string. I have tried using both %s and $id but as I have not declared that it is the category id I am wanting it fails to work. The resulting output has been either an error or all the posts regardless of category.
What argument will convey the category string?
Below is a page illustrating the problem. This is a category page, meaning it should hold all the necessary info. If it was working it would only show the topmost post as it is the only post en site that has the "Press Release" category. Worth mentioning that I have another page just like it called "Dokument" and it displays the press release.
Page: http://www.skinwellness.se/category/pressrelease/
EDIT
I did not notice this before, but it seems that you are using the bundled theme twentythirteen. Simply delete the category.php from your child theme. If you have made changes to the parent theme directly, you should get a fresh copy of the theme, and create a child theme with all your modifications. Never make changes to a theme that you did not write. When such themes update, all you changes will be gone forever
You should then just need the pre_get_posts section in your child theme's functions.php to make everyone work
ORIGINAL ANSWER
You problem is purely your custom loop. As explained in the linked post, you should not be using custom queries in place of the main query on any type of archive page or on your home page
To solve this, revert back to the default loop. This is all you should have. No get_posts or foreach loops
if( have_posts() ) {
while( have_posts() ) {
the_post();
// Add your loop elements here like the_title() and the_content()
}
}
This should fix the problem that when you visit a category page, only the category been viewed will be viewed, no posts from other categories will be shown
Now, if you need to change anything on your category page, use pre_get_posts to do that. Make use of the is_category() conditional tag to target your category pages only. You can also target a specific category with this tag
Say for instance, you need to change the posts per page on you category page, as your case, display all posts with no pagination, you can do this in your functions.php
function so26589648_category_ppp( $query ) {
if ( !is_admin() && $query->is_category() && $query->is_main_query() ) {
$query->set( 'posts_per_page', '-1' );
}
}
add_action( 'pre_get_posts', 'so26589648_category_ppp' );
If you need to for example change the order to ASC and need to order posts by author, you can do this
function so26589648_category_ppp( $query ) {
if ( !is_admin() && $query->is_category() && $query->is_main_query() ) {
$query->set( 'posts_per_page', '-1' );
$query->set( 'order', 'ASC' );
$query->set( 'orderby', 'author' );
}
}
add_action( 'pre_get_posts', 'so26589648_category_ppp' );
You should see WP_Query for all available parameters to use
I have a made a custom post type called "Member Resources" the posts under this CPT have a few taxonomies such as categories and tags.
Tags = "Diversity" Categories = "Guidance"
When I go to the following urls:
www.domain.com/tags/diversity
www.domain.com/tags/guidance
No posts appear.
Though I have set public => true on the CPT function.
Posts are displaying if you go to the Member Resources archive page though, so they are displaying, but not when you filter them by taxonomies.
Update -
Adding the following code to my functions.php file allows the member-resources CPT to show in Category and Tags pages respectively, but now in the wordpress backend under the "Pages" tab and all other content tabs such as posts etc it seems to have overrided my pages and posts and is showing just the member-resources posts.
add_action( 'pre_get_posts', 'add_my_custom_post_type' );
function add_my_custom_post_type( $query ) {
if ($query->is_main_query())
$query->set( 'post_type', array( 'member-resources' ) );
return $query;
}
your code looks correct. but you are including the CPT member-resources in too many of wordpress's queries. the is_main_query means "the loop" i think.
so you need to restrict this to just running when on a tag archive page.
the following code is from the wordpress site
add_action( 'pre_get_posts', 'foo_modify_query_exclude_category' );
function foo_modify_query_exclude_category( $query ) {
if ( ! is_admin() && is_main_query() && ! $query->get( 'cat' ) )
$query->set( 'cat', '-5' );
}
You need to do a similar thing but determine if you are in a "tags" page.