I am using the hook "pre_get_posts" to query only posts that have featured image in the front page:
add_action( 'pre_get_posts', 'my_pre_get_posts' );
function my_pre_get_posts( $q ){
if ( $q->is_home() // only target homepage
&& $q->is_main_query() // only target the main query
&& !is_admin() // target front end only
) {
$q->set( 'meta_key', array( '_thumbnail_id' ) );
}
}
It looks like this portion is being ignored.
$q->set( 'meta_key', array( '_thumbnail_id' ) );
Your help is appreciated.
You need to check whether '_thumbnail_id' meta_key exists or not. So let's modify your code like this.
add_action( 'pre_get_posts', 'my_pre_get_posts' );
function my_pre_get_posts( $q ){
if ( $q->is_home() // only target homepage
&& $q->is_main_query() // only target the main query
&& !is_admin() // target front end only
) {
$meta_query = array(
array(
'key'=>'_thumbnail_id',
'compare'=>'EXISTS',
),
);
$query->set('meta_query',$meta_query);
}
}
Related
I am trying to filter products by product range in backend. Somehow I am unable to do so. Any help will be highly appreciated. Also let me know if there is any plugin available for this one? My recent code.
add_action( 'restrict_manage_posts', 'my_custom_product_filters' );
function apply_my_custom_product_filters( $query ) {
global $pagenow;
// Ensure it is an edit.php admin page, the filter exists and has a value, and that it's the products page
if ( $query->is_admin && $pagenow == 'edit.php' && isset( $_GET['price'] ) && $_GET['price'] != '' && $_GET['post_type'] == 'product' ) {
// Create meta query array and add to WP_Query
$meta_key_query = array(
array(
'key' => '_my_meta_value',
'value' => esc_attr( $_GET['price'] ),
)
);
//$query->set( 'meta_query', $meta_key_query );
$query->query_vars['price'] = $meta_key_query;
}
}
add_action( 'pre_get_posts', 'apply_my_custom_product_filters' );
Thanks in advance guys.
I wanted to update all my pages' excerpt with custom excerpt. So I created my own plugin with few lines of code. I dunno why it is not working, This is my code
function update_my_metadata_new(){
$pages = get_pages();
foreach ( $pages as $page ) {
// Run a loop and update every meta data
if(in_category('books')){
$the_post = array(
'ID' => $page->ID,//the ID of the Post
'post_excerpt' => 'Read books',);
wp_update_post( $the_post );
}
}
}
This plugin will loop into all pages in the given category and update the excerpts, when activated.
and I have enabled excerpts for pages by adding this code.
add_post_type_support( 'page', 'excerpt' );
to the functions.php file.
It looks like you are using in_category outside of the main loop. You need to pass in the ID of the page for in_category to work:
in_category('books', $page->ID )
function update_my_metadata_new(){
$pages = get_pages();
foreach ( $pages as $page ) {
/* pass the page ID here */
if( in_category( 'books', $page->ID ) ){
$the_post = array(
'ID' => $page->ID,//the ID of the Post
'post_excerpt' => 'Read books',
);
wp_update_post( $the_post );
}
}
}
register_activation_hook( __FILE__, 'update_my_metadata_new');
I multiple custom post types and I would like to set a default category that can be set from one function through parameters. I initially did this for one custom post type and was hardcoded but I thought I'd make so we only have to output the function with parameters in case we ever decided to add more custom post types.
The problem is inside the function WordPress doesn't seem to link it's own hooks and functions and is returning invalid taxonomy errors when trying to set it via the wp_set_object_terms() function. When I have done this the WordPress way which would make you create a separate function for each post type and default term you want which I have put below.
function set_default_object_terms( $post_id, $post ) {
if ( 'publish' === $post->post_status && $post->post_type === 'comic' ) {
$defaults = array(
'story' => array( 'draft' )https://silentcomics.com
);
$taxonomies = get_object_taxonomies( $post->post_type );
foreach ( (array) $taxonomies as $taxonomy ) {
$terms = wp_get_post_terms( $post_id, $taxonomy );
if ( empty( $terms ) && array_key_exists( $taxonomy, $defaults ) ) {
wp_set_object_terms( $post_id, $defaults[$taxonomy], $taxonomy );
}
}
}
}
add_action( 'save_post', 'set_default_object_terms', 0, 2 );
What I am trying to achieve is something like below but none of WordPress' global variables are working or any wordpress function like get_object_taxonomies() or wp_set_object_terms() which are used in the example WordPress gives.
Below is a simplified version of what I am trying to achieve and the parameters I am trying to pass through is the cpt slug, the taxonomy slug and the id of the category I want to fallback to. I have also tried it with the variable $cat_id as an array.
function set_default_term( $cpt_slug, $taxonomy_slug, $term_id ) {
if ( isset( $_GET['post'] ) ) {
$post_id = intval( $_GET['post'] );
$post_type = get_post_type( $post_id );
$status = get_post_status( $post_id );
$cat_id = array( $term_id );
$cat_id = array_map( 'intval', $cat_id );
$cat_id = array_unique( $cat_id );
if ( $status === 'publish' && $post_type === $cpt_slug ) {
wp_set_object_terms( $post_id, $cat_id, $taxonomy_slug, true );
}
}
}
add_action( 'save_post', 'set_default_term', 20, 3 );
set_default_term('deployment-guides', 'deployment-guide-category', 98);
Hi Brad i've checked your code just replaced post type to post and taxonomy to post_tag just to test general principle and it works well. See below a little modified code i've used to check.
function my_custom_set_default_terms_for_posts( $post_ID, $post ) {
if ( 'publish' === $post->post_status && 'post' === $post->post_type ) {
$defaults = array(
'post_tag' => array( 'example-default-tag-slug' ),
);
$taxonomies = get_object_taxonomies( $post->post_type );
foreach ( (array) $taxonomies as $taxonomy ) {
$terms = wp_get_post_terms( $post_ID, $taxonomy );
if ( empty( $terms ) && array_key_exists( $taxonomy, $defaults ) ) {
$terms_to_attach = $defaults[ $taxonomy ];
if ( count( $terms_to_attach ) ) {
wp_set_object_terms( $post_ID, $terms_to_attach, $taxonomy );
}
}
}
}
}
add_action( 'save_post', 'my_custom_set_default_terms_for_posts', 0, 2 );
And thinking more about actual issue returning invalid taxonomy errors when trying to set it via the wp_set_object_terms() it might happened not because of your save_post handler does something wrong - it's a good right code.
You need to check how you initialize taxonomy and Custom Post Type (CPT), ideally you should do it on init hook so your CPT and custom taxonomies got initialized before this save_post hook or other code runs which relies on taxonomies\cpt existence.
Also when you initialize CPT and bound to it taxonomy, you need to initialize CPT first via register_post_type() and then initialize taxonomy which is bound to it via register_taxonomy(), as normally when you register taxonomy it expects an array of Post types to be used with, and they needs to be registered before registering taxonomy.
I created a custom post type with a plugin. A registered user can insert a new post from front-end and it is saved as draft. When I edit it in back-end I need it is saved with private visibility.
I found this snippet to set visibility by default:
public function force_dpa_request_private( $data , $postarr ) {
if( empty( $data['post_name'] ) && 'my-cpt' == $postarr['post_type'] )
$data[ 'post_status' ] = 'private';
return $data;
}
but it works only on first insert, when I edit it the visibility change to public...
You can hook to the save_post which is called after the post is created or updated.
<?php
add_action( 'save_post', 'callback_save_post', 10, 3);
function callback_save_post( $post_ID, $post, $update ){
if ( 'my-cpt' === get_post_type( $post_ID) && ! wp_is_post_revision( $post_ID ) ) {
// unhook this function so it doesn't loop infinitely
remove_action('save_post', 'callback_save_post', 10 );
// Make the post private if it is edited else make it draft.
if ( $update ) {
$postarr = array(
'ID' => $post_ID,
'post_status' => 'private'
);
} else {
$postarr = array(
'ID' => $post_ID,
'post_status' => 'draft'
);
}
// Update the post.
wp_update_post( $postarr );
// re-hook this function.
add_action( 'save_post', 'callback_save_post', 10, 3);
}
}
Reference:
https://developer.wordpress.org/reference/hooks/save_post/
https://codex.wordpress.org/Function_Reference/wp_update_post
Slight variation from your question, but you can still edit the post on creation, if you make all your post type private. Thus this works.
function force_type_private($post)
{
if ($post['post_type'] == 'Your Post Type')
$post['post_status'] = 'private';
return $post;
}
add_filter('wp_insert_post_data', 'force_type_private');
Is there anyway I can sort by stock? Since it reads 'Stock x 30' it would be helpful if I could sort by high or low. Is there any code I can add to the functions.php to enable that feature?
easy enough... it can be accomplished with this code...
add_filter( 'manage_edit-product_sortable_columns', 'rei_product_sortable_columns' );
function rei_product_sortable_columns( $columns ) {
$custom = array(
'is_in_stock' => 'is_in_stock'
);
return wp_parse_args( $custom, $columns );
}
but this would just sort what the stock column has... example, 'in stock' or 'out of stock'
To have a custom sort, (for example, using the stock number), you may follow the instruction on this blog post.
Working version as of: Year 2022, WP 5.9.3, WC 6.4.1:
// make quantity column sortable
function rei_product_sortable_columns( $columns )
{
$custom = array(
'is_in_stock' => 'stock_disponible'
);
return wp_parse_args( $custom, $columns );
}
add_filter( 'manage_edit-product_sortable_columns', 'rei_product_sortable_columns' );
// only load on edit.php page
function my_edit_movie_load() {
add_filter( 'request', 'my_sort_movies' );
}
add_action( 'load-edit.php', 'my_edit_movie_load' );
// do the sorting
function my_sort_movies( $vars )
{
/* Check if we're viewing the 'product' post type. */
if ( isset( $vars['post_type'] ) && 'product' == $vars['post_type'] )
{
/* Check if 'orderby' is set to 'stock_disponible'. */
if ( isset( $vars['orderby'] ) && 'stock_disponible' == $vars['orderby'] )
{
/* Merge the query vars with our custom variables. */
$vars = array_merge(
$vars,
array(
'meta_key' => '_stock',
'orderby' => 'meta_value_num'
)
);
}
}
return $vars;
}