Excluded category posts from admin, add them to search - wordpress

The story is like this, I wanted to createa new menu in WP Admin that displays only posts from a specific category (cat 13 in our case). While this works pretty well, the problem is, when I try to used the WP admin search, no posts from that category is displayed. I'm not sure how to approach this, at this moment I'm scratching my head.
Current code is:
function exclude_category_posts( $query ) {
if ( $query->is_main_query() && is_admin()) {
if($_REQUEST['page_type']=="single_cat")
$query->set( 'cat', '13' );
else
$query->set( 'cat', '-13' );
}
}
add_filter( 'pre_get_posts', 'exclude_category_posts' );
How can I do this to allow the search to look for posts from that category..or better, on that separate page I made, to make the search look only for posts from that category.
Thanks in advance!

Related

Display posts on Elementor that belong to Page Author ID (Page Creator)

Hi I wanted to use elementor widgets and show some custom posts for current post author. I thought maybe with Query ID in Elemntor I can do it easily so the widget will show **some custom posts of "Page Author" I wrote a query that show the custom-post-type1 and custom-post-type2 of current post author
My query doesn't work unfortunately
function mzba_post_types( $query ) {
$query->set( 'post_type', [ 'custom-post-type1', 'custom-post-type2' ] );
$author = get_queried_object_id();
if( is_single() ){
$author = get_the_author_meta( 'ID' );
}
$query_vars[ 'author' ] = $author;
}
add_action( 'elementor/query/{$query_id}', 'mzba_post_types' );
anyone can help for a query (or any other way) that shows the Page creator custom posts in Elementor?
thank you so so mcuh for helping
This might help you. However, you can try pre-build filters as well to achieve this.
https://elementor.com/blog/introducing-advanced-query-control/

Elementor Pro - Custom Query

I need some help. I am trying to create a custom query for my Custom Posts I created in Wordpress, and using Elementor Pro.
On my post, I added a custom field 'sorting' with a numeric value, which I would like to use to order my posts manually.
However, I cannot seem to get this to work.
I am using the latest Elementor pro version.
And I tried following the instructions as per their page: https://developers.elementor.com/custom-query-filter/
Here is my code I added to my theme's functions.php file
// Showing posts ordered by comment count in Posts Widget
add_action( 'elementor/query/speaker_order', function( $query ) {
// Here we set the query to fetch posts with
// ordered by comments count
$query->set( 'orderby', 'sorting' );
} );
I have added 'speaker_order' as the Query ID in the Elementor Editor.
You are close. There's one thing you left out (if I am grasping what you want to do).
It should look like this:
add_action( 'elementor/query/speaker_order', function( $query ) {
// Here we set the query to fetch posts with
// ordered by comments count
$query->set( 'meta_key', 'sorting' );
$query->set( 'orderby', 'sorting' );
} );
You have to add two more lines of code:
// Showing posts ordered by comment count in Posts Widget
add_action( 'elementor/query/speaker_order', function( $query ) {
$query->set('meta_key','sorting');
$query->set('orderby', 'sorting');
$query->set('orderby','ASC');
});

How to list post of certain category (only one category lets say cat id = 5 ) only on blog listing page?

I need to list posts of certain category only on blog listing page , let's say of category id 5. I need it to do from plugins or function.php file. I do not want to change the template files, i think blog listing is on index.php.
I used parse_query hook like following. But is affect other places as well . The menu bar is gone. Please help me. Thank you.
add_filter( 'parse_query', 'pp_posts_filter' );
function pp_posts_filter( $query ){
$query->query_vars['cat'] = 5;
}
To make the query changes specific to main query only and not secondary ones like Menus or Sidebars etc. Use is_main_query function i.e.
add_action( 'pre_get_posts', 'foo_modify_query_exclude_category' );
function foo_modify_query_exclude_category( $query ) {
if ( ! is_admin() && $query->is_main_query() && ! $query->get( 'cat' ) )
$query->set( 'cat', '-5' );
}

how to sort articles in the designated category of WordPress?

There are many categories in my wordpress ,and there are many articles in every category.
Now i want to sort articles in the 'python' category at my willing .
How can i do then?
you will have to add a meta box to save a metakey for each post as i can't see a logic for the sort order you want, something like a orderby num e.g. 'pythonsort' see - http://codex.wordpress.org/Function_Reference/add_meta_box
then you can add this to your functions file:
function sort_by_order(){
if ( is_admin() ) {
$wp_query->set( 'orderby', 'meta_value_num' );
$wp_query->set( 'meta_key', 'pythonsort' );
}
}
add_filter('pre_get_posts', 'sort_by_order' );

Disable Shop page on Woocommerce to protect categories

Im trying to disable "shop" page in Woocommerce. Basically im creating a shop theme to sell prints and image downloads for a photographer.
Because i need to create private galleries i created a custom post type where i use the woocommerce category shortcode to show products and then i password protect the post type.
This is a workaround for password protecting the woocommerce categories (if someone knows a better one please explain).
The problem is that is someone goes to /shop they will all products, including the "protected ones". So i need to disable the shop page and i need to do it programmatically on my theme functions. Any thoughts?
To disable the shop page, copy over the archive-product.php file from the /wp-content/plugins/woocommerce/templates/archive-product.php and put in /wp-content/themes/{Your Theme}/woocommerce/archive-product.php
Open up the file and overwrite everything in file with the following code below:
<?php
global $wp_query;
$wp_query->set_404();
status_header(404);
get_template_part('404');
Save the file, and now your Shop page is gone and replaced with a 404 Page!
Add this to functions:
function woocommerce_disable_shop_page() {
global $post;
if (is_shop()):
global $wp_query;
$wp_query->set_404();
status_header(404);
endif;
}
add_action( 'wp', 'woocommerce_disable_shop_page' );
Docs: WooCommerce Conditional Functions Documentation
WooCommerce has a filter for the array that it uses to create the Product post type: woocommerce_register_post_type_product.
Rather changing the archive template to force it to redirect, you can completely remove the post type’s archive, but changing the has_archive attribute on the post type on creation.
add_filter('woocommerce_register_post_type_product', function($post_type) {
$post_type['has_archive'] = false;
return $post_type;
});
You should then remove the shop page in the CMS by going to WooCommerce » Settings » Product » Display, and clicking the “x” on the “Shop Page” option.
You might need to flush the permalink cache, which you can do just by clicking the “Update” button in Settings » Permalinks.
*Edit -
Apparently the page setting I suggested below no longer works. If WooCommerce doesn't have a plugin setting to change it, I personally would use a wordpress redirect plugin like Redirection. This way you can automatically redirect them from the undesired shop page to whatever page displays your products. It avoids a 404 issue and keeps everything in tact. It also avoids editing template files which adds complications to non-developers.
Old Answer:
Have you tried Woo settings?
Admin area, left main menu, Woocommerce > Settings
Click the pages tab.
Under Pages setup is "Shop Base Page", on the dropdown, there's a small "x" to right right. Click that to get rid of the page.
If there are links elsewhere that need to be fixed let me know and I'll find the hooks/filters to remedy it.
template_redirect is the last hook before page render so in my use case I ask if the page being viewed is the "shop" page and if it is I redirect to (in my case) a pricing page.
function my__template_redirect(){
if(is_shop()){
wp_redirect(site_url() . '/pricing/', '302');
}
}
add_action('template_redirect', 'my__template_redirect');
The last suggestion didn't work for me with WP 4.6.1 and WooCommerce 2.6.4. Hiding products in the Publish tab works for me.
http://paperhedge.com/hide-products-from-displaying-in-shop-page-woocommerce/
To disable the default shop page and leave the /shop/ slug free for custom pages use this:
function remove_woocommerce_default_shop( $args, $post_type ) {
if (class_exists('WooCommerce')) {
if ( $post_type == "product" ) {
$args['has_archive'] = true;
}
return $args;
}
}
add_filter('register_post_type_args', 'remove_woocommerce_default_shop', 20, 2);
You need to hook a couple (or maybe more) things:
/* hide category from shop pages */
add_action( 'pre_get_posts', 'custom_pre_get_posts_query' );
function custom_pre_get_posts_query( $q ) {
if ( ! $q->is_main_query() ) return;
if ( ! $q->is_post_type_archive() ) return;
if ( ! is_admin() ) {
$q->set( 'tax_query', array(array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => array( YOUR-CATEGORY-SLUG, YOUR-CATEGORY-SLUG-2 ), // Don't display products in the knives category on the shop page
'operator' => 'NOT IN'
)));
}
remove_action( 'pre_get_posts', 'custom_pre_get_posts_query' );
}
Change, obviusly YOUR-CATEGORY-SLUG, YOUR-CATEGORY-SLUG-2 for your cat or cats to hide from shop pages.
Then, you need to hide them from menues too, right?:
/* hide category from menues */
add_filter( 'get_terms', 'get_subcategory_terms', 10, 3 );
function get_subcategory_terms( $terms, $taxonomies, $args ) {
$new_terms = array();
// if category and on the shop page (change it with is_woocommerce() if want to hide from all woo pages)
if ( in_array( 'product_cat', $taxonomies ) && ! is_admin() && is_shop() ) {
foreach ( $terms as $key => $term ) {
if ( ! in_array( $term->slug, array( YOUR-CATEGORY-SLUG, YOUR-CATEGORY-SLUG-2 ) ) ) {
$new_terms[] = $term;
}
}
$terms = $new_terms;
}
return $terms;
}
But i don´t think this solve 100% the thing, since then, what about search results?
Try this
Create new page named "Shop"
Go to "woocommerce" > "Settings" > "Product" > "Display tab"
Select shop page named "Shop" then click save changes
Back to "Pages" then delete "Shop" page (keep the page on trash,
don't delete permanently)
This method works fine for me

Resources