Accessing Advanced Custom Fields by Page Name - wordpress

I am trying to retrieve all advanced custom fields tied to a particular page. This is different than iterating through posts, I am familiar with the following:
$posts = get_posts(array(
'post_type' => 'post_name',
'meta_key' => 'color',
'meta_value' => 'red'
));
However this method is specific to posts and does not allow me to retrieve all ACF by page name.
I appreciate any suggestions on how to accomplish this.

The are too ways to do this that come to mind...
1. Using the Loop
Using WP_Query you can do something like this...
<?php
// WP_Query arguments
$args = array (
'pagename' => 'homepage',
);
// The Query
$query = new WP_Query( $args );
// The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
the_field( "field_name" );
}
} else {
// no posts found
}
// Restore original Post Data
wp_reset_postdata();
?>
In place of the 'homepage' in 'pagename' => 'homepage', you want to put the page slug of your page. And of course in place of the_field( "text_field" ); you want to add your fields/content.
You can also query by Page ID and some other parameters. You can find all other parameters that you can use here:
https://codex.wordpress.org/Class_Reference/WP_Query#Post_.26_Page_Parameters
2. Adding second parameter to the_field() function
More simple way, without custom loop, is just to use ACF's built-in the_field() function with the $post->ID parameter added as a second parameter.
<?php the_field('field_name', 123);
This might be the way to go since you want to show the content of only one page, and therefore don't really need to loop.
Reference: http://www.advancedcustomfields.com/resources/how-to-get-values-from-another-post/

You can use ACF's get_fields() function -
<?php $fields = get_fields( $post->ID ); ?>
You could then loop through them or simply print the array for testing.
http://www.advancedcustomfields.com/resources/get_fields/

Related

How to reset after nested WP_Query inside WooCommerce products loop?

On the front-page I use multiple shortcodes like [products limit="4"] or custom WP queries. All of them fail because I am doing additional WP_Query inside each product in those loops.
Inside WooCommerce template content-product.php I am trying to call a function and I need to pass product ID to it.
<li <?php wc_product_class('', $product); ?>> <!-- this is the usual Woocommerce stuff to begin loop -->
<?php
$data = testtttt($product->get_id());
var_dump($data);
This is a function (I've simplified it for the sake of example)
function testtttt($product_id)
{
$args = array(
'posts_per_page' => -1,
'post_type' => 'discounts',
'meta_query' => array(
array(
'key' => 'connected_product_id',
'value' => $product_id
),
)
);
$discount_query = new WP_Query($args);
$data = [];
if ($discount_query->have_posts()) :
while ($discount_query->have_posts()) :
$discount_query->the_post();
$data[] = get_the_title();
endwhile;
endif;
//$discount_query->reset_postdata();
wp_reset_postdata();
return $data;
}
The problem is the following error, so everything after first <li>{product}</li> brakes completely.
PHP Fatal error: Uncaught Error: Call to a member function is_on_sale() on null in C:\wamp64\www\...\wp-content\plugins\woocommerce\templates\loop\sale-flash.php:26
That is probably because $product variable disappears after ...->the_post() thing. So I tried to make the first part like this:
// Try to save product variable for later reset
$product_temp = $product;
$data = testtttt($product->get_id());
var_dump($data);
// Try to reset product variable
global $product;
$product = $product_temp;
Now the code doesn't brake anymore but each <li></li> has completely wrong data, instead of product titles, titles for each product is now "Frontpage".
Any idea how to solve this?

Custom WP_Query for search

I'm trying to make a custom WP_Query for a search results page.
With the following code, the page always displayed all posts regardless:
<?php
$args = array(
'posts_per_page' => '-1',
);
$query = new WP_Query( $args );
?>
So I've added the search Query to the $args, but this always returns no results - where is this going wrong?
<?php
$search_query = get_search_query();
echo $search_query;
$args = array(
'posts_per_page' => '-1',
's' => $search_query
);
$query = new WP_Query( $args );
?>
1) You can use the templatesearch.php and searchform.php as your starting points. Creating a Search Page Codex
2) As far as the custom query goes, you can use pre_get_posts hook to test if you're on a search page, then you get $_GET your values, edit your query accordingly. Action Reference - pre_get_posts
There are tons of tutorials online and questions on this exchange to help you out. Some are Simple and others are more Complex. You'll have to do some real research to accomplish this. Hope it helps!
I have solved this using pre_get_posts - I'm still intrigued as to why using the WP_Query method doesn't work, but here's what I'm now using:
function search_filter($query) {
if ( !is_admin() && $query->is_main_query() ) {
if ($query->is_search) {
$query->set('posts_per_page', '-1');
}
}
}
add_action('pre_get_posts','search_filter');

Custom post query breaks original search term in Wordpress loop

I want to create a loop and query posts by their author role and display the results of a search term based on the authors role.
When I apply my query the posts returned are for the user role and not the original search term.
Here's my query and the loop;
$ids = get_users(
array(
'role' => 'administrator' ,
'fields' => 'ID'
)
);
$query = new WP_Query(
array(
'author__in' => $ids,
)
);
// If the query has data
if($query->have_posts() ) :
// Post loop
while ($query->have_posts() ) :
// Setup post data
$query->the_post();
?>
<!-- Do HTML markup and template tags here, eg. the_content(), the_title() etc.. -->
<h1>You're a post from administrator - <?php the_title(); ?></h1>
<?php get_template_part( 'template-parts/content', 'search' ); ?>
<?php
endwhile;
// End "If the query has data"
endif;
This is on my search.php page ...
So for example if I have two posts published from the user type 'administrator' one of those posts is tagged with 'England' and the other is not - so when I go to the search form and enter 'England' I would expect this code to display the one post out of those two however it doesn't, it displays both.
So it appears to be just displaying all posts from the user type 'administrator' and ignoring the search term.
I've got no idea why that is the case, in my mind this code should be working so if anyone has any advice or can point me in the right direction that would be great!
You are creating a custom loop so you need to add the search term back into the query args.
$query = new WP_Query(
array(
'author__in' => $ids,
's' => $_GET['s']
)
);

Wordpress query_posts with ID and slug combination

is it possible to use query_posts with a combination of id's and slugs?
I have an array of id's and slugs from user input and want to know if I can use the two in the same post query, or do I have to convert the slugs into their respective post ID's before and then use posts__in?
I have the following mixture of slugs and ID's in an array…
$values = array('this-is-a-test-slug', '2345', '4510', 'another-slug-here', '8934');
How can I use this in query_posts? Can I at all?
query_posts(array('post_type' => 'post', 'post__in' => $values, 'orderby' => 'rand'));
I know that post__in works ok with numeric ID's but I don't think slugs work here as it expects a numerical array.
Thanks
If you're doing something like this, i don't see why it wouldn't be a problem to just convert them all over to ID? There's a thread that sort of helps, and I've written some code (with help from that link) that might help you get started
function allToID($array){
$id = array();
foreach($array as $convert):
if(is_numeric($convert)):
continue; // skip if it's already an ID
else:
$the_slug = $convert;
$args=array(
'name' => $the_slug,
'numberposts' => 1
);
// Ask wordpress to get this post
$my_posts = get_posts($args);
if( $my_posts ) :
// push onto our new array of only IDs
array_push($id, $my_posts[0]->ID);
else continue;
endif;
endif;
endforeach;
}
Ideally you'll be able to run post__in => alltoID($values)
Hope this helps!

Search for Custom Field with Custom Post Types

There are tutorials that explain how to limit a search to a specific category.
My question is, is there a way to configure wordpress' search to, within a custom post type, search for a custom field value.
So for example, if I search for "hello", the results would come up with posts that have a certain custom field equal to "hello". The certain post would also be a certain custom post type.
Any help is appreciated.
To filter search by custom post type use:
<?php query_posts($query_string . '&post_type=custom-post-type-name' ); ?>
before the loop.. then within the loop add a condition similar to this
<?php if ($meta_data[ 'meta-name' ] == 'hello') {
//do something
} ?>
I think here is what you are looking for:
key is the custom field.
value is the value you are looking for
and compare is the operator you want to use.
you can also use LIKE if you want.
// WP_Query arguments
$args = array (
'post_type' => 'vendors',
'post_status' => 'published',
'meta_query' => array(
array(
'key' => 'state',
'value' => 'Misissipi',
'compare' => '=',
),
),
);
// The Query
$query = new WP_Query( $args );
// The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
// do something
}
} else {
// no posts found
}
// Restore original Post Data
wp_reset_postdata();

Resources