The reason you get 0 back is because of the test at the end of the function:
if ( ! empty( $query->posts ) && $query->is_singular )
return $query->post->ID;
else
return 0;
The posts page doesn’t pass the is_singular
test so 0 is returned.
If you have a URL and want to know if it’s the posts page, you can match it against the permalink returned from get_permalink
when passed the page_for_posts
option:
$page_for_posts_url = get_permalink( get_option( 'page_for_posts' ) );
if( $current_url == $page_for_posts_url ) echo 'this is the page for posts!';
Related Posts:
- disable Tab post on nav-menus page (Admin)
- How to display by default only published posts/pages in the admin area?
- Using single.php from plugin folder instead of default template folder
- delete post also attachments
- Static posts page with home.php
- Pagination not working on Custom Page Template
- Allow role to delete posts but block him the wp-admin
- Delete Post From Front-End and attachment permanently
- $post object is null
- When paginating a Page with the tag, how can the 2nd and subsequent page styles be customised?
- Is there any WordPress alternative to Book module of Drupal?
- Run a function when post is deleted?
- Posts vs. Pages
- Why content_arr[‘extended’] removes paragraph tags?
- Adding buttons to Add New Post and Add New Page
- Static posts page with home.php
- “about us”, ” contact” sections should be article(post) or page in the simple small Business website?
- Format latest/newest post differently
- is_page Funtion for Posts ?
- WordPress Query Posts From Category Post on Static Page
- WordPress 302 Redirect to Random Product/Post/Page
- the_content() Not Grabbing All Content
- Optimize CPT-function with a loop
- Organization of time insensitive content
- What would causes search to return incorrect results?
- Help on conditional statement to accompany wp_insert_post function please?
- Changing permalink of WordPress
- How to put the author of the post in the comments?
- Alt text attributes not showing over portfolio images
- WordPress Sticky Post Count “Fix” Breaking Pagecount by 72 pages!
- How to add a custom class attribute into code wrapper? [duplicate]
- Added a ACF field to custom page and want the results to show as a column in admin
- Show post count in custom taxonomy page
- Put page on Archived statut after end of publication date
- post and page and custom post type
- How do I set content in my pages so that it remains permanent while blog posts are added below?
- Permalink base only for posts
- Show only top 3 posts from 3 categories in order on home page
- Allow users to create their own page/s
- Make a custom URL and link to custom Template
- Automatically delete default posts and pages on theme install?
- How to have more post in a page than in your home page
- custom post type single page template not working
- Insert Shortcode exactly at the end of the content
- How to have 2 posting pages on a blog
- What function does the loop of displaying posts?
- Edit Page button is gone
- Parameter for pagination posts_pagination
- Making a wordpress page print friendly
- Page of posts made to look like homepage
- post/page content
- Link to older links
- How to link post titles on page A to its posts on page B?
- WordPress Architecture as a CMS – Posts & Pages
- Getting post and attached image from subdomain
- Is it to possible to insert a page in the post depending on the category ofthe post
- Custom post type slideshow background images to insert in a plugin
- Direct some posts to only appear on a specific page
- Adding if statement into the_content()
- How can I add a page that shows posts from a single category?
- How to show next/prev link, when it’s not active?
- Custom Select Query – Selecting the Year?
- Archieve page, Advertisement Every 3 Posts
- Deleting old posts
- How To Use Custom Tax of a Post Type
- How to output all taxonomy links from a custom post type in a menu?
- Prevent 404 of Author pages without posts
- Prevent duplicate pages from being added
- Can I style single post that are in multiple catergories?
- Trying to exclude custom posts based on date, while sorting by custom field
- Different post views for different category views
- WordPress Post Deletion Issue
- How Can I Always Display A Particular Post First Using WP_Query?
- Cannot publish posts, but can create new pages
- Making a new post visible on a specific page
- How to implement a WordPress comments function?
- MySQL: Possible to replace all of wp_posts.post_content(id#) via UPDATE + REPLACE + SELECT?
- How to add content above footer in posts from specific category
- Can I restore restore content from old Worpress site to a new Worpress site from an archive
- Hook save_post_cpt not triggered when custom fields are modified
- Change modified date to current date when title updated automatically
- Set Default Post as Parent Child (Nested) Relation
- Separate blog and reviews categories
- Creating a redirect option for current page url
- Indenting within a blockquote
- How Do I Redirect WordPress Pages but not posts?
- I want to add another search box on posts page in dashboard
- I need to create “View Profile” and “Edit Profile” links by incorporating the Post Title/ID, but can’t seem to manage it
- Turn off display for custom sidebar post widget when on the post it’s displaying
- Permanently deleted posts still exist
- Create a List of Widgets, where each widget holds three attributes
- Edit post & page option does not display on latest wordpress
- Automatic duplicate/copy of post to a related page in WordPress
- Change published date and time using SQL query for page in every 5 hour
- how to show comments only author which send own posts in wordpress
- Is there a way to find all posts with Youtube video embedded?
- Custom Fields – How to get the list of a specific active widget each time it rendered
- get category in list of posts // shortcode for custom related posts
- shortcode // get posts by ids
- get posts, run a custom query, and join the results