I’m not sure you understand the logic of WP_Query
. Rather than explain in words, here’s a code example;
$query = new WP_Query( array( 'meta_key' => 'Old ID', 'meta_value' => $atts['oldid'] ) );
if ( $query->have_posts() )
return $query->posts[0]->post_title;
return '';
Check out the codex on interacting with WP_Query.
UPDATE: To use the query as you would normally, i.e. The Loop;
<?php if ( $query->have_posts() ) : ?>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<a href="https://wordpress.stackexchange.com/questions/16600/<?php the_permalink(); ?>"><?php the_title(); ?></a>
<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_postdata(); ?>
Related Posts:
- How to get specific string in explode array?
- WP Query by 4 different taxonomies
- How to paginate wordpress [gallery] shortcode?
- Too slow when using both ‘tax_query’ and ‘meta_query’ both in WP_Query
- How can I save an array from a random post sequence for later use?
- Last post ordered by publish date using WP_Query on front page
- Getting an array out of WPQuery
- assign 2 $args to one wp_query
- Use Transient API to cache queries for all posts in all categories?
- Shortcode returns escaped HTML tags
- WP_Query in a shortcode
- WP Meta Query for some meta (array) values
- meta_query with array as value
- Placing a div or img in between a post array using WP Query [closed]
- Show one post per author and limit query to 8 posts
- ‘&’ causes an error in my shortcode when I list the content of the page
- Can certain (site-crashing) limitations on WP_Query in shortcode be overcome?
- How add a custom posttype name using ACF field to a query post array
- How to retrieve current page WP_Query arguments?
- How can I get an array of all IDs from the current queried object?
- How can I pick a single post from the latest 3?
- WP Query for variable taxonomies
- wp_query add arguments using array_push if variable met
- Category attribute not working in custom shortcode
- Minimize database queries to user tables?
- (Solved) WP_Query ($ args) -> How to sort letters and numbers within the same array
- How to get all child pages in their parent page?
- Database Queries Optimization with new WP_Query
- Adding an array from a query string to a WP meta_query
- Orderby is working with one query but not with other
- Weird orderby => post__in issue
- How to list custom fields as headers and list all pages sharing that custom field under it?
- Shortcode for latest -not expired- posts
- Gallery shortcode numerical sorting
- Query all posts if multiple meta key don’t exist
- Get posts by list of post IDs ordered by those IDs?
- How to filter a query by date in a shortcode?
- Redirect to another page using contact form 7? [closed]
- How should I approach changing the template & $query as part of a shortcode’s execution?
- Removing duplicate values between two wordpress queries
- Include current post into loop
- Combine results of multiple WP_Query to resemble single WP_Query
- How do I turn these values from MYSQL into an array
- Does meta_value (array) work with ‘orderby’?
- search serialised meta_value for date value?
- Combine 2 arrays in a query parameter
- Shortcode Not Working in Slider When Added To Post Title
- Shortcode for custom query not returning results when attributes are added
- How to Display Shortcode in the correct location, currently displaying before content after Page Title?
- WP_query only displays one of my custom post type entries
- Assign a minimum result count for WooCommerce query shortcodes?
- How can I comment comma-separated array values?
- How do I add a relation parameter to my filter query?
- construct complex queries with WP User Query
- Using a Shortcode to List Posts from Multiple Categories
- Get a list of ACF Repeater-Fields as array
- embedding shortcodes in php template
- WP_Query with page_ids in arguments without a result
- Related Posts Excluding Certain Categories
- Query using string from custom field as array value
- Multiple meta query from array
- Category based on post id
- How to avoid filling up an array each time I run a WP_query?
- Remove from array in WP_Query loop
- meta_query returning excluded result
- Trouble passing attribute into shortcode function
- Woocommerce Shortcode which displays a table with product data
- Combine relationship posts with existing wp_query
- Counting instances of words in the results of a post query
- sanitize_post() is not sanitizing Post Object
- Retrieve all custom field values of a specific custom field metakey as an array, inside WP_Query
- Trouble with serialized metadata
- create custom shortcode wp and put php code in
- What code to use in an array to call the current sub-category?
- WP_User_Query Orderby Not Working
- Loop through array of pages
- How to display a post(by id) along with css in a page?
- Shortcode attributes causes 500 error after updating the page
- Repeat array inside array through while loop
- Sorting Posts by Taxonomy thats not within the query’s $args
- pages shortcode filtering by category
- WP_Query & shortcode : Return 3 articles from a category WordPress
- Related Post by Tags Code
- date_query problem
- Query by Category and Custom Field – Shortcode
- How to take parameters from a function and make them editable as attributes in a shortcode
- How to modify WP Query to target the first most recent post to adjust content
- Object of class WP_Query could not be converted to int inside shortcode
- How can I create another instance of my custom shortcode
- WP_Query: Meta_Query with serialized value (or a workaround)
- how to query for meta_value have array
- Get Current CPT taxonomy category in shortcode
- wp_query – Modify $query to include duplicate content
- Custom shortcode not displaying categories and correct date
- Iterate through ID’s in loop
- How to get several fields from wp_query?
- How to sort a WP_Query array by post_name after an array_merge();
- return wp query results in a shortcode
- Issue with WP_Query (need a array of selected ID’s)
- More efficient query to display posts in same sub-category?