Different restrictions on media library modal - wordpress

I know it's possible to restrict the media library modal's items using the ajax_query_attachments_args filter, but is it possible to do so differently wether you're editing a field or another of a same post ?
For example : i have an ACF Gallery field on a post type, and i want its images to only be a selection of images, while the other media libraries on the page (featured image, editor add media, ...) still displays all the images.
Thanks

ACF Support provided me an answer, so i'm sharing it here :
Using the ajax_query_attachments_args filter, it is possible to identify the field initiating the request, as "_acfuploader" :
Array
(
[action] => query-attachments
[post_id] => 838
[query] => Array
(
[post_mime_type] => image
[orderby] => date
[order] => DESC
[posts_per_page] => 40
[_acfuploader] => field_579b87ce88001
[paged] => 1
)
)
So with ajax_query_attachments_args, it is then possible to modify the query :
add_filter( 'ajax_query_attachments_args', 'filter_product_attachments', 10, 1 );
function filter_product_attachments( $query = array() ) {
if ($_POST['query']['_acfuploader'] == $THE_FIELD_ID){
// Filter media query here
}
return $query;
}

Related

WooCommerce - include custom fields in the search function

So I'm looking to include the functionality of displaying content by its custom fields as well as its title and content.
I need to be able to search for orders, as well as subscriptions, on WooCommerce by custom field as well as the normal method. Is there any way I can, without adding additional search forms or booleans, simply get Wordpress to display posts that match with the search term by their custom fields, too?
I have used the following code thanks to a responder on here:
function custom_search_query( $query ) {
$custom_fields = array(
// put all the meta fields you want to search for here
"gender",
"birthdate"
);
$searchterm = $query->query_vars['s'];
// we have to remove the "s" parameter from the query, because it will prevent the posts from being found
$query->query_vars['s'] = "";
if ($searchterm != "") {
$meta_query = array('relation' => 'OR');
foreach($custom_fields as $cf) {
array_push($meta_query, array(
'key' => $cf,
'value' => $searchterm,
'compare' => '=='
));
}
$query->set("meta_query", $meta_query);
};
}
add_filter( "pre_get_posts", "custom_search_query");
This works great when searching orders, but what I need to search is subscriptions, where it doesn't work.
Help would be much appreciated!

Event Espresso Query Modification?

I am using Event Espresso with WordPress.
May u help me out in further query modification?
Hope you will :)
I want to use meta_query to list events on page.
Somewhat like below code.
$atts = array(
'title' => NULL,
'limit' => 10,
'css_class' => NULL,
'show_expired' => FALSE,
'month' => NULL,
'category_slug' => NULL,
'order_by' => 'start_date',
//'order_by' => 'end_date',
'sort' => 'DESC',
'meta_query' => array(
array(
'key' => 'start_date',
'value' => '2017-01-08 08:00:00',
'type' => 'DATETIME',
'compare' => '>=',
),
)
);
I want to implement search functionality for Event Espresso and i have those fields:
State - Dropdown (How to list all state? May be Venue)
Category - Dropdown
Start Date - Datepicker
End Date - Datepicker
Keyword - input
On submit those values will be submitted and based on those , i will get filtered events that are related with those values.
So how to implement this?
Please help.
Thanks in Advance
A slightly delayed response to this...
First, this depends on the version of EE you're using. They support both EE3 and EE4. I use EE4, so any reference to code I make is specific to version 4.
In order to create an events archive with a filtering functionality, you'd need to use the EE events archive. EE uses custom database tables and a lot of different post types to achieve what you see, so creating a simple archive with these filters won't work very well. Very little is stored in the _posts and _postmeta tables and you need to get post meta from related post types and tables that aren't laid out like WP_Query likes. They have a shortcode for the events list, which is laid out here and has a lot of the filters you're looking for, but no search functionality.
Their support forum has a lot of snippets and such created by their staff and there's a (long) related post about formatting the events archive page here. You'll also need to go through this documentation to see about their custom methods and hooks.
You could copy over and modify the archive templates they describe in a child theme to add search functionality to the top of the page. This would allow you to directly override the archive. You'll need to make use of some fancy filters, which are covered pretty clearly over on WPMU Dev.
The pieces of information you're looking for for filtering are here, minus keyword:
<?php
if( have_posts() ){
while ( have_posts() ){ the_post(); // enter the WordPress loop
$id = get_the_ID(); // get the ID of the current post in the loop, post ID = EVT_ID
$terms = get_the_terms( $id, 'espresso_event_categories' ); // get the event categories for the current post
if ( $terms && ! is_wp_error( $terms ) ) {
$cats = array();
foreach ( $terms as $term ) {
// do something
}
}
$event = EEM_Event::instance()->get_one_by_ID( $id ); // get the event OBJECT using EE's existing method
$venue = $event->venue(); // get the venue object for the current event
$state = $venue instanceof EE_Venue ? $venue->state_abbrev(); // get the event venue's state, but you can use state_name() to get the full name
// using the event to get the first and last date for the entire event. Sub $datetime for $event to do it per datetime
$start_date = $event->start_date('j M Y');
$end_date = $event->end_date('j M Y');
$start_time = $event->start_time(get_option('time_format'));
$end_time = $event->end_time(get_option('time_format'));
?>
<!-- Do some awesome layout stuff here -->
<?php
}
}
espresso_pagination();
?>
This will give you the meta for each post within the loop as variables, but you may want to pull them into pre_get_posts. You can just as easily create an array of $events and then filter it using the variables.
I'm not sure what your exact needs are regarding keyword. Do you mean tags? Title keywords? Description search? You'll need to narrow down exactly what you need this to do in order to write a function for it.

RSS item elements not being not being read by XML Reader

I don't know if i am asking this correctly but plz help me.
I am using Drupal Views to display RSS for my site. I am using the 'Views RSS: Core Elements' module to map the RSS fields with the custom fields added by me in the views. All works well. But a requirement forces me to add custom item elements in the RSS display. I am implementing the hook_views_rss_item_elements() to add custom item elements. When i view the RSS page i get the see the custom items but when i try to read the RSS using a RSS reader, the items are not being read. What am i doing wrong, or am i missing something?
Here is my code to add custom items to the 'SHOW ITEM ELEMENTS : CORE' for RSS display:
function Mymodule_views_rss_item_elements() {
$elements['blogs_title_image'] = array ( 'title' => t('Blogs Title Image'), 'description' => t('Blogs Title Image'), );
$elements['blogs_additional_tags'] = array ( 'title' => t('Blogs Additional Tags'), 'description' => t('Blogs Additional Tags'), );
$elements['blogs_short_desc'] = array ( 'title' => t('Blogs Short Description'), 'description' => t('Blogs Short Description'), );
return $elements;
}
After some research i did manage to find my way out:
I implemented hook_views_rss_namespaces to add a custom namespace and the custom items under the namespace:
function Mymodule_views_rss_namespaces() {
$namespaces['blogs'] = array(
'prefix' => 'xmlns',
'uri' => 'http://base.google.com/ns/1.0',
);
return $namespaces;
}
Note: Use the same 'prefix' and 'uri' as given in the above code or else namespace uri error will arise.

Wordpress query all categories for recent and always from specific categories limit

I seen other questions regarding something similar but nothing exactly like I need. I want to query the wp database for the most recent 6 posts. However, I always need it to pull one post from two certain categories.
To sum up: Pull 4 recent posts and always grab one from two specific categories totaling 6 posts. How can this be achieved? Right now I am able to pull 6 recent posts but I am not able to tell it to always pull 1 post from two other categories regardless if they are recent or not.
Array //$args
(
[post_status] => publish
[posts_per_page] => 6
[ignore_sticky_posts] => 1
[post_type] => post
[meta_key] => _thumbnail_id
[suppress_filters] =>
[] => Array
(
[0] => Array
(
[taxonomy] => category
[field] => id
[terms] => Array
(
)
)
)
)
$carousel_query = new WP_Query( $args );
if ( $carousel_query->have_posts() ) :
while ( $carousel_query->have_posts() ) : $carousel_query->the_post();
........
endwhile;
endif;

How are Wordpress Dynamic Sidebar Widgets rendered?

I have been reading through the Wordpress Source, trying to get a better understanding of how dynamic sidebars are rendered.
However, I am hitting a sticking point...
894 | do_action( 'dynamic_sidebar', $wp_registered_widgets[$id] );
I can't find where add_action('dynamic_sidebar', ... ) is defined. Without that part, I am sort of lost in what happens.
See the code here:
https://github.com/WordPress/WordPress/blob/b7c13e27c255e1fc1f03ab2ab432f1652a0ac212/wp-includes/widgets.php#L894
And to give more context, I am trying to figure out how to grab an array of widgets from a specific sidebar, and from there, I need to know how would you render each widget within that array.
I need finer control than dynamic_sidebar(...); gives me
Well, that specific line permits you to access each registered Widget properties, and it's used like:
<?php
/* Plugin Name: Test registered widgets */
add_action( 'dynamic_sidebar', 'sidebar_widgets_so_18666065' );
/**
* As this is an action hook, we don't return nothing
* use the passed values to do your stuff
*/
function sidebar_widgets_so_18666065( $registered_widget )
{
# Each registered widget passes the following array
/*
$registered_widget = Array
(
[name] => Meta
[id] => meta-2
[callback] => Array
(
[0] => WP_Widget_Meta Object
(
[id_base] => meta
[name] => Meta
[widget_options] => Array
(
[classname] => widget_meta
[description] => Log in/out, admin, feed and WordPress links
)
[control_options] => Array
(
[id_base] => meta
)
[number] => 2
[id] => meta-2
[updated] =>
[option_name] => widget_meta
)
[1] => display_callback
)
[params] => Array
(
[0] => Array
(
[number] => 2
)
)
[classname] => widget_meta
[description] => Log in/out, admin, feed and WordPress links
)
*/
}
Relevant search query at WordPress Answers.

Resources