ACF next/previous on single post template from relationship field - wordpress

I have ACF with a relationship field to choose posts from a custom post type (artworks). Instances of the relationship field occur on multiple pages but with different post entries chosen.
It works fine to display the chosen posts as a list but when I get down to the single-artworks template level how can I get the prev and next links to adhere to only the chosen posts in the parent relationship field?
next_post_link()
previous_post_link()
on the single template seems to traverse all entries in the post type, ignoring the choices and order of the relationship field.
Seems like a need to provide the single template with an array of postIDs from the relationship field to choose from, or is there a better way?

You can define your custom pagination to show next and prev links as below:
$args = 'get you relationship query';
$curr_id = $post->ID;
unset($args['paged']);
$args['posts_per_page'] = -1;
$tempposts = query_posts($args);
$post_ids = array();
if($tempposts):
foreach($tempposts as $post){
$post_ids[] = $post->ID;
}
endif;
$current_index = array_search($curr_id, $post_ids);
// Find the index of the next/prev items
$prev = $current_index - 1;
$next = $current_index + 1;
//Prev post
get_permalink($post_ids[$prev]);
//Next post
get_permalink($post_ids[$next]);

Related

Advanced Custom Fields Default Values Based On Current User

How to set product field value based on current user's display name in ACF plugin?
Following acf/load_field[acf/load_field filter documentation][1] I tried adding this to my functions.php:
function acf_load_user_display_name( $field ){ $user = wp_get_current_user(); $display_name = $user->display_name; return $user->display_name; } // acf/load_field - default for product_display_name field add_filter('acf/load_field/name=product_display_name', 'acf_load_user_display_name');
This code makes a bug on the edit-product page and ACF-edit-field groups, and the custom field "product_display_name" is still blank on the product page.
[1]: https://www.advancedcustomfields.com/resources/acf-load_field/

Make Custom Post Type with custom field inherit category's custom field when empty

been struggling finding a solution to my problem for weeks.
Case :
I have a custom post type named : design. This CPT have a custom field (made with ACF plugin) called thematique. I created the same custom field (thematique) for design's categories.
Expected behaviour:
I want that whenever if we make a get_posts() or WP_Query if a design's thematique field is empty, it should inherit its categorie's thematique.
I've investigated into the pre_get_posts hook but I'm not quite sure how to handle it.
Anybody has an idea ?
Thanks in advance, I really appreciate your help !
You can just do this the easy way and inside your WP Query where you have the formatting for each returned item add this:
<?php $thematique = get_field('thematique'); //Gets current posts value of fiels
<?php if (empty($thematique)){ //Checks if the field is empty, if so do the following
$postCat = get_the_category(); //Get current posts category ID
$catID = 'category_' . $postCat; //Merge category ID and needed string for ACF
$thematique = get_field('thematique', $catID); //Updated the value of $thematique with categories value
}?>
Although not tested this should indeed work as it's how ACF says to get the value from categories. Find out more here.
#Ali_k
I'm not so sure about how to go about it though. I would need something like :
// Designs Thematique priority mechanic
function design_thematique_priority($query){
if($query->query['post_type'] == "design"){
foreach($query->posts as $post){
if($post->thematique == ""){
$post->thematique = $post->category->thematique;
}
}
}
}
add_filter( 'pre_get_posts', 'design_thematique_priority' );
But I don't think there is any loop I can use to loop through posts in pre_get_posts right ?

Show product variations on the product category and shop pages

I'd like to display product variations for some (not all) variable products on the shop and product-category pages based on a custom field I'm planning to add to each variable product. To keep pagination working, I can hook in to the woocommerce_product_query hook in WC_Query::product_query($q) but I can't figure out how to modify the query to give me what I'm after.
The logic I want to apply is:
//Pseudo-code
$loop = $query->get_all_products // i.e. not just for one page
foreach ( $loop as $product) {
if ($product->is_simple) {
display_simple($product)
} else {
$variations = wc_get_products($product->get_children());
foreach ($variations as $product) {
if ($product->custom_show_on_archive_pages) {
display_variation($product)
}
}
}
}
The problem with the above (apart from being pseudo-code) is that it assumes we have an array called $loop but actually we have a query object.
My ideal would be to modify the query so that it generates the results as per the pseudo-code, and then my archive-product.php template can use an if/else to display simple vs variable products.
Something like this in my functions.php:
add_action( 'woocommerce_product_query', 'show_variable_products');
function show_variable_products( $q ) {
$meta_query = $q->get('meta_query');
// Do something
$q->set('meta_query', $meta_query);
}
I'm wondering if I need to actually run multiple queries somehow:
The original query but filtered to parent products
The children of these parent products (i.e. the variations), filtered to those with custom field checked
The original query but filtered to simple products
Merge (2) and (3)
Would this be possible?
Alternatively, is there another solution (that does not involve buying a plugin)?

Is it possible to do a loop inside the option page of plugins on wordpress?

I've encounter this issue during the develop of my plugin.
I've to loop through all the pages of the website and print the titles and the IDs of them.
When I do the simply loop it just don't print anything.
Is there a way for do a loop inside options of my plugin?
Found the solution.
I don't know if there's some kind of block inside the plugin options page but we can loop through every custom post type with a little portion of code:
<?php
//Search only for custom post type which is public.
$args = array('public'=>true);
//Get all custom post type name
$allMyCustomPostTypes = get_post_types($args);
foreach ($allMyCustomPostTypes as $myCustomPostType) {
//Create a filter for get_posts with every custom post type
$filter = array('numberposts' => 5, 'post_type' => $myCustomPostType);
//Pass the value to *get_posts*
$theposts = get_posts($filter);
foreach ($theposts as $thepost) {
//Easy print the name of the current post of the current custom post type
echo $thepost->post_title;
};
};
?>
Here we are, hope this helps somebody.

WordPress - Custom page order for next_post_link within custom taxonomy

next_post_link as far as I'm aware, just gets the next post within the taxonomy based on date of creation. I was wondering if I could use custom fields to have it select the next post by the order in there. Like a page number field and it grabs the next post within the taxonomy with the next page number.
So we're in Taxonomy 'x' on page 2 (created 2 weeks ago), and want to move to page 3 (created today), and there's a page 4 (created 1 week ago).
It would go to page 4, but I want it to go to page 3.
Is there a good way to do this?
If you use custom fields like you mentioned, it wouldn't be using a taxonomy. You would be ordering the posts, or getting the posts, based on the get_field() function for acf.
$posts = get_posts();
foreach ($posts as $post) {
// This gets the custom field from ACF
$post->order = get_field('name-of-field', $post->ID);
if ($post->order == whatever_number_you_want) {
$active_post = $post;
}
}
echo $active_post;

Resources