I need a list, in wp-admin, on the profile page, which lists all the users posts. Not just post_type post, but also with author with the current user ID.
I've set the relationshop to the post_type, but isn't it possible to make another "meta query" of some sort, so I can chose only to show the current users posts?
The answer is to create an ACF relationshop query.
function profile_relationship_query( $args, $field, $post_id ) {
$post_type = $args["post_type"][0];
$args = array(
'numberposts' => 10,
'post_type' => $post_type,
'meta_query' => array (
array (
'key' => 'authors',
'value' => '"' . $post_id . '"',
'compare' => 'LIKE'
)
)
);
return $args;
}
add_filter('acf/fields/relationship/query/name=profile_articles', 'profile_relationship_query', 10, 3);
And add it to functions.php.
Related
I have a custom post type with a post object field that enables the selection of related pages for the custom post type, the post object field is enabled for multiple posts, and is set to return post id.
I want to build a query in a specific page that checks all the custom post types that are related to this page with post object.
I am using this query:
$val = get_the_ID(); // get current page ID
$args = array(
'post_type' => 'articles_content', //custom post type
'posts_per_page' => -1,
'meta_query' => array(
'key' => 'pages_link', //the name of the post object field
'value' => $val, //the value of the current ID
'compare' => 'LIKE'
),
);
$the_query = new WP_Query( $args );
if( $the_query->have_posts() ):
while( $the_query->have_posts() ) :
$the_query->the_post();
// Do something...
endwhile;
endif;
wp_reset_query();
The problem is this - for examlpe if my page ID is "80", then the query also brings custom posts that are have pages with ID of "8002" or something similar.
I tried to use the "=" sign in the compare but got no results at all.
How can I query only by the custom posts that have the exact page ID as the current page in their post object array values?
This is the solution if anyone encounters the same issue:
$args = array(
'posts_per_page' => -1,
'post_type' => 'articles_content',
'meta_query' => array(
array(
'key' => 'pages_link',
'value' => '"' . get_the_ID() . '"',
'compare' => 'LIKE'
)
)
);
I have created a custom post type and I have Woo Subscriptions. I would like to create a function that detects the post author role of each post.
If the author role is "Unsuscribed" then, change post status by that author to Draft. So with this, if users stops to pay the subscription, automatically their posts will not be shown.
Thank you!
I think that you can use the add_user_role action hook. This hook fires after a user has been given a new role.
add_action( 'add_user_role', 'unpublish_non_subscriber_posts', 10, 2 );
function unpublish_non_subscriber_posts( $user_id, $role ) {
if ( $role !== 'unsubscribed' ) {
return;
}
$user_posts = get_posts( array(
'posts_per_page' => -1,
'post_type' => 'your_custom_post_type',
'post_status' => 'publish',
'author' => $user_id,
'fields' => 'ids'
) );
if ( count( $user_posts ) === 0 ) {
return;
}
foreach ( $user_posts as $post_id ) {
wp_update_post( array(
'ID' => $post_id,
'post_status' => 'draft'
) );
}
}
How do I query related product post title instead of the whole post object array in my WordPress query below?
$product_search = $_GET['s'];
$related_product = get_field( 'related_products' ); // This is an ACF relationship field so I can link products to articles and display teh related article in search.php from the product page
$knowledge_args = array(
'post_type' => 'knowledge_hub',
'fields' => 'ids',
'posts_per_page' => -1,
'meta_query' => array(
'key' => $related_product, // How do I query related product post title instead of whole post object array?
'value' => $product_search,
'compare' => 'LIKE'
)
);
$relatedProductArticles = new WP_Query($knowledge_args);
You could do that by manipulating the sql "WHERE" clause.
Create a custom function that does the filtering for you!
Add the argument that our custom function is looking for
Add the filter right before the custom WP_Query
Remove the filter after WP_Query
So you could write a the custom function in your functions.php like so:
function related_products_title_filter($where, $wp_query)
{
global $wpdb;
if ($searched_term = $wp_query->get('my_custom_search_filter_title')) {
$where .= ' AND ' . $wpdb->posts . '.post_title LIKE \'%' . esc_sql(like_escape($searched_term)) . '%\'';
}
return $where;
}
Add a custom argument to the arguments:
$knowledge_args = array(
'my_custom_search_filter_title' => $product_search, // This is the custom argument that our function is looking for
'post_type' => 'knowledge_hub',
'fields' => 'ids',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => $related_product,
'value' => $product_search,
'compare' => 'LIKE'
)
)
);
Add the filter right before the custom WP_Query:
add_filter('posts_where', 'related_products_title_filter', 10, 2);
Remove the filter after WP_Query
remove_filter('posts_where', 'related_products_title_filter', 10, 2);
So your entire code would be something like this:
function related_products_title_filter($where, $wp_query)
{
global $wpdb;
if ($searched_term = $wp_query->get('my_custom_search_filter_title')) {
$where .= ' AND ' . $wpdb->posts . '.post_title LIKE \'%' . esc_sql(like_escape($searched_term)) . '%\'';
}
return $where;
}
$product_search = sanitize_text_field($_GET['s']);
$related_product = get_field( 'related_products' );
$knowledge_args = array(
'my_custom_search_filter_title' => $product_search,
'post_type' => 'knowledge_hub',
'fields' => 'ids',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => $related_product,
'value' => $product_search,
'compare' => 'LIKE'
)
)
);
add_filter('posts_where', 'related_products_title_filter', 10, 2);
$relatedProductArticles = new WP_Query($knowledge_args);
remove_filter('posts_where', 'related_products_title_filter', 10, 2);
Let me know if you were able to get it to work!
I've included a function to show the product count in on the category overview page in WooCommerce:
add_action( 'woocommerce_before_subcategory_title', 'custom_woocommerce_subcategory_thumbnail', 10 );
function custom_woocommerce_subcategory_thumbnail( $category ) {
echo $category->count;
}
The problem is that I've for example 4 products in category a, but one of them is hidden in the catalog. So there should be a changed category count of 3 because the 4th one is hidden. But there is stillt 4 shown.
How can I exclude hidden products from the count?
You will have to use a custom WP_Query that will only return visible items. Something like this:
add_action( 'woocommerce_before_subcategory_title', 'custom_woocommerce_subcategory_thumbnail', 10 );
function custom_woocommerce_subcategory_thumbnail( $category ) {
$args = array(
'posts_per_page' => -1,
'post_type' => 'product',
'product_cat' => $category->term_id,
'meta_query' => array(
array(
'key' => '_visibility',
'value' => 'hidden',
'compare' => '!=',
)
)
);
$wc_query = new WP_Query($args);
echo $wc_query->found_posts;
}
I want to display "Related posts" of a single post page with custom post type named 'property' which is using the ACF Relationship Field for another custom post type.
That other post type is 'contact' and in the Single Properties post type, the relationship field is calling out for that. I have been trying to understand ACF's documentation here, but I was not able to really comprehend why my code isn't working.
I need to show related properties based on the brokers. I don't fully understand SQL statements and table joining.
$properties = get_posts(array(
'post_type' => 'property', // Page Custom Post Type
'posts_per_page' => 6,
'meta_query' => array(
// 'relation' => 'AND',
// array(
'key' => 'contact', // Field name with 2nd custom post type, 'contact'
'value' => '"' . get_the_ID() . '"',
'compare' => 'LIKE'
// )
)
));
Turns out the reason that this was messed up was due to the way ACF stored the array. Their documentation, while it works for their case, was off in mine due to a nester array.
This is what worked for me.
// This is the start of figuring out the array issue
$contact = get_field( 'contact' );
// This showed me the first array
$contact_array = $contact[0];
// This showed me the ACF array and allowed me to return the ID
$contact_ID = $contact_array->ID;
$properties = get_posts(array(
'post_type' => 'property',
'posts_per_page' => 6,
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'contact',
// Had to call the value like this as the array was nested like
// a:2:{i:0;s:3:"123";i:1;s:3:"321";} or something.
'value' => '"' . $contact_ID . '"',
'compare' => 'LIKE'
)
)
));
A more complex query with an additional taxonomy called medienform
// Shortcode for ACF - Add Relationship ACF field [sc_acf_fields]
//
add_shortcode( 'sc_acf_fields', 'related_relationship' ); // Add your shortcode here
function related_relationship() {
// get the taxonomy medienform
$cat_taxonomies = get_terms( 'medienform', $args );
$count = count($cat_taxonomies); // wird nicht verwendet
//
/// foreach category as $form_category - medienform
//
foreach ( $cat_taxonomies as $form_category ):
// check the arguments of the post_type, orderby
$args =array(
'posts_per_page' => -1,
'post_type' => 'materialien', // the CPT
'orderby' => 'title', // menu_order
'order' => 'ASC', // DESC
'tax_query' => array( // the tax_query is important to list the posttypes to its taxonomies
'relation' => 'AND',
array(
'taxonomy' => 'medienform',
'field' => 'slug',
'terms' => $form_category->slug
)
),
'meta_query' => array( // the meta_query is important to list only the related posts of the key
array(
'key' => 'post-relationship', // name of custom field Relationship in: https://qualitaetsoffensive-teilhabe.de/wp-admin/post.php?post=12067&action=edit&classic-editor=1
'value' => '"' . get_the_ID() . '"', // matches exactly "123", not just 123. This prevents a match for "1234"
'compare' => 'LIKE'
)
),
);
//
// make an own WP_Query from the $args
$materials = new WP_Query( $args );
//
// let´s give the category ->name here and the term_link().
// actually a category should only be displayed when it has some child // here we build the accordeon
//
if ($materials->have_posts() ) {
echo '<h4>' . $form_category->name . '</h4>';
};
//
// while schleife
while ( $materials->have_posts() ) {
//
//// use variable as the_post() query
$materials->the_post();
?>
<div class="post-loop-single">
<div class="thumbnail-img"><span><?php the_post_thumbnail('post-thumb'); ?> </span></div>
<div class="post-loop">
<div class="post-loop-title"><?php the_title(); ?></div>
</div>
</div>
<?php } wp_reset_query();
endforeach;