Add categories to default WordPress search - php

I have a search form containing select fields. The first two are populated with custom taxonomies and the third with the default wordpress categories. When using the first two only for a query it works fine. When I use the third( the categories), the search query just ignores the field and comes up with the same results. How can I fix this?
I've used these functions to make them work:
function ftiaxnospiti_filter_search($query) {
if ( is_admin() || ! $query->is_main_query() ) {
return;
}
if ( $query->is_search ) {
$query->set( 'post_type', array('post', 'seller') );
}
return $query;
};
add_action('pre_get_posts', 'ftiaxnospiti_filter_search');
function ftiaxnospiti_add_custom_types_to_tax( $query ) {
if ( is_admin() || ! $query->is_main_query() ) {
return;
}
if ( is_category() || is_tag() && empty( $query->query_vars['suppress_filters'] ) ) {
// Get all your post types
$post_types = array( 'post', 'seller' );
$query->set( 'post_type', $post_types );
}
return $query;
}
add_action( 'pre_get_posts', 'ftiaxnospiti_add_custom_types_to_tax' );

Related

WordPress prevent specific posts from query with elementor

I am trying to manipulate an elementor query for all post types. I am using the Profile Builder plugin to restrict content. Their support sent me this function to check wether the post is restricted or not:
wppb_content_restriction_is_post_restricted( $post_id )
When I output the result of this function on the post it is either 1 or nothing. (1 being restricted).
The support also send me a custom addon-plugin of another plugin they have, where I'd only need to add the new function. When I do this and install it to my page, nothing changes though.
Here is the code of their mini plugin:
<?php
/*
Plugin Name: Paid Member Subscriptions - Exclude Restricted Posts From Query
Plugin URI: http://www.cozmoslabs.com
Description: Exclude restricted posts and pages from the main queries like blog, archive and taxonomy pages. It does not exclude them from custom queries.
Author: Cristian Antohe
Version: 1.0
Author URI: http://www.cozmoslabs.com
*/
add_action( 'pre_get_posts', 'pmsc_exclude_post_from_query' );
function pmsc_exclude_post_from_query( $query ) {
remove_action('pre_get_posts', 'pmsc_exclude_post_from_query');
if( !function_exists( 'pms_is_post_restricted' ) || is_admin() || is_single() )
return;
if( $query->is_main_query() || ( $query->is_search() && isset( $_GET['s'] ) ) ) {
$args = $query->query_vars;
$args['suppress_filters'] = true;
$args['posts_per_page'] = get_option( 'posts_per_page' );
$posts = get_posts($args);
$ids = wp_list_pluck( $posts, 'ID' );
$restricted_ids = array_filter($ids,'pms_is_post_restricted');
$query->set( 'post__not_in', $restricted_ids );
}
}
In case that a post is restricted, I want to exclude it from my query. So that the user only sees posts that can be accessed.
Now the challenge is to write a custom query that I can then put inside of the query ID of elementor's post widget to perform my custom query.
My query looks like this so far:
function prevent_restricted_posts_from_loading( $query ) {
if( !function_exists( 'wppb_content_restriction_is_post_restricted' ) || is_admin() || is_single() )
return;
if( $query->is_main_query() || ( $query->is_search() && isset( $_GET['s'] ) ) ) {
$args = $query->query_vars;
$posts = get_posts($args);
$ids = wp_list_pluck( $posts, 'ID' );
$restricted_ids = array_filter($ids,'wppb_content_restriction_is_post_restricted');
$query->set( 'post__not_in', $restricted_ids );
}
}
add_action( 'elementor/query/prevent_restricted_posts_from_loading', 'prevent_restricted_posts_from_loading' );

How to make the "coupon amount" column sortable in WooCommerce

Departing from the following tutorial: "Columns in WooCommerce", the intention is to make the coupon amount column sortable based on amount.
Making it sortable seems to be successful with my already written code. Nevertheless, the rule to sort goes wrong.
I've tried several things before but the sort code is not applied after clicking,
Who would like to take a closer look?
What I've used so far:
add_filter('manage_edit-shop_coupon_sortable_columns', 'misha_sortable');
function misha_sortable( $sortable_columns ){
$sortable_columns['amount'] = 'amount';
return $sortable_columns;
}
add_action( 'pre_get_posts', 'misha_filter' );
function misha_filter( $query ) {
// if it is not admin area, exit the filter immediately
if ( ! is_admin() ) return;
if( empty( $_GET['orderby'] ) || empty( $_GET['order'] ) ) return;
if( $_GET['orderby'] == 'amount' ) {
$query->set('meta_key', 'amount' );
$query->set('orderby', 'meta_value'); // or meta_value_num
$query->set('order', $_GET['order'] );
}
return $query;
}
The correct metakey is coupon_amount instead of amount
So this should suffice
// Make column sortable
function filter_manage_edit_shop_coupon_sortable_columns( $columns ) {
$columns['amount'] = 'amount';
return $columns;
}
add_filter( 'manage_edit-shop_coupon_sortable_columns', 'filter_manage_edit_shop_coupon_sortable_columns', 10, 1 );
// Fires after the query variable object is created, but before the actual query is run.
function action_pre_get_posts( $query ) {
// If it is not admin area, exit the filter immediately
if( ! is_admin() ) return;
// Get orderby
$orderby = $query->get( 'orderby' );
// Set query
if( $orderby == 'amount' ) {
$query->set( 'meta_key', 'coupon_amount' );
$query->set( 'orderby', 'meta_value_num' );
}
}
add_action( 'pre_get_posts', 'action_pre_get_posts', 10, 1 );

Advanced Custom Field Filter Wordpress

Could someone please explain why the first section of code works but not the second. I am trying to filter the content on a website based on what values have been selected in the backend on each post using Advanced Custom Fields.
function my_pre_get_posts( $query ) {
// do not modify queries in the admin
if( is_admin() ) {
return $query;
}
// only modify queries for 'event' post type
if( isset($query->query_vars['post_type']) && $query->query_vars['post_type'] == 'property' ) {
// allow the url to alter the query
if( isset($_GET['bedrooms']) ) {
$query->set('meta_key', 'bedrooms');
$query->set('meta_value', $_GET['bedrooms']);
}
}
// return
return $query;
}
add_action('pre_get_posts', 'my_pre_get_posts');
non working:
add_action('pre_get_posts', 'my_pre_get_posts');
function my_pre_get_posts( $query ) {
// bail early if is in admin
if( is_admin() ){
return;
}
// get meta query
$meta_query = $query->get('meta_query');
if( isset($_GET['bedrooms']) ){
$meta_query[] = array(
'key' => 'bedrooms',
'value' => $_GET['bedrooms'],
'compare' => '=',
);
}
// update meta query
$query->set('meta_query', $meta_query);
return;
}

Strange side effect of a PHP post query code in Wordpress

Why this code in functions.php has this strange side effect of switching the menu to the mobile version in not-home pages in wordpress?
function my_blog_category( $query ) {
if ( $query->is_home() && !is_front_page() || is_archive()) {
$query->set( 'cat', '6');
}
}
add_action( 'pre_get_posts', 'my_blog_category' );
This code should affect only posts in blog and archive page so why's that?
The reason it's not working is because you were asking:
If $query->is_home() && !is_front_page()
Or, is_archive()
Group your conditional expression using brackets.
function my_blog_category( $query ) {
if ( $query->is_home() && ( !is_front_page() || is_archive() ) ) {
$query->set( 'cat', '6');
}
}
add_action( 'pre_get_posts', 'my_blog_category' );
Now you're asking:
If $query->is_home()
And, if !is_front_page() or is_archive()

Replace custom content type slug in Wordpress

In WordPress, I'm using Jetpack's portfolio custom content type, but would like to change the slug from "portfolio" to "examples". I found this example on how to do it (http://www.markwarddesign.com/2014/02/remove-custom-post-type-slug-permalink/) and this plugin (https://github.com/devinsays/no-slug-portfolio-post-types). Both as based on a post on Wordpress VIP that was linked to by the Jetpack team and now has a dead link.
Here is my code, for some reason it is not working. I have refreshed my permalinks by going to Settings > Permalinks and hitting save changes.
/**
* Remove the slug from custom post type permalinks.
*/
function vipx_remove_cpt_slug( $post_link, $post, $leavename ) {
if ( ! in_array( $post->post_type, array( 'portfolio' ) ) || 'publish' != $post->post_status )
return $post_link;
$post_link = str_replace( '/' . $post->post_type . '/', '/examples', $post_link );
return $post_link;
}
add_filter( 'post_type_link', 'vipx_remove_cpt_slug', 10, 3 );
/**
* Some hackery to have WordPress match postname to any of our public post types
* All of our public post types can have /post-name/ as the slug, so they better be unique across all posts
* Typically core only accounts for posts and pages where the slug is /post-name/
*/
function vipx_parse_request_tricksy( $query ) {
// Only noop the main query
if ( ! $query->is_main_query() )
return;
// Only noop our very specific rewrite rule match
if ( 2 != count( $query->query )
|| ! isset( $query->query['page'] ) )
return;
// 'name' will be set if post permalinks are just post_name, otherwise the page rule will match
if ( ! empty( $query->query['name'] ) )
$query->set( 'post_type', array( 'post', 'portfolio', 'page' ) );
}
add_action( 'pre_get_posts', 'vipx_parse_request_tricksy' );
Any ideas how to get this code working again?
function na_remove_slug($post_link, $post, $leavename) {
if ('POST TYPE SLUG' != $post - > post_type || 'publish' != $post - > post_status) {
return $post_link;
}
$post_link = str_replace('/'.$post - > post_type.
'/', '/', $post_link);
return $post_link;
}
add_filter('post_type_link', 'na_remove_slug', 10, 3);
function na_parse_request($query) {
if (!$query - > is_main_query() || 2 != count($query - > query) || !isset($query - > query['page'])) {
return;
}
if (!empty($query - > query['name'])) {
$query - > set('post_type', array('post', 'POST TYPE SLUG', 'page'));
}
}
add_action('pre_get_posts', 'na_parse_request');

Categories