For example if there are 10 custom posts, and No. 1 is the oldest, and
they happened to select the 3rd oldest one, they want it to say “No.
3”.
There is a more elegant way than that function:
$args = array(
'post_type' => 'book',
'fields' => 'ids',
'ignore_sticky_posts' => true,
'orderby' => 'post_date',
'order' => 'DESC'
);
$posts = new WP_Query($args);
// var_dump($posts->posts);
$index = array_search(300,$posts->posts);
if (!empty($index)) {
echo $index + 1;
} else {
echo 'not found';
}
It is hardly worth a function, really, but if you want one:
function get_post_index ( $ids, $vs = 0 {
if (empty($ids) || !is_array($ids)) return false;
$index = array_search($vs,$ids);
if (!empty($index)) {
return $index + 1;
} else {
return false;
}
}
Related Posts:
- WP insert post PHP function dynamically generated Custom Fields
- Inserting data into `post meta` table?
- WP_Query search posts by custom post type and custom taxonomy
- Conditional to modify query results
- Custom query to filter posts that have current post as a taxonomy [closed]
- creat filter with wp_query
- Querying a custom post type, but not show duplicates
- Check that a slug is present in the get_terms request
- Custom post type order by post_title
- Code in custom widget queries all posts, when it should only query the current post
- Order custom post type is beign ignored
- How to display categories of my Custom Post Type?
- Display a query with multiple post types and same relationship on a single page
- How to get a list of term names of the custom post type im currently on in single.php
- is_singular won’t call my functions?
- Include images from pages in wp search.php results in default wp search
- List events by month
- Custom Search | Wrong output & question
- Get the most popular terms for a custom post type
- show posts only on homepage but using custom post type and taxonomy for query posts via url
- Include different loop templates in search query
- Adding a span when custom post type is updated
- How do you output custom code between posts in the loop?
- How to make in WordPress admin panel sortable column for the custom field, that count the number of page impressions?
- Output custom post shortcode. Help spot the error.
- Is it possible to create a shortcode that will query a post based on taxonomies?
- Selecting New & Edit Page for Custom Post Types
- Blog page showing same content as homepage
- Get Post Primary Category
- A good strategy to print custom posts (offer) that are checked inside the metabox of a post?
- Very Slow Page – How to Optimize # of Queries?
- Custom post type permalink returns bad url
- How to display selected taxonomies by their parent
- Custom Field Order by Last Name and First Name
- get the custom post type title/name
- Push metadata in array
- Get custom post type list for every category shortcode
- How do i calculate the total of values of custom fields in custom post types?
- Custom post type – get_day_link()
- Meta query for custom post type ignored in main query
- How do I move/order posts with a tag to the end?
- Use custom metabox to update automatically a post after a given date
- get_post_meta not working on publishing
- WordPress custom post type split into pages
- Assign External Database Queries to Global Variables and Make Them Accessible
- Different Category system needed for the Custom Post Type
- Specifying Mouseover Icon for Custom Post
- Custom post type causes php-error in php version 5.2.17
- Make (custom) post type accessible only by custom query
- Displaying custom post types as a gallery
- Get data from PHP to JavaScript to set position of each post on front page
- Removing full title and breadcrumb header container for a specific post type
- Query custom post types & Taxonomies and list them in a table on a page
- Creating a “glossary” of WordPress posts of a specific post type
- Using Pagination for Custom Post Type and keeps returning 404
- Nested Custom Taxonomies | Incorrect posts when querying
- Validate Custom Post Type fields
- meta query not retrieving posts
- Hide text if query has no posts
- Create query for both custom post type and category
- How do I show my containing my custom field ONLY if there is a set value on that custom field? [closed]
- Custom taxonomy query showing more than 4 posts
- Function to erase every post from a taxonomy
- Custom Post type showing up in loop, regular posts are not showing up
- How do I ensure that post_type and Taxonomy use the same slug?
- Group episodes by seasons
- How to get post count of specific taxonomy that have store name & category
- CPT loop doesn’t seem to account for post date?
- What is the best way to set the post category (Custom Post Type) automatically based on the one of the tags assigned to the post?
- How to change Elementor Template for Single Page Post on Load
- Alike Shortcode using in Custom Shortcode
- Action on Custom Post publish
- Why it doesn’t create a custom post type after form submitting
- Set a template on a custom post in the plugin
- Filter Custom Post Type Posts by Taxonomy
- Using WordPress custom post type to display StoryMapJS
- Change Search display for Custom Post Type
- Stored meta from attachment, video length?
- tax_query not working?
- Add active class to foundation 6 tabs while looping categories
- How to make a shortcode for my WP_Query Loop? [duplicate]
- WordPress query posts by custom post type not workng
- Get posts of an specific term of a custom taxonomy
- WP_Query return highest number only
- Conditional posts in WP_query for search
- Can’t pick up a field created with Advanced Custom Fields
- Pods pagination erases my sub menu
- Display year value before a set of posts
- How to add parameters for custom post type
- Why is my custom post type slider only displaying the latest slide?
- using ACF datepicker to filter posts on a page
- WordPress multi-query not returning as expected
- how to limit query_post to first page?
- How to loose the image ‘alt’ attribute in my custom post type?
- Retrieve post data via WPDB class
- Listing custom post type items from a couple of custom taxonomies
- Pagination not working with custom loop
- posting twice from an array?
- custom post-type query just returns two posts
- How can I show a field ID in Elementor with Request Parameter