57164

Wordpress - ACF - get post multiple times with repeater date field

Question:

I have a custom post type koncerty with custom date field datum, and I'm currently retrieving the posts with this query (which works well):

$date = date("Ymd"); $sql = <<<SQL SELECT p.ID, p.post_content, p.post_title, datum.meta_value as datum, featured.meta_value as featured FROM {$wpdb->posts} p LEFT JOIN {$wpdb->postmeta} featured ON featured.post_id = p.ID AND featured.meta_key = 'featured' LEFT JOIN {$wpdb->postmeta} datum ON datum.post_id = p.ID AND datum.meta_key = 'datum' WHERE p.post_type = 'koncert' AND p.post_status = 'publish' AND datum.meta_value >= '$date' ORDER BY featured DESC, datum ASC, RAND() LIMIT 3 SQL; $posts = $wpdb->get_results( $sql );

I would like to make the datum field repeatable, using the ACF repeater field addon. How should I modify the query, to get the post multiple times? Ie., I'll have a show with three dates, and need to display it like

<ul><li>1.1.2013 - Show 1 - info</li> <li>2.1.2013 - Show 1 - info</li> <li>3.1.2013 - Show 1 - info</li> </ul>

There's some tutorial here: <a href="http://www.advancedcustomfields.com/resources/tutorials/querying-the-database-for-repeater-sub-field-values/" rel="nofollow">http://www.advancedcustomfields.com/resources/tutorials/querying-the-database-for-repeater-sub-field-values/</a>, but with my limited sql knowledge I'm not able to put it together.

Thanks

Answer1:

I seem to got it working, here's the query:

SELECT p.ID, p.post_content, p.post_title, datum.meta_value as datum, datum.meta_key , featured.meta_value as featured FROM wp_posts p LEFT JOIN wp_postmeta featured ON featured.post_id = p.ID AND featured.meta_key = 'featured' LEFT JOIN wp_postmeta datum ON datum.post_id = p.ID AND datum.meta_key LIKE 'datum2_%_datum_koncertu' WHERE p.post_type = 'koncert' AND p.post_status = 'publish' AND datum.meta_value >= '$date' ORDER BY featured DESC, datum ASC, RAND() LIMIT 3

I use 'datum2_%_datum_koncertu', because the naming convention ACF uses to save data to database is $ParentName_$RowNumber_$ChildName (see here <a href="http://www.advancedcustomfields.com/resources/tutorials/querying-the-database-for-repeater-sub-field-values/" rel="nofollow">http://www.advancedcustomfields.com/resources/tutorials/querying-the-database-for-repeater-sub-field-values/</a>).

This way the query returns each post multiple times, and to display the date, I just use <?php date("d.m.", strtotime($post->datum))?>

Not sure, if this is possible with wp_query at all.

Recommend

  • dynamic subdomains in Symfony 1.4
  • Cannot modify header information - headers already sent in WordPress [duplicate]
  • How to query SQL for coords nearest a point?
  • SQL join, sum, group-by and instead of null 0
  • LEFT JOIN doesn't behave as expected as gives NULLs in MySQL
  • MYSQL fetch 10 posts, each w/ vote count, sorted by vote count, limited by where clause on posts
  • MySql update fields based on other fields
  • Woocommerce - List product by using specific attributes
  • Why echoing a local variable inside the same function is not working? [duplicate]
  • SQL query to include specific WooCommerce tag
  • Woocommerce meta_query not working for featured products
  • wordpress wpdb->update not working
  • NoReverseMatch error when running tests in Django
  • PHP: How to put a variable in an array?
  • No callback with FB.ui
  • How can I initialize wpdb class in a php file?
  • Wordpress - How to change search value using a drop down selector?
  • Is there a MySQL query that can encode to JSON in exactly this way?
  • 2-table interaction: insert, get result, insert
  • Simplify where clause with repeated associated type restrictions
  • Is there any way to use wpdb prepare statements for array implode(' OR ', $myArray)?
  • d3.js selection conditional rendering
  • Use default value of a column in stored procedures
  • Getting short path in python
  • joining two bezier curves
  • Basic many-to-many left join query
  • R convert summary result (statistics with all dataframe columns) into dataframe
  • Implicit joins and Where in Doctrine - how?
  • D3 get axis values on zoom event
  • Listbox within Listbox and scrolling trouble in Windows Phone 7 Silverlight
  • Disable Enter in editText android
  • Jetty Server not starting: Unable to establish loopback connection
  • Django: Count of Group Elements
  • How to check if every primary key value is being referenced as foreign key in another table
  • How to handle AllServersUnavailable Exception
  • How to get next/previous record number?
  • python regex in pyparsing
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?