Advanced custom fields - relationships logic reversed - wordpress

On our site we currently have 'work' posts that we create and then associate with an author and genre post type.
The overall goal is:
When we view an author or genre post we want to list all the work posts that we have associated/related with that certain author/genre.
We are using the following code which seems to have us half way...
<?php $args = array(
'numberposts' => -1,
'post_type' => 'post',
'meta_query' => array(
array(
'key' => 'related_posts',
'value' => $post->id
)
)
);
$posts_array = get_posts( $args );
var_dump($posts_array);
if( $posts_array ) {
echo '<ul>';
foreach( $posts_array as $related ) {
echo '<li>';
echo '' . $related->post_title . '';
echo '</li>';
}
echo '</ul>';
}
?>
However the 'value' field in the array doesn't work. It technically should be passing the id of the current post (author or genre) and selecting the related content. When we remove this from the array it does bring all the posts in whether they are related or not.
In summary, we think that the 'value' problem may be the key in resolving the issue as that is what should be filtering the posts.
Thanks in advance

did you try $post->ID? (uppercase not lowercase)

Related

Get all values of a ACF field in all posts and the links to the posts

I have created an ACF field where I can add 1 keyword per post. I want to get now a list of all keywords set in all my posts and sort it by alphabet and add a link to the post where it was found. Every keyword will be unique. So it would be a kind of table of content. How can I do that programatically? I have no idea right now on how to loop through posts and get field values.
Place the following function in functions.php . Use it directly in your template or as shortcode or w/e
function keywords_post_list() {
//We build our query for posts containing any value in meta field
$args = array(
'post_type' => 'post',
'posts_per_page' => -1,
'meta_query' => array(
'key' => 'keyword', //change with your meta key
'value' => '',
'compare' => '!='
)
);
$query = new WP_Query($args);
global $post;
$items = array();
if($query->have_posts()):
while($query->have_posts()):
$query->the_post();
//Looping each post we collect the keyword and the link for a post.
//Grab any other information if you need and add in the array
$keyword = get_post_meta($post->ID,'keyword',true);
$link = get_the_permalink($post->ID);
//Our array
$items[] = array('keyword'=> $keyword,'link' => $link);
endwhile;
endif;
// We need to sort results by keyword currently ASC
array_multisort(array_column($items, 'keyword'), $items);
// If we need to sort DESC uncommnet bellow coment above
// array_multisort(array_column($items, 'keyword'),SORT_DESC, $items);
// error_log(print_r($items,true));
if($items):
echo '<ol class="keyword-list">';
foreach($items as $item):
$keyword = $item['keyword'];
$link = $item['link'];
echo '<li>'.$keyword.'</li>';
endforeach;
echo '</ol>';
endif;
}

How to create a filter to display custom post types

I have a custom post type 'projects' and have an overview page that displays these post types with featured image & post title. I have also created a custom taxonomy for that Post Type and assigned the posts to the categories from that taxonomy.
What I want to achieve now is that on the Overview Page where all the posts are listed, above them should be something like a filter bar with the custom taxonomy categories displayed.
My question now is: What WordPress functions do I need so that when someone clicks on one of the categories, only the posts assigned to that category will be displayed? I don't want the page to refresh or load another page. Here is an example of what I want to achieve: https://www.hauserlacour.de/en/work
Also, I am not a coder. I use Pinegrow to convert my static html sites to a wordpress theme. but in Pinegrow I have the option of a lot of WP function. That's why I just need to understand how the set up of something like above would work.
Many thanks in advance!
If you know a bit more about the the WP_Query you can use the tax_query as below:
$args = array(
'post_type' => 'project',
'post_status' => 'publish',
'tax_query' => array(
array(
'taxonomy' => array( 'project_cat' ), // <-- NO! Does not work.
'field' => 'slug',
'terms' => array( 'project_cat1', 'project_cat2')
)
)
);
$query = new WP_Query( $args );
Reference: https://developer.wordpress.org/reference/classes/wp_tax_query/
or you can simply list the taxonomy terms and simply redirect to taxonomy detail page where respective projects will be listed automatically.
$args = array( 'hide_empty=0' );
$terms = get_terms( 'my_term', $args );
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ) {
$count = count( $terms );
$i = 0;
$term_list = '<p class="my_term-archive">';
foreach ( $terms as $term ) {
$i++;
$term_list .= '' . $term->name . '';
if ( $count != $i ) {
$term_list .= ' ยท ';
}
else {
$term_list .= '</p>';
}
}
echo $term_list;
}
Reference: https://developer.wordpress.org/reference/functions/get_terms/

Exclude children of custom post type from search

I have a custom post type of 'location'. I then have children pages for each of the pages for that cpt. so it looks something like this, "www.example.com/location/location-name/child-page/", each child page is using a post template of "location-product-services.php". So what I am trying to do is exclude from the search results the children of this cpt.
I am trying to do it by checking the meta data to see if it is using that template. I just cant seem to get it working. Any help would be great.
This is what I currently have -
// Exclude local product and services pages from search result.
function location_subpages_exclude_search( $query ) {
if ( is_search() && !is_admin()) {
$query->set('meta_query',
array(
'key' => '_wp_page_template',
'value' => 'location-product-services.php',
'compare' => '!='
)
);
}
}
add_action('pre_get_posts', 'location_subpages_exclude_search');
Thanks in advance.
First, I pretty much exclusively use the Relevanssi plugin any time I want to modify search. But to search programmatically, I think this is what you're after:
$taxonomy = 'location';
$terms = get_terms($taxonomy, array( 'parent' => 0, 'search' => $query ) );
if ( $terms && !is_wp_error( $terms ) ) :
?>
<ul>
<?php foreach ( $terms as $term ) { ?>
<li><?php echo $term->name; ?></li>
<?php } ?>
</ul>
<?php endif;?>
Use the function get_terms to search your CPT, the 'search' is your $query (you might consider wrapping the search string with the SQL wildcard '%') and 'parent'=>0 returns only the top level.
I figured it out.
First I got all parent pages of my post type, used get_pages() grab them all.
Looped through each of the parent pages and ran another get_pages() for children of that parent.
function SearchFilter($query) {
if ($query->is_search) {
$args = array(
'hierarchical' => 0,
'post_type' => 'location',
'parent' => 0, //returns all top level pages
'post_per_page' => -1
);
$parents = get_pages($args);
$excludes = array();
foreach($parents as $parent) :
$args = array(
'post_type' => 'location',
'child_of' => $parent->ID,
'post_per_page' => -1
);
$children = get_pages($args);
foreach($children as $child):
array_push($excludes, $child->ID);
endforeach;
endforeach;
$query->set('post__not_in', $excludes);
}
return $query;
}
add_filter('pre_get_posts','SearchFilter');

Does "Advanced Custom Fields" taxonomy field support user pages?

I have a "taxonomy" custom field for the user pages. I want to build a query filtered by this field. It works with normal querys but not with user-querys, am i doing something wrong?
<?php
$args = array(
'key' => 'fruits',
'value' => 'apple'
);
// The Query
$user_query = new WP_User_Query( $args );
// User Loop
if ( ! empty( $user_query->results ) ) {
foreach ( $user_query->results as $user ) {
echo $user->display_name;
}
} else {
echo 'No users found.';
}
?>
Try get_users instead:
$users = get_users(array(
'meta_key' => 'fruits',
'meta_value' => 'apple'
));
var_export($users);
Wordpress codex: get_users()
Edit:
After a bit of research it turns out that get_users() is only a wrapper for WP_user_query, so switching to this function will make no difference.
However... did you notice that in my answer (and vrajesh') we have substituted your key with meta_key, and value with meta_value ... They are definitely defined in the WP_User_Query class, so I would be surprised if they didn't have any meaning.
If by chance you are using your original $args (which I guess does not actually refer to fruits and apples), then that may well be the explanation you are getting nothing.
try this:
$args = array( 'meta_key' => 'fruits', 'meta_value' => 'apple','compare' => '=');
Use WP_Query instead of WP_User_Query. WP_User_Query is used to retrieve data from user and usermeta table. And according to my understanding your are retrieving data from posts and postmeta table.
Class Reference/WP User Query and
Class Reference/WP Query
UPDATED:
Try this
<?php
$args = array(
'meta_query' => array(
'key' => 'fruits',
'value' => 'apple',
'compare' => '='
)
);
// The Query
$user_query = new WP_User_Query( $args );
// User Loop
if ( ! empty( $user_query->results ) ) {
foreach ( $user_query->results as $user ) {
echo $user->display_name;
}
} else {
echo 'No users found.';
}
?>

custom search against custom post type

In my wordpress site i need two types of search one for basic wordpress search another is custom search.both r separate .first one is ok but second one is creating problem.In custom search i have to search category and key word,Here category is custom_taxonomy and post type also custom post type.
taxonomy=faq-group
post_type=faq-item
example: if any one search category=Australia and keyword=visa Then it will show all post that have visa keyword and Australia category from faq module.
I've searched it in google.I think,i've write custom query
Thanks in advance
Use following code for achieve this
function custom_search_where( $where )
{
if(isset($_POST['your-custom-serchtext'])){
$where = "post_content Like %$_POST['your-custom-serchtext']% ", $where );
}
return $where;
}
$query = new WP_Query( array('post_type' => 'faq-item','tax_query' => array(
array(
'taxonomy' => 'faq-group',
'field' => 'slug',
'terms' => 'australia '
)
)) );
add_filter('posts_where', 'custom_search_where' );
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
echo '<li>' . get_the_title() . '</li>';
}
} else {
// no posts found
}
remove_filter( 'posts_where', 'custom_search_where' );
I have not tested this code but I hope it will useful for you.

Resources