1

I am using ACF plugin and I would like to create an aggregate shortcode containing data from other pages which use custom fields. Can you help how to start?

Thank you

So some of my posts have custom fields such as "photo" allowed. I would like to query all posts and get the data from all pages that contain "photo" custom field.

I have query that should work

<?php
// args
$args = array(
    'meta_key' => 'photo'
);

// get results
$the_query = new WP_Query( $args );

// The Loop
?>
<?php if( $the_query->have_posts() ): ?>
    <ul>
    <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
        <li>
            <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
        </li>
    <?php endwhile; ?>
    </ul>
<?php endif; ?>

But it returns no results.

If I call

<?php the_field('photo'); ?>

It returns the expected value

3
  • the second parameter in the get_field function is the post/page id. So you can get a custom field from any post/page at any time.
    – gdaniel
    Commented Feb 8, 2015 at 19:08
  • The Shortcode API codex page is a good place to start. Please edit your question to show what you have tried, you are expected to make some efforts to solve your problem so there is a starting point for someone to provide meaningful assistance.
    – Milo
    Commented Feb 8, 2015 at 19:10
  • I updated the description
    – tomexx
    Commented Feb 8, 2015 at 22:12

1 Answer 1

1

Your query seems to be in order except for the fact that you do need to reset your query once done. Simply just add wp_reset_postdata() between endwhile and endif. Failing to do so might lead to other queries being influenced by this query. The opposite is also true, other custom queries can also influence this one if not resetted.

I can only speculate to what might be wrong, but here is a couple of point to inspect

  • post_type -> By default, except for tax queries, this parameter is set to post. If you are using a custom post type or pages, your query will simply return nothing

  • post_status -> By default this is set to publish, private is added when the user is logged in. If your post's status is anything else, you will need to add that to your query.

If this does not work, inspect your SQL request by placing the following code just below your query

echo $the_query->request;

and then taking it from there.

One quick tip, if you want to "safegaurd" a new instance of WP_Query against modifications from filters, just add 'suppress_filters' => true, to your query arguments. This is also useful in debugging as this can be a quick test to see if unexpexted behavior by a custom query is caused by a custom filter

On your shortcode, before you even create one, just a tip, if you are going to use do_shortcode() in a template file, then you should reconsider and just use the function and drop the shortcode. Shortcodes need to be parsed, so it will be slower than just calling the function

If you do see the use of a shortcode fit for your needs, you need adjust your custom query accordingly. Shortcodes should never echo their output, but return it instead. To do this, use the get_ prefix functions of template tags you are currently using, then add your HTML markup and template tags to a variable and simply return the variable at the end

Additional info:

1
  • Yup, it was the post_type. When I forced it to my custom post type, it started to work. I thought it would query all the post types. But seems it does not work that way.
    – tomexx
    Commented Feb 10, 2015 at 19:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.