vendredi 27 mars 2015

Query post using Advanced Custom fields (Text one textfield with multi values;comma separation)


Hi i developed a page template that query post through the use of ACF lets call it page-test.php with this code:



<?php

$appvalue = get_field('em_application_value');
$appexplode = explode(",",$appvalue);
$appimplode = implode(" or ",$appexplode);

$prodvalue = get_field('em_product_value');
$prodexplode = explode(",",$prodvalue);
$prodimplode = implode(" or ",$prodexplode);

$resultnum = get_field('em_number_of_results_to_show');

?>

<div style="margin: 5% 8.3333%">
<h1>Application <?php echo $appimplode; ?> and Product <?php echo $prodimplode; ?> results:</h1>
</div>


<?php



// args
$args = array(
'numberposts' => $resultnum,
'post_type' => 'post',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'em_application',
'value' => $appexplode,
'compare' => 'IN'
),
array(
'key' => 'em_product',
'value' => $prodexplode,
'compare' => 'IN'
)
)
);

// query
$the_query = new WP_Query( $args );
?>

<?php if( $the_query->have_posts() ): ?>
<?php
// Start the loop.
while ( $the_query->have_posts() ) : $the_query->the_post();

/*
* Include the Post-Format-specific template for the content.
* If you want to override this in a child theme, then include a file
* called content-___.php (where ___ is the Post Format name) and that will be used instead.
*/
get_template_part( 'content', get_post_format() );

// If comments are open or we have at least one comment, load up the comment template.
/* if ( comments_open() || get_comments_number() ) :
comments_template();
endif; */

// End the loop.
endwhile;
?>
<?php else: ?>

<div style="margin: 5% 8.3333% 1%; background: #fff; padding: 5% 10%;">
Not found
</div>

<?php endif; ?>


Then In page editor when you select this template , custom fields will appear and I named them


em_application_value for Application textfield em_product_value for Product textfield em_number_of_results_to_show and for numeric field


The use of the template is to query posts through these custom fields. the first attempt was okay but when i tried to input multiple values the posts couldn't show in the frontend. Maybe i have an error for content.php (twentyfiftheen theme)


My only problem here is that what if the single post has multiple application and product values? let's say in one single textfield i can put multiple values separated with commas and i want the wordpress to detect that they are different values. how can page-test.php compare the words using "IN" . please help





Aucun commentaire:

Enregistrer un commentaire