Filter by part of the slug GhostCMS API - ghost-blog

I am using NextJS + GhostCMMS and #tryghost/content-api
I want to filter all pages by part of the slug.
For the example, if I have pages - slug1-test, slug2-test, slug3-test, slug4, ...
I want to select all pages with -test at the end of the slug.
Then the result of the example will be:
slug1-test, slug2-test, slug3-test.
This is my code:
const posts = await api.posts.browse({
limit: 10,
include: ['tags', 'count.posts'],
order: ['featured DESC', 'published_at DESC'],
filter: 'slug:[slug1-test]'
})
This code returns a post with a slug slug1-test, because there is a post with exactly that slug, but I need to select, only posts with slug, which includes -test at the end.

Related

WooCommerce, WP all import - Find product ID by attribute value

I want to update WooCommerce products by the WP All import. I have 2 independent xml files (from the first one I'm getting prices, from the second one descriptions). For some reasons product's _sku is {code} from the first one xml not {ean}. So I put {ean} into product attribute pa_ean to match the products from the first one and the second one xml.
Now I don't know how to write the php function to return product_id so WP All import could update right products with right descriptions.
Do you have any unique identifier that are same for the product in both files? Like product_key or something. If you have it you can store it as custom meta for that product and get product by that meta key, something like below ↓
function check_if_product_exist($rawDataFromFile) {
global $products;
// Get WC_Product Objects
$products = wc_get_products([
'meta_key' => 'your_meta_key',
'meta_value' => $rawDataFromFile->your_key
]);
if (!empty($products)) {
return $products[0];
}
return null;
}
I put {ean} to Custom field, not to Attribute and after that I can easily use it in WP All import.

NextJS dynamic urls query not passing back ID

I am trying to load data when clicking on a <Link> by passing an ID as a query param. I also need to have a clean URL so I set the "as" option. My problem is that the query param I get back on the loading page is now the value I passed in "as" so I can't use the ID to fetch my data.
Is there an alternative way or something I'm missing?
What I need query param to be is => id = 3
What I'm getting is the slug name => id = "Uncategorized"
<Link key={i} href={{pathname: '/blog/category/[id]', query: {id: cat.category_id}}} as={/blog/category/${cat.category_slug}}>
<a>{cat.category_name}</a>
</Link>
I solved this by fetching all my categories before fetching my posts and matching the category slug with the query.id. I don't know if there is a more nextjs way of doing this but it works for now.

Select posts that have a specific term using withSelect in Gutenberg block

I'm trying to select posts -- or more specifically, a custom post type -- that belong to a specific term, and display them in my Gutenberg block's edit screen. I'm wanting to recreate what would be tax_query with WP_Query, but in Javascript.
I'm able to select posts, but I'm unsure what parameters to use with getEntityRecords to select by term (or if it is even possible). The documentation is still a little vague at this point.
Here's where I'm at. This successfully selects all posts of the 'rmenu' type:
const items = select("core").getEntityRecords(
"postType",
"rmenu"
);
Does anyone know if getEntityRecords is the right way to handle this?
Thanks.
The third parameter to getEntityRecords is a query object. You can pass any query argument accepted by Wordpress API such as categories or tags. Selecting by category term would look like this:
const items = select("core").getEntityRecords(
"postType",
"rmenu",
{ categories: [ 13 ] }
}
Although Capi's answer is correct, it wasn't clear for me that this would also work for custom taxonomies. It turns out wp.data will add your custom taxonomies automatically as properties to the post object. For example, a post could look like this:
{
title: "hello world",
content: "this is a post",
id: 123,
type: 'my-custom-posttype'
my-custom-tax: [5, 8, 24],
...
}
So, in order to get all the posts of the type my-custom-posttype that have a term with ID 4 or 8 of the taxonomy my-custom-tax, you would run this query:
wp.data.select( 'core' ).getEntityRecords( 'postType', 'my-custom-posttype', { 'my-custom-tax':[4,8] })
Important! If you test this in your browser, you need to run it twice. The first time it will return an empty array, because it only invokes the promise.

Modify the product search query

I have replaced the wordpress search engine with the WooCommerce product search engine, that is, to include it in the header of my site I use:
get_product_search_form();
instead of :
get_search_form (true);
I would like the product search engine to also search the product categories.
The problem is that I have a category called "Jackets" but the product in its title and in its content uses the word without the "s" so that if I search for the word "jacket" I get many results, but if I search the word "jackets" (with "s") I do not get any, despite that being the name of the category.
I have thought about modifying the query that makes the request so that it adds something of the type: OR category_name LIKE $ search_term, and I tried to use pre_get_posts in functions.php to modify the query, but it doesn't work.
Could someone help me modify that search query for products so that I can also return the products that are of the category whose name matches with the search term.
Thank you very much in advance!
Kind regards,
Raquel
You can look at this link .
simple example:
// hook into wp pre_get_posts
add_action('pre_get_posts', 'jc_woo_search_pre_get_posts');
function jc_woo_search_pre_get_posts($q){
if ( is_search() ) {
add_filter( 'posts_join', 'jc_search_post_join' );
add_filter( 'posts_where', 'jc_search_post_excerpt' );
}
}

Adding a custom query argument to a custom taxonomy term page

I have a custom taxonomy created and I'm trying to add a new query argument with add_query_arg(), but it's not recognizing it at all when I visit the URL of a term of that taxonomy, with a query in the URL
for example, for a playlist taxonomy and the "edm" term and this URL:
http://someurl.com/playlist/edm/?some_variable=abc
some_variable is not recognized at all
When I use this code, to show all the query vars:
global $wp_query;
print_r($wp_query->query_vars);
some_variable doesn't exist, at all. It's like it doesn't see it at all!
You want to implement a hook somewhere in your plugin or theme so that WP is aware of your new variable.
public function my_plugin_add_query_vars($vars) {
$vars[] = "some_variable";
return $vars;
}

Resources